var timerID;

function preLoadCastes() {
	var cboReligion = document.getElementById("Religion");
	var cboCaste    = document.getElementById("Caste");
	
	// If no Religion was selected, leave religion and caste inputs empty
	if (cboReligion==null || cboReligion.value < 0) {
		return;
	}
	
	if (!isCastComboPopulated()) {
		populateCaste(cboReligion);
	}
	else if (wasCasteSelected()) {
		maintainCasteState();
	}
}

function isCastComboPopulated() {
	var oCaste 		= document.getElementById("Caste");
	var isPopulated = (oCaste!=null && oCaste.options.length>1);
	return isPopulated;
}

function wasCasteSelected() {
	var wasSelected = (iSelectedCaste!=null && iSelectedCaste!="null" 
					&& iSelectedCaste!=""   && iSelectedCaste!="-1" 
					&& iSelectedCaste!="Not Specified");
	return wasSelected;
}

// If Caste was selected set the selected caste
function maintainCasteState() {
	var oCaste = document.getElementById("Caste");
	
	if (oCaste.options.length<2) {
		return;
	}
	
	for (var i=0; i<oCaste.options.length; i++) {
		if (oCaste.options[i].value == iSelectedCaste) {
			oCaste.selectedIndex = i;
			break;
		}
	}
	
	clearTimeout(timerID);
}

function populateCaste(oReligion) {
    toogleCursor(true);
    
    var sUrl = "/servlet/AJAX_Castes?religion="+oReligion.value;
    loadXMLDoc(sUrl, getCastes, true);
    
    toogleCursor(false);
}

function getCastes() {
	// only if xmlRequest shows "loaded"
	if (xmlRequest.readyState == 4) {
		// only if "OK"
		if (xmlRequest.status == 200) {
			//alert(xmlRequest.responseTEXT);
			deserialiseCastes(xmlRequest.responseXML);
		}
		else {
			alert("There was a problem retrieving the XML data:\n" + xmlRequest.statusText);
		}
	}
}

// <castes>
//	 <c>
//	   <txt>Khatri</txt>
//	   <idx>12</idx>
//	 </c>
//   ...
//   <selectedCaste>8</selectedCaste>
// </castes>
function deserialiseCastes(responseXML) {
	var sParentCastes = responseXML.getElementsByTagName("castes")[0];
	var sAllCastes	  = sParentCastes.getElementsByTagName("c");
    var iLen      	  = sParentCastes.childNodes.length-1; // Ignore SelectedCaste
	var cboCaste  	  = document.getElementById("Caste");
    var iOp			  = 1;
    
    cboCaste.options.length = iLen+1;
	cboCaste.options[0] 	= new Option("", -1);
	
    for (var i=0; i<iLen; i++, iOp++) {
    	var sVal = sAllCastes[i].getElementsByTagName("i")[0].firstChild.data;
    	var sTxt = sAllCastes[i].getElementsByTagName("txt")[0].firstChild.data;
    	
        cboCaste.options[iOp].value= sVal;
        cboCaste.options[iOp].text = sTxt;
        //cboCity.options[i] = new Option(sCityName, i);
    }
	
	// Default selected index to blank. This will be changed on page load by maintainCasteState() above
	cboCaste.selectedIndex = 0;
		
	xmlSelectedCastes = sParentCastes.getElementsByTagName("selectedCaste");
	if (xmlSelectedCastes!=null && xmlSelectedCastes.length>0) {
		iSelectedCaste = xmlSelectedCastes[0].firstChild.data;
		maintainCasteState();
	}
	else if (wasCasteSelected()) {
		maintainCasteState();
	}
}

function toogleCursor(showMsg) {
	if (showMsg) {
		document.body.style.cursor = 'wait';
	}
	else {
		document.body.style.cursor = 'default'; //auto
	}
}
