// JScript File

var strPath = '/content/upload/AssetMgmt/inDirectoryPhysicianSearchXML/';
var tag;
var AjaxServerPageName = strPath + 'Insurances.xml' ;
    

function AjaxCaller(ctrl, tag, pdir) {

	var obj = document.getElementById(ctrl);
	removeAllOptions(obj);

	if (tag =='Insurance') {AjaxServerPageName = strPath + 'Insurances.xml' ;}
	if (tag =='language') {AjaxServerPageName = strPath + 'Languages.xml';}
	if (tag =='Specialty' && (pdir!=null)) {AjaxServerPageName = strPath + 'Specialty.xml';}

	var requestUrl = AjaxServerPageName;

	var ajax = new AjaxDelegate(requestUrl, CreateList, ctrl, tag);
	ajax.Fetch();
}


function CreateList(url, response, ctrl, tag) {

	var data = new Array();
	data =	response ;
	//tag = 'Insurance';

	var obj = document.getElementById(ctrl);
	//removeAllOptions(obj);
	loadXMLToDropdown(data, obj, tag) 
}


function removeAllOptions(from) { 
	//remove all options
	var max = from.options.length
	for (var i=0; i<max; i++) {
		from.options[0] = null; 
	}
	from.selectedIndex = -1; 
} 

function loadXMLToDropdown(xml, dd, field) {
        var xmldoc, parser;
        var Elem;
        var destination="", addtext, addvalue;

        try{		// Internet Explorer
		xmldoc = new ActiveXObject("Microsoft.XMLDOM") 
		xmldoc.async = false 
		xmldoc.loadXML(xml)         

		Elem = xmldoc.getElementsByTagName(field) ; 
		Provlength = Elem.length; 
            
		for(i=0; i < Provlength; i++) 
		{ 
		    destination = Elem.item(i).text; 		    
		    addtext = destination; 
		    
		    if (destination == "All") {destination=""}
		    if (destination == "Choose an Insurance") {destination = ""}
		    if (destination == "General Pediatrics") {destination = "*Pediatrics"}
		    if (destination == "Choose a Language") {destination = ""}
		    
		    addvalue = destination; 
		    
		    dd.options[i] = new Option(addtext,addvalue);                  
		}            
            
        }catch(e){	//Firefox, Mozilla, Opera, etc.                
            	parser = new DOMParser();
            	xmldoc = parser.parseFromString(xml,"text/xml");

		Elem = xmldoc.getElementsByTagName(field) ; 
        	Provlength = Elem.length;                        
        
		for(i=0; i < Provlength; i++) 
		{ 
		    destination = Elem[i].childNodes[0].nodeValue; 
		    addtext = destination; 
		    
		    if (destination == "All") {destination=""}
		    if (destination == "Choose an Insurance") {destination = ""}
		    if (destination == "General Pediatrics") {destination = "*Pediatrics"}
		    if (destination == "Choose a Language") {destination = ""}

		    addvalue = destination; 

		    dd.options[i] = new Option(addtext, addvalue);                  
		}
        	
        }

                              
    } 


function setLocationList(val){
	//var obj = document.getElementById('dlSpecialty');
	//removeAllOptions(obj);
	//Cookie NAME: TypeOfDoctor
	
	var ctrlSpecialty = 'dlSpecialty';

	if (val =='Specialist'){
		setCookie('TypeOfDoctor','Specialist',7)
		AjaxServerPageName = strPath + 'Specialty.xml';
	}else{
		setCookie('TypeOfDoctor','GeneralPediatrician',7)
		AjaxServerPageName = strPath + 'GeneralPediatrics.xml';
	}
	AjaxCaller(ctrlSpecialty,'Specialty');
}


var ctrlConst='inCtx12cboDirElem';

function GenderClicked(val){
	//alert(rdoObj);
	document.getElementById(ctrlConst+"1").value = val;
}


var strAgeRange = '';
var Age1 = 25, Age2 = 99;

function GetAgeRange(){

	var error = '';
	strAgeRange = '';
	Age1 = 25, Age2 = 99;

	/*************** CODE COMMENTED FOR AGE RANGE ******
	try{        
	    //if(obj.id=='AGE1'){
		Age1 = document.getElementById('AGE1').value;
		if (Age1 ==''){Age1=25;}
		if (isNaN(Age1) == false){ Age1 = parseInt(Age1)  } 
		else { 
			Age1=25;
			error = 'Please enter valid age range.';
		}
	    //}else if(obj.id=='AGE2'){
		Age2 = document.getElementById('AGE2').value;
		if (Age2 ==''){Age2=99;}
		if (isNaN(Age2) == false){ Age2 = parseInt(Age2)  } 
		else { 
			Age2=99; 
			error = 'Please enter valid age range.';
		}	
	    //}    


	    if (error == '' && Age1 !='' && Age2 != '' && Age2 >= Age1 ){
		var max = Age2 - Age1;
		for (var i=0; i<=max; i++){
			//alert(Age1 + i);
			if (strAgeRange !=''){strAgeRange = strAgeRange + ','}
			strAgeRange = strAgeRange + (Age1 + i);
		}
		///////////////document.getElementById(ctrlConst+'4').value = strAgeRange;

	    }else{
		alert(error);
		//return false;
	    }
	    
	}catch(e){
		var j;
	}
	*************************************************/

	try{ 

		var ind;

		//Set Specialty
		ind = document.getElementById('dlSpecialty').selectedIndex;
		document.getElementById(ctrlConst+'2').value = trim(document.getElementById('dlSpecialty').options[ind].value);

		//Set Insurance
		ind = document.getElementById('dlInsurance').selectedIndex;
		document.getElementById(ctrlConst+'3').value = trim(document.getElementById('dlInsurance').options[ind].value);

		//Set Language
		ind = document.getElementById('dlLanguages').selectedIndex;
		document.getElementById(ctrlConst+'5').value = trim(document.getElementById('dlLanguages').options[ind].value);


		//alert(document.getElementById(ctrlConst+'1').value);
		//alert(document.getElementById(ctrlConst+'2').value);
		//alert(document.getElementById(ctrlConst+'3').value);
		//alert(document.getElementById(ctrlConst+'5').value);
		//alert(strAgeRange);
		//if(document.getElementById(ctrlConst+'6').value != ''){
		//	alert(document.getElementById(ctrlConst+'6').value);
		//}
		
    	}catch(e){
    		var k;
	}
	return true;
}



function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

// example of using trim, ltrim, and rtrim
//var myString = " hello my name is ";
//alert("*"+trim(myString)+"*");
//alert("*"+ltrim(myString)+"*");
//alert("*"+rtrim(myString)+"*");



// ############# GET/SET Cookie #################

function getCookie(c_name) {
	if (document.cookie.length>0) {
  	c_start=document.cookie.indexOf(c_name + "=");
  	if (c_start!=-1) { 
		c_start=c_start + c_name.length+1; 
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
    	} 
  	}
	return "";
}

function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString());
}