/*
start with queryHTTP() function to understand flow.
*/
/**
 * 
 * This file will produce popup ajax search.
 * @file ajaxSearch.js
 * @author Uday Shiwakoti < uday@uday.com.np>
 * @version 1.0
 * @date 23rd, may 2006
 */
var searchBox,targetBox,responseTxt,fldName;
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}



function setOption(optionName,valArray){ 
	var length = valArray.length;
	var i;
	
	 document.getElementById(optionName).options.length = length;	
	  for(i=0;i<length;i++){
	  	tmpArray=valArray[i].split('~');		
	  	 document.getElementById(optionName).options[i].text=tmpArray[1];
		 document.getElementById(optionName).options[i].value=	tmpArray[0];
	  }	

		
}


function handleFormField() {
  if (http.readyState == 4) {
    // Split the comma delimited response into an array
	//alert(http.responseText);
    results = http.responseText.split(",");
	//alert(results);
//alert(results);
		for(i=0;i<results.length;i++){

		val=results[i].split(":");
		//alert(val);
		/*
		val[0] stores field name.
		val[1] stores field value.
		*/

		if(document.getElementById(val[0])){
				
				valSelect=val[1].split("|");
		//alert(valSelect);
				if(document.getElementById(val[0]).type.indexOf("select") != -1){
					 setOption(val[0],valSelect);
				}else if(valSelect.length==1){
						document.getElementById(val[0]).value=val[1];
				}
			}
		
		}
	
  }
  
  
}


function fillDbContent() {

  if (http.readyState == 4) {
			result = http.responseText
			document.getElementById("dbContent").innerHTML=result;
	
	 }
}

function searchBox(boxName,searchScript,defaultValue){
	if (searchBox.arguments.length == 2)
		defaultValue='';
		var filter=/^.+\?.+.$/
		if(filter.test(searchScript))
			getConcat="&";
		else
			getConcat="?";
		

	output="<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" ><tr><td>";
		output+="<input type=\"text\" name=\""+boxName+"\"  id=\""+boxName+"\" onKeyUp=\"queryHTTP('"+searchScript+getConcat+"id='+this.value,'listSearchContent');\" value=\""+defaultValue+"\">";
	output+="</td></tr>";
	output+="<tr><td>";
		output+="<div id=\""+boxName+"_div\" style=\"position:absolute;\"></div>";
	output+="</td><//tr></table>";
	searchBox=boxName;
	document.write(output);
}

function listSearchContent() {
  if (http.readyState == 4) {
    // Split the comma delimited response into an array
	//    results = http.responseText.split(",");
	results = http.responseText;
	output='';
	results = http.responseText.split("|");
	if(http.responseText!=""){
	output="<table border=\"0\" cellpadding=\"1\" cellspacing=\"0\" bgcolor=\"#6E6E6E\" width=\"144\"><tr><td>";
	output+="<table  style=\"background-color:#FFFFFF;font-family: Arial, Helvetica, sans-serif;font-size: 11px; cursor: default;\" width=\"144\">";
		for(i=0;i<results.length;i++){
			val=results[i].split(":");
			output+="<tr  onMouseOver=\"this.style.backgroundColor='#CCCCCC'\" onMouseOut=\"this.style.backgroundColor='#FFFFFF'\" >";
			output+="<td>&nbsp;&nbsp;<a href=\"#\" onClick=\"setVal('"+val[0]+"','"+val[1]+"')\">"+val[1]+"</a>&nbsp;&nbsp;</td></tr>";
		}
	output+="</table></td></tr></table>";
	}
	document.getElementById(searchBox+"_div").innerHTML=output;

  }
}

function setVal(val_cd,val_desc){
	document.getElementById(searchBox).value=val_cd;
	document.getElementById(searchBox+"_div").innerHTML='';
	
}

//Function for ajax Nepali TO English Date Convestion and setting up of calendar
function nepToEngDate(){
	 if (http.readyState == 4) {    
//	 alert(http.responseText);
		result = http.responseText.split("/");
		SetInputDate('testDate', result[2], result[1], result[0]);
				
	} 
}

//responseText
function getResponseTextPop(){
	if (http.readyState == 4) {
		opener.document.getElementById("dbContent").innerHTML=http.responseText;
		
		self.close();
//		responseTxt = http.responseText;				
	} 

	
}

function getResponseText(){
	if (http.readyState == 4) {
		//alert(http.responseText);
		document.getElementById("dbContent").innerHTML=http.responseText;							
	} 
}

function setSelect() {
	var i;
	var val;
	  if (http.readyState == 4) {
		// Split the comma delimited response into an array
		results = unescape(http.responseText);
		field = results.split("``");
		for (i = 0; i < field.length; i++) {
			results = field[i];
			val= results.split(":");
			valSelect=val[1].split("|");
			setOption(val[0],valSelect);
			
		}
	  }  
  
}

/***************************************************************************/
	function setDivValue(){
	  if (http.readyState == 4) {
				result = unescape(http.responseText);
//				alert(result);

		document.getElementById(fldName).innerHTML=result;
		
		 }
	}

	function setFieldValue(){
	  if (http.readyState == 4) {
				result = http.responseText
				document.getElementById(fldName).value=result;
		
		 }
	}
	
	
	

	function setSelectValue() {
	  if (http.readyState == 4) {
		// Split the comma delimited response into an array
		results = http.responseText;
			result = http.responseText
			valSelect=results.split("|");
			setOption(fldName,valSelect);
			
	  }  
  	}

/***************************************************************************/

function queryHTTP(url,whichFunction,fieldName) {
/*
http is an object of getHTTPObject which is created just below
*/

 	if (queryHTTP.arguments.length == 3){
 			fldName=fieldName;
	 }
	 
  http.open("GET", url , true);
		if(whichFunction=='handleFormField')
			  http.onreadystatechange = handleFormField; 
		else if(whichFunction=='setSelect')
			  http.onreadystatechange = setSelect; 
		 else if(whichFunction=='fillDbContent')
			  http.onreadystatechange = fillDbContent; 
		else if(whichFunction=='listSearchContent')
			  http.onreadystatechange = listSearchContent; 
		else if(whichFunction=='nepToEngDate')
			  http.onreadystatechange = nepToEngDate;
		else if(whichFunction=='getResponseTextPop')
			  http.onreadystatechange = getResponseTextPop;
		else if(whichFunction=='getResponseText')
			  http.onreadystatechange = getResponseText;		
	
	
		else if(whichFunction=='setDivValue')
			  http.onreadystatechange = setDivValue;
		else if(whichFunction=='setFieldValue')
			  http.onreadystatechange = setFieldValue;
		else if(whichFunction=='setSelectValue')
			  http.onreadystatechange = setSelectValue;

	  
	 


  http.send(null);
}

var http = getHTTPObject(); // We create the HTTP Object
