var MAX_NAV_LEVEL = 6;
var nw;
var html_w;

var HOME_PG = '../cgi-bin/home.cgi';
var EVENT_PG = '../cgi-bin/event.cgi';
var EVENT_HTML = '../events/event.html';
var PIC_PG = '../cgi-bin/pictures.cgi';
var PIC_HTML = '../events/pictures.html';
var PIC_UP_PG = '../cgi-bin/pictures_upload.cgi';
var PIC_UP_HTML = '../events/pictures_upload.html';
var RPT_PG = '../cgi-bin/report.cgi';
var RPT_HTML = '../events/report.html';
var BUG_PG = '../cgi-bin/bugs.cgi';
var BUG_RPT_PG = '../cgi-bin/bug_rpt.cgi';
var CAL_PG = '../cgi-bin/calendar.cgi';
var EMAIL_PG = '../cgi-bin/email.cgi';
var CONTACT_PG = '../cgi-bin/contacts.cgi';
var HTML_PG = '../about/write_html.html';
var LOCAL_PG = '../cgi-bin/local.cgi';
var INTERN_PG = '../cgi-bin/intern.cgi';
var NEWS_PG = '../cgi-bin/news.cgi';
var LINKS_PG = '../cgi-bin/links.cgi';
var INV_PG = '../cgi-bin/inventory.cgi';
var UPDATE_PG = '../cgi-bin/update_login.cgi';
var MINUTES_PG = '../cgi-bin/minutes.cgi';
var MEMBERS_PG = '../cgi-bin/members.cgi';
var ACTION_PG = '../cgi-bin/action_pts.cgi';
var ACTION_EDIT_PG = '../cgi-bin/action_pts_edit.cgi';
var ACTION_SCHED_PG = '../cgi-bin/action_pts_sched.cgi';
var ACTION_NOTE_PG = '../cgi-bin/action_pts_note.cgi';
var SCHEDULE_PG = '../cgi-bin/schedule.cgi';
var XFER_PG = '../cgi-bin/streeto_xfer.cgi';

/* Make sure we are at the main page - if not, put the page the person is trying to get to into the div/iframe */

var qs = top.location.href.indexOf('?');
if (qs == -1)  
	qs = top.location.href.length;
var url = top.document.location.href.substring(0, qs);

if (url.toLowerCase() != "http://www.ardf.org.au/cgi-bin/menu.cgi" && url.toLowerCase != "www.ardf.org.au/cgi-bin/menu.cgi" && url.toLowerCase() != "http://www.ardf.org.au/cgi-bin/menu1.cgi" && url.toLowerCase != "www.ardf.org.au/cgi-bin/menu1.cgi" && url.toLowerCase != "http://www.ardf.org.au/cgi-bin/operatest.html") {
	// Remove domain name
	var temp = url.replace("http://www.ardf.org.au/", "");
	temp = temp.replace("www.ardf.org.au/", "");
	if (temp == "cgi-bin/pictures.cgi" || temp == "cgi-bin/pictures_upload.cgi" || temp == "cgi-bin/event.cgi" || temp == "events/event.html" || temp == "events/pictures.html" || temp == "events/pictures_upload.html")
		temp = "cgi-bin/calendar.cgi";
	top.location.href = "http://www.ardf.org.au/cgi-bin/menu.cgi?frm_src=" + temp;
}

/************ Nav link stuff **************/

function linkStyle(obj, over_fg, f_size) {
	if (over_fg == 1) {
		obj.className = 'lblNavLinkOver' + f_size;
		obj.style.cursor = 'pointer';
	}
	else {
		obj.className = 'lblLinkStyle' + f_size;
	}
}

function removeNav(nav_id) {
	var i;
	for (i = nav_id + 1; i <= MAX_NAV_LEVEL; i++) {
		top.document.getElementById("spnArrow" + eval(i - 1)).innerHTML = "";
		var spnl = top.document.getElementById("spnLink" + i);
		spnl.innerHTML = "";
		spnl.className = "lblNavNoLink";
		top.document.getElementById("spnURL" + i).value = "";
	}
	top.document.getElementById("spnLink" + nav_id).className = "lblNavNoLink";
	top.document.getElementById("spnIndex").value = nav_id;
	if (nav_id == 1)  top.document.getElementById("btnMenuBack").style.display = "none";
}


function setNav(url_nm, item_nm) {
	top.document.getElementById('fraMain').src = url_nm;
	setLinks(url_nm, item_nm);
	top.document.getElementById("btnMenuBack").style.display = "";
}

function setLinks(url_nm, item_nm) {
	var ti = top.document.getElementById("spnIndex");	
	for(var i = 1; i <= eval(ti.value); i++) {	// Check if link to item already exists (mainly for back button issues)
		if (top.document.getElementById("spnLink" + i).innerHTML == item_nm) {
			removeNav(i);
			return;
		}
	}
	top.document.getElementById("spnArrow" + ti.value).innerHTML = ">>";
	top.document.getElementById("spnLink" + ti.value).className = "lblNavLink";
	ti.value = eval(ti.value) + 1;
	top.document.getElementById("spnURL" + ti.value).value = url_nm;
	var spnl = top.document.getElementById("spnLink" + ti.value);
	spnl.innerHTML = item_nm;
}

function goBack() {
	var ti = top.document.getElementById("spnIndex");
	if (ti.value == 1)
		return;
	if (top.document.getElementById("spnLink" + eval(ti.value - 1)).innerHTML == 'View Event') { top.document.getElementById('txtUpdate').value = 0; }
	top.document.getElementById('fraMain').src = top.document.getElementById("spnURL" + (eval(ti.value) - 1)).value;
	//location.href = top.document.getElementById("spnURL" + (eval(ti.value) - 1)).value;
	removeNav(eval(ti.value - 1));	
	
}

/********** Opening links **************/

function openWindow(url_nm) {
	if (nw != null) 
		try {nw.close();} catch (e) {}
	nw = window.open(url_nm, '');
}

function openHTMLWindow() {
	if (html_w != null)
		try {html_w.close();} catch (e) {}
	html_w = window.open(HTML_PG, '');
}

/*********** Iframe stuff **************/

function setLogin(upd_fg) {
	if (top.document.getElementById("txtInvalid").value == -1) { // Logged in
		document.getElementById("txtLogin").value = top.document.getElementById("txtLogin").value;
		document.getElementById("txtPassword").value = top.document.getElementById("txtPassword").value;
	}
	if (upd_fg >= 1) {
		document.getElementById("txt_s7_id").value = top.document.getElementById("txt_s7_id").value;
		document.getElementById("txtUpdate").value = top.document.getElementById("txtUpdate").value;
	}
	if (upd_fg >= 2) {
		document.getElementById("txtSize").value = top.document.getElementById("txtSize").value;
	}
	if (upd_fg >= 3) {
		document.getElementById("txtSel_s7_id").value = top.document.getElementById("txtSel_s7_id").value;
	}
	if (upd_fg == 4) {
		document.getElementById("txtWidth").value = top.document.getElementById("txtWidth").value;
	}
}

function submitForm (form_obj, action_page) {
	form_obj.action = action_page;
	form_obj.submit();
}

function setHiddenTop(s7_id, upd_fg) {
	top.document.getElementById('txt_s7_id').value = s7_id; 
	top.document.getElementById('txtUpdate').value = upd_fg;
}

function setHiddenTopYear(s7_id, upd_fg, year_val) {
	setHiddenTop(s7_id, upd_fg);
	top.document.getElementById('txtYear').value = year_val;
}

function checkItem (item_nm) {
	if (item_nm == 'View Event') { top.document.getElementById('txtUpdate').value = 0; }
	
}

/************ Generic functions ****************/

function chkNoChars(obj, ml) {
	var count = obj.value.length;
 	if(count > ml) 
		obj.value = obj.value.substring(0, ml);
}

 
function left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function onlyNos(val) {
    val = val.replace(/[^0-9.]/g,'');
    return val;
}

function onlyInts(val) {
    val = val.replace(/[^0-9]/g,'');
    return val;
}

function checkDodgyChars(str) {
	return (str.indexOf("\'") != -1 || str.indexOf('"') != -1 || str.indexOf("?") != -1);
}

/************ Automatic parsing of text fields **************/

function getRequestBody(obj) {
    var aParams = new Array();
    var els = obj.getElementsByTagName("textarea")
    
    for (var i=0 ; i < els.length; i++) {
        var sParam = encodeURIComponent(els[i].name);
        sParam += "=";
        sParam += encodeURIComponent(els[i].value);
        aParams.push(sParam);
    }
    
    var els = obj.getElementsByTagName("input")
	    
	for (var i=0 ; i < els.length; i++) {
			if (els[i].type != "text" && els[i].type != "hidden" && els[i].type != "password")	continue;
	        
	        var sParam = encodeURIComponent(els[i].name);
	        sParam += "=";
	        sParam += encodeURIComponent(els[i].value);
	        aParams.push(sParam);
    }
    
    return aParams.join("&");
}

function getLoginPword() {
	var str = '';
	try {
		str = "txtPassword=" + top.document.getElementById('txtPassword').value;
		str += "&txtLogin=" + top.document.getElementById('txtLogin').value;
		
	} catch (e) {
	
	}
	return str;
}
/*************** Generic Ajax stuff - left as a reference ***************/

function ajaxObj(s_flg) {
	var xmlHttp;
	try  {   // Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
    } catch (e) {  // Internet Explorer
    	try  {
      		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      	} catch (e) {
      		try {
        		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        	} catch (e) {
        		alert("Your browser does not support AJAX!\nThis application has been tested in IE 6.0+, Opera 9.10+ and Firefox 1.5+...");
        		return null;
        	}
      	}
    }
    return xmlHttp;
}

/*
	State	Description
	0	The request is not initialized
	1	The request has been set up
	2	The request has been sent
	3	The request is in process
	4	The request is complete
*/


function stateHandler() { 
 	if (xmlHttp.readyState == 4) { 
		if (xmlHttp.status == 200) { 
			div.innerHTML = xmlHttp.responseText;
		} else { 
			alert('Unable to return state data');
		} 
	}
}

var requester;
function testSrc() {
	try { 
 		requester = new XMLHttpRequest(); 
	} catch (e) { 
 		try { 
   			requester = new ActiveXObject("Microsoft.XMLHTTP"); 
 		} catch (e) {
 			return false; 
 		} 
	}
	alert(requester);
	requester.onreadystatechange = stateHandler;
	requester.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	
	requester.open("POST", "../cgi-bin/home.cgi"); 
	var sparam = getRequestBody(document.Forms[0]);
	requester.send("name=Bob&email=bob@example.com");
}


