var xhr = new Array(); // ARRAY OF XML-HTTP REQUESTS
var xi = new Array(0); // ARRAY OF XML-HTTP REQUEST INDEXES
xi[0] = 1; // FIRST INDEX SET TO 1 MAKING IT AVAILABLE

function xhrRequest(type) {
	if (!type) {
		type = 'html';
	}

	// xhrsend IS THE xi POSITION THAT GETS PASSED BACK
	// INITIALIZED TO THE LENGTH OF THE ARRAY(LAST POSITION + 1)
	// IN CASE A FREE RESOURCE ISN'T FOUND IN THE LOOP
	var xhrsend = xi.length; 
	
	// GO THROUGH AVAILABLE xi VALUES
	for (var i=0; i<xi.length; i++) {

		// IF IT'S 1 (AVAILABLE), ALLOCATE IT FOR USE AND BREAK
		if (xi[i] == 1) {
			xi[i] = 0;
			xhrsend = i;
			break;
		}
	}

	// SET TO 0 SINCE IT'S NOW ALLOCATED FOR USE
	xi[xhrsend] = 0;

	// SET UP THE REQUEST
	if (window.ActiveXObject) {
		try {
			xhr[xhrsend] = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xhr[xhrsend] = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	} else if (window.XMLHttpRequest) {
		xhr[xhrsend] = new XMLHttpRequest();
		if (xhr[xhrsend].overrideMimeType) {
			xhr[xhrsend].overrideMimeType('text/' + type);
		}
	}
	return (xhrsend);
}

function fcn(url, divID) {
	var xhri = xhrRequest('html');	
	xhr[xhri].open('GET', url, true);
	xhr[xhri].onreadystatechange = function() {
		//alert(url);
		if (xhr[xhri].readyState == 4 && xhr[xhri].status == 200) {
		    //alert(xhr[xhri].responseText);
			document.getElementById(divID).innerHTML=xhr[xhri].responseText;
			xi[xhri] = 1;
			xhr[xhri] = null;
		}
	};
	xhr[xhri].send(null);
}

function fcn2(url, divID) {
	var xhri = xhrRequest('html');	
	xhr[xhri].open('GET', url, true);
	xhr[xhri].onreadystatechange = function() {
		//alert(url);
		if (xhr[xhri].readyState == 4 && xhr[xhri].status == 200) {
		    //alert(xhr[xhri].responseText);
			removeFCKEditor(divID);
			document.getElementById(divID).value=xhr[xhri].responseText;
			//var oFCKeditor = FCKeditorAPI.GetInstance(divID);
			//oFCKeditor.Value = xhr[xhri].responseText;
			//alert("tet" + oFCKeditor.GetHTML())
			//alert("text box: " + document.getElementById(divID).value);
			xi[xhri] = 1;
			xhr[xhri] = null;
			//setFCKEditor(divID);
		}
	};
	xhr[xhri].send(null);
}


function gotoURL(URL) { 
	window.location = URL;
}

function ResetDL(dlid){
	var selectbox = document.getElementById(dlid);
	selectbox.selectedIndex = 0;
	reloadIframe();
}

function reloadIframe(){
	var cidDL = document.getElementById('FlowerCategoryDL');
	var cid = cidDL.options[cidDL.selectedIndex].value;
	var fidDL = document.getElementById('FlowerNameDL');
	var fid = fidDL.options[fidDL.selectedIndex].value;
	var colidDL = document.getElementById('FlowerColourDL');
	var colid = colidDL.options[colidDL.selectedIndex].value;
	var f = document.getElementById('iframeDiv');
	var kword = "";
	f.src = "../flowers/flowers_result.aspx?cid=" + cid + "&fid="+ fid + "&colid=" + colid + "&k=" + kword;
}



function SetDLs(cid){
	SetColourDL(cid);
	SetFlowerNameDL(cid);
	reloadIframe();
}


function SetColourDL(cid){
	var xhri = xhrRequest('html');	
	xhr[xhri].open('GET', '../varfiles/getflowercolours.aspx?cid='+cid, true);
	xhr[xhri].onreadystatechange = function() {
		if (xhr[xhri].readyState == 4 && xhr[xhri].status == 200) {
			//clear the dropdown list first 
			var arrDLs = xhr[xhri].responseText.split("$|$");
			var theDL = "FlowerColourDL";
			var selectbox = document.getElementById(theDL);
			for (var j = (selectbox.options.length-1); j > 0; j--){
				selectbox.options[j]=null;
			}
			
			var i = 0
			for(i=0;i<arrDLs.length-1;i++){
				var arrDL = arrDLs[i].split("||");
				var optn = document.createElement("OPTION");
				optn.text = "    " + arrDL[1];
				optn.value = arrDL[0];
				selectbox.options.add(optn);
			}			
			xi[xhri] = 1;
			xhr[xhri] = null;
		}
	};
	xhr[xhri].send(null);
}

function SetFlowerNameDL(cid){
	var xhri = xhrRequest('html');	
	xhr[xhri].open('GET', '../varfiles/getflowernames.aspx?cid='+cid, true);
	xhr[xhri].onreadystatechange = function() {
		if (xhr[xhri].readyState == 4 && xhr[xhri].status == 200) {
			//clear the dropdown list first 
			var arrDLs = xhr[xhri].responseText.split("$|$");
			var theDL = "FlowerNameDL";
			var selectbox = document.getElementById(theDL);
			for (var j = (selectbox.options.length-1); j > 0; j--){
				selectbox.options[j]=null;
			}
			var i = 0
			for(i=0;i<arrDLs.length-1;i++){
				var arrDL = arrDLs[i].split("||");
				var optn = document.createElement("OPTION");
				optn.text = "    " + arrDL[1];
				optn.value = arrDL[0];
				selectbox.options.add(optn);
			}			
			xi[xhri] = 1;
			xhr[xhri] = null;
		}
	};
	xhr[xhri].send(null);
}



function getHomeTabInfo(psid){
    fcn('../varfiles/gettabinfo.aspx?psid='+psid,'featureDiv');
}

function shownews(sid){
	setNavColor(sid);
    fcn('../varfiles/gethomenews.aspx?sid='+sid,'homeNews');
}

function getEventDetails(EventID){
	fcn('/varfiles/geteventdetails.aspx?EventID='+EventID+'&sid='+Math.random(),'EventHolder');
    window.location.hash ="#s"
}

function getGameDetails(GameID, TypeID){
	fcn('../varfiles/getgamedetails.aspx?GameID='+GameID+'&TypeID='+TypeID+'&sid='+Math.random(),'EventHolder');
}

function ShowEventheaders(eday, spanid) {
    fcn('../varfiles/getcalendarheaders.aspx?d=' + eday + '&sid=' + Math.random(), spanid);
}

function setEmailMessage(formName, emailboxid){
	var el, nids, eids, i
	nids = "";
	for (i=0; i<document.aspnetForm.elements.length; i++) { 
		el = document.aspnetForm.elements[i];
		if(el.type=="checkbox"){
			if (el.name.indexOf("CBLNews")!=-1){
				if (el.checked){
					if(nids==""){
						nids = el.value;
					}else{
						nids = nids + "," + el.value ;
					}
					//alert(el.checked + ":" + el.value);
				}
			} else if(el.name.indexOf("CBLEvents")!=-1){
				if (el.checked){
					eids += el.value +",";
					//alert(eids);
				}
			}
		}
	}
	//alert("nids: " + nids);
	//alert('../varfiles/getnewsevents.aspx?nids=' + nids + '&eids=' + eids + "&rnd=" + Math.random());
	fcn2('/varfiles/getnewsevents.aspx?nids=' + nids + '&eids=' + eids + "&rnd=" + Math.random(), emailboxid);
}

function removeFCKEditor(divID){
	
	if(typeof(FCKeditorAPI)!=='undefined'){
		var oFCKeditor = FCKeditorAPI.GetInstance(divID);
		oFCKeditor.disabled=true;
	}
	/*
	var oFCKeditor = new FCKeditor(divID);
	oFCKeditor.ToolbarSet = 'AdminAdvanced';
	oFCKeditor.ReplaceTextarea();*/
}

function setFCKEditor(divID){
	var oFCKeditor = new FCKeditor(divID);
	oFCKeditor.ToolbarSet = 'Members';
	oFCKeditor.ReplaceTextarea();
}


function HideEventheaders(){
	document.getElementById("calendarInfo").style.display = 'none';
	document.getElementById("calendarInfo").style.left = "-1000px";
}

function GP_popupConfirmMsg(msg) { //v1.0
  ///document.MM_returnValue = confirm(msg);
}
