function getMultipleSelect(mSelect){

    var delim       = "";
    var selParams   = "";

    for(j = 0; j < mSelect.options.length; j++) {
        if(mSelect.options[j].selected) {
            selParams += delim + mSelect.options[j].value;
            delim = ":";
        }
    }
    return selParams;
}

function clearSelect(selectElem){

    for(var count = selectElem.options.length - 1; count >= 0; count--)
    {
        selectElem.options[count] = null;
    }
}


//Browser Support Code
function populateCastes ( sourceReligion, sourceLanguage, targetId, form, targetFieldType ){


    var ajaxRequest;  // The variable that makes Ajax possible!

    try {
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){

        // Internet Explorer Browsers
        try {
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                return false;
            }
        }
    }


	// Add params to the url
    for(i=0; i < form.elements.length; i++) {
		if (form.elements[i].name == sourceReligion) {
			var	religion = form.elements[i].value;
		}

		// Get the castes based on language only for Hindu
		
		if (form.elements[i].name == sourceLanguage) {
			var language;
			var sel = form.elements[i];
			//alert (sel.multiple);
			if (sel.multiple) {
				var mSelOptions = getMultipleSelect (sel);
				language = mSelOptions;
   			}
			else {
			   	language = form.elements[i].value;
			}
		}

		if (form.elements[i].id == targetId) {
			var targetSelected;

			var sel = form.elements[i];
			if (sel.multiple) {
				var mSelOptions = getMultipleSelect (sel);
				targetSelected = mSelOptions;
            }
			else {
			    targetSelected = form.elements[i].value;
			}
		}
    }
	
	
	var targetElement = document.getElementById(targetId);

	// If Religion is Pick One, we do not need to go to the DB
	// Set the Caste to 'Pick One' and disable	
   	var DoAjaxDbCall = 'true';
	var castesHtml = '';
	if (religion == '') { // Pick One
		DoAjaxDbCall = 'false';
		castesHtml = ",Pick One,0";
		targetElement.disabled = true;
	}

	//alert (DoAjaxDbCall);
	// Field Type needed to be passed in since in case of Advanced Search, it is a MutiSelect

	if ( DoAjaxDbCall == 'true') { 
	
		var url = 'http://www.oriyamatrimonials.com/cgi-bin/matrim/getCastes.pl?Religion=' + religion + '&Language=' + language + '&FieldType=' + targetFieldType + '&TargetSelected=' + targetSelected;

    	ajaxRequest.open('GET', url, false);

    	ajaxRequest.send(null);


		castesHtml = ajaxRequest.responseText;	
		targetElement.disabled = false;
		
	}

    var selectElem = document.getElementById (targetId);

	clearSelect (selectElem);

	var casteOptions = castesHtml.split(":");
	var i;
	var len = casteOptions.length;

	for (i=0; i < len; i++ ) {

		var thisSet = casteOptions[i];
		var thisSetArr = thisSet.split (",");
		var optionElem = document.createElement("option");
		optionElem.value = thisSetArr[0];
		optionElem.text = thisSetArr[1];
		//alert(thisSetArr[1]);
		//alert(thisSetArr[2]);
		if (thisSetArr[2] == 1) {
			 optionElem.selected = true ;
		}
// With null works for Firefox, without null works for IE.  We have to research and see what works.  
// Find out and code accordingly. Also, what is the code at the end of the function doing? 
		//selectElem.add(optionElem, null); // this works in fire fox
		try {
			selectElem.add(optionElem, null); // standards compliant; doesn't work in IE
  		}
  		catch(ex) {
			selectElem.options.add(optionElem); // IE only
  		}

	}

	// Set the inner HTML.  If doing asynchronous, you will need to comment
    // this out.  IMPORTANT: in case of synchronous call the 'send' call appears
    // before the 'responseText' assignment
	if (targetFieldType == 'M') {
		targetElement.size = 3;
	}

	//var browser=navigator.appName;
    //if (browser == "Microsoft Internet Explorer") {
    //    select_innerHTML(targetElement, castesHtml);
    //}
    //else {
		//alert(ajaxRequest.responseText);
        //targetElement.innerHTML = castesHtml;
    //}	


	// WHAT THE HELL DOES THE CODE HERE DOES ???
    // Generate the change event on the target element, in case the target element
    // also has a callback with it.  If not, the event will be ignored
    //if (browser == "Microsoft Internet Explorer") {
     //   targetElement.fireEvent("onchange");
    //}
    //else {
     //   var e = document.createEvent('HTMLEvents');
      //  e.initEvent('change', false, false);
       // targetElement.dispatchEvent(e);
    //}
    return true;
}


