﻿
function createXmlHttpRequest(){
	var xmlhttp=null;
	if(window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest();
	}else if(window.ActiveXObject){
		try{
			xmlhttp= new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){try{
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){}
		}
	} 
	return xmlhttp;	
}
function makeGetRequest(xmlhttp,url, fnToBeCalled,async){

	if (xmlhttp==null){alert('Sorry, not able to create XMLHttpRequest object');return;}
	 
	xmlhttp.onreadystatechange=fnToBeCalled;
	
	xmlhttp.open('GET',url,async);
	
    xmlhttp.send(null);
   
}
function makePostRequest(xmlhttp,url, fnToBeCalled,value,async){
	if (xmlhttp==null){alert('Sorry, not able to create XMLHttpRequest object');return;}
	xmlhttp.onreadystatechange=fnToBeCalled;
	xmlhttp.open('POST',url,async);
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlhttp.send(value);
}
//function used to fill a dropdown place inside a div with string options
function fillCombo(str,ddl,div){
    ddl.innerHTML='';
    var b=div.innerHTML;
    b=b.substring(0,b.length-9);
    b=b + str + "</select>"
    div.innerHTML=b;
}
//function used to fill a dropdown with options from character separated string.
function populateCombo(ctl,strText,strValue,sep){
	var text=new Array(),value=new Array();
	text=strText.split(sep); value =strValue.split(sep);
	while(ctl.length>0){ctl.remove(0);}
	for(i=0;i<text.length;i++) ctl.options[i]=new Option(text[i].toString() ,value[i].toString());
}
//function to call dropdown fill page
var ddl_xmlhttp;
var ddl_Target;
function ddl_AjaxCall(ddlSource,ddlTarget,functionName){
    ddl_Target = ddlTarget;
    ddl_xmlhttp = createXmlHttpRequest();
    var urltocall = 'AjaxPage.aspx?combo=' + functionName + '&value=' + ddlSource.value;
    makeGetRequest(ddl_xmlhttp,urltocall,ddl_ProcessResponse,false);
}
function ddl_ProcessResponse(){
    if ( ddl_xmlhttp.readyState==4){
       if ( ddl_xmlhttp.status != 200 ){
            Alert('Error in communication');
            return;
       }
       //if session timed out then redirect to login screen
       var response=ddl_xmlhttp.responseXML.documentElement;
       if(response==null){document.location.href=ddl_xmlhttp.responseText;response=null;ddl_xmlhttp=null;return;}
	    
	   var text = response.getElementsByTagName("text")[0].firstChild.nodeValue;
	   var value = response.getElementsByTagName("value")[0].firstChild.nodeValue;
       
       populateCombo(ddl_Target, text,value, '~');
       ddl_xmlhttp = null;     
       ddl_Target = null;    
    }
}


function FillList(availableNode,oSelField)
{
            //clear the listbox values
            var leng=oSelField.options.length;
            for (i=0; i <leng; i++) {
               oSelField.options.remove(0);
            }
                     
            //add values to the listbox
             for(var index=0; index < availableNode.childNodes.length; index ++){
           oSelField.options[index]=new Option(availableNode.childNodes[index].childNodes[0].text ,availableNode.childNodes[index].childNodes[0].text);
            
            }
}
