//////////////////////////////////////////////////////////////////////////////////////////////////////////
// INFOSOFT SA DE CV
// Date:    2006-07-13
// Updates: 2006-07-13
// Author:Rodolfo Machon
// File Name: qwsr.js
// Description:
//////////////////////////////////////////////////////////////////////////////////////////////////////////
var lan;
var appid;
var clase;
var gh;
var gv;
var tdheader;
var tdbody;
var bodyrows;
var xmlHttp;
var xmlHttpStream;
var xmlDoc;
var openwindow;
var openwindow1;
var TableConstructed=false;
var filters = "";
var openwindowPL = "";
function GetXmlHttpObject(handler){
	var objXmlHttp=null;	
	if(window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		objXmlHttp = new XMLHttpRequest();
		objXmlHttp.onload=handler;
		objXmlHttp.onerror=handler; 		
	}
	if(window.ActiveXObject){// code for IE6, IE5
		objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		objXmlHttp.onreadystatechange=handler;		
	}
	if(objXmlHttp===null){alert("Your Browser Does Not Suppport AJAX");}
	return objXmlHttp;
}
function GetClase(){
    GetParams();
    ConstructClase();
	if(typeof window.ProcessPage == 'function') {
		ProcessPage();
	}
}
function GetParams(){
    var query   = window.location.search.substring(1);
    var params	= query.split("&");
    var la      = params[0].split("=");
    var ap      = params[1].split("=");
    var cl      = params[2].split("=");
    lan		= la[1];
    appid	= ap[1];
    clase	= cl[1];
}
function ConstructClase(){
    var NewFilters 		= "";
    var FiltersValues	= "";
    var fields = filters.split(",");
    for (i=0;i<fields.length;i++){
		if(document.getElementById(fields[i])){
			var campo = document.getElementById(fields[i]).value;			
			NewFilters	+= fields[i]+",";
			FiltersValues	+= campo+"|";
		}
    }
	NewFilters = NewFilters.substr(0,NewFilters.length-1)
	FiltersValues = FiltersValues.substr(0,FiltersValues.length-1)	
    var URL     = "/cgi-bin/"+appid+"/bin/qwsp.pl?lan="+lan+"&appid="+appid+"&clase="+clase+"&filters="+NewFilters+"&filters_values="+FiltersValues;
	//alert(URL);
    xmlHttp	= GetXmlHttpObject(stateChanged);
    xmlHttp.open("GET",URL,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");		
    xmlHttp.send(null);
}
function stateChanged(){
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
    xmlHttpStream=xmlHttp.responseText;
	//document.write(xmlHttpStream);
	//alert(xmlHttpStream);
	var Data   = xmlHttpStream.split("^^");
		if(Data!=""){
			gh	     = Data[0];
			//alert(gh);
			gv 	     = Data[1];
			//alert(gv);
			fr	     = Data[2];
			//alert(fr);
			filters  = Data[3];
			//alert(filters);			
			filters_values  = Data[4];			
			//alert(filters);
			tdheader = Data[5];
			//alert(tdheader);
			tdbody   = Data[6];
			//alert(tdbody);
			bodyrows = Data[7];
			//alert(bodyrows);
			GetData();
		}
    } 
}
function GetData(){
    if(TableConstructed==false){
        ConstructTable();
        PopulateTable();
    }else{
        CleanTable();
        PopulateTable();
    }
}
function ConstructTable(){
	var grid_header		= gh.split("~~");
	var grid_body		= gv.split("~~");
	var T  				= document.getElementById('t1');
	var header_length	= (grid_header.length*1);
	var Cells 			= new Array(header_length);
	var fields			= new Array(header_length);	
	var columns_width	= new Array(header_length);	
	var element_type	= new Array(header_length);
	var element_properties	= new Array(header_length);	
	var NewRow		= T.insertRow(0);
	//alert(tdheader);
	NewRow.style.cssText 	= tdheader;
	//return;
	for (j=0;j<grid_header.length-1;j++){
		record				= grid_header[j].split("|");
		Cells[j]  			= NewRow.insertCell(j);
		Cells[j].innerHTML	= record[1];
		fields[j]			= record[0];
		columns_width[j]	= record[2];
		element_type[j]		= record[3];
		element_properties[j]	= record[4];		
	}
	var Rows = bodyrows;
	for (var i=0;i<Rows;i++){	
		k = (i*1)+1;
		NewRow	= T.insertRow(i+1);
		if(navigator.appName=='Microsoft Internet Explorer'){
			//NewRow.attachEvent('onmouseover',Hilite);
			//NewRow.attachEvent('onmouseout',Restore);		
		}else{
			NewRow.addEventListener("mouseover", Hilite, false);
			NewRow.addEventListener("mouseout", Restore, false);
		}	
		NewRow.style.cssText 	= tdbody;		
		for (j=0;j<grid_header.length-1;j++){
			var checked = "";
			if(element_type[j]=="text"){
				href	= "";
			}else if(element_type[j]=="input_text"){
				href  = "<input type=text name="+fields[j]+"__"+k+" id='"+fields[j]+"__"+k+"'";
				href += " size="+columns_width[j];
				href += " maxlength="+fields[j].substr(1,1);
				href += " "+element_properties[j];
				href += " class=my_style>";
			}else if(element_type[j]=="checkbox"){
			
			}
			Cells[j]  		= NewRow.insertCell(j);
			Cells[j].innerHTML	= href;
		}
	}
	TableConstructed=true;
}
function CleanTable(){
	var Rows 	= bodyrows;
	for (var i=1;i<=Rows;i++){
		GridClean(i);
	}	
}
function PopulateTable(){
	var gh1		 = gh.split("~~");
	var gv1		 = gv.split("~~");
	for (var i=1;i<gv1.length;i++){
		var values = gv1[i-1].split("|");		
		for (var j=0;j<values.length-1;j++){
			var fields = gh1[j].split("|");
			eval("document.form."+fields[0]+"__"+i+".value ='"+values[j]+"'");		
		}
	}	
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// Auxiliar Sub-Routines
function GridOpenWindow(objRef,fldRef,sfield){
	var id_value	= "";
	var component	= "";
	var clase1		= "";
	var getback		= "";
	var opener_form	= "";
	var left		= "";
	var top			= "";
	var svalue		= "";
	var id_field	= objRef.name;
	var objIndx  	= objRef.name.split("__");
	var references  = fr.split("~~");
	for (var i=0;i<references.length;i++){
		var reference = references[i].split("|");
		if(fldRef==reference[0]){
			var component	= reference[1];
			var clase1		= reference[2];
			var getback		= reference[3];
			var opener_form	= reference[4];
			var left		= reference[5];
			var top			= reference[6];
		}
	}
	if(id_field!==""){
		id_value = eval("document.form."+id_field+".value");
		if(id_value===""){
			alert('Please Choose ID Firts');
			return;
		}
	}
	if(sfield!==""){
		var arr  = sfield.split(",");
		for (var j=0;j<arr.length;j++){
			if(eval("document.form."+arr[j]+"__"+objIndx[1])){
				svalue += eval("document.form."+arr[j]+"__"+objIndx[1]+".value")+"|";				
			}
		}
	}
	if (!openwindow || openwindow.closed){
		win ="window.open('/cgi-bin/"+appid+"/bin/"+component+".pl?lan="+lan+"&appid="+appid+"&clase="+clase1+"&opener_form="+opener_form+"&id="+id_value+"&search_fields="+sfield+"&search_values="+svalue+"','"+component+clase1+"','left="+left+",top="+top+",width=100,height=100,scrollbars=no,toolbar=no,menu=no')";
		openwindow=eval(win);
	}else{openwindow.focus()}	
}
function GridOpenWindowPl(objRef,fldRef){
	var id_value	= "";
	var id_field	= objRef.name;
	var objIndx  	= objRef.name.split("__");
	var references  = fr.split("~~");
	for (i=0;i<references.length;i++){
		var reference = references[i].split("|");
		if(fldRef==reference[0]){
			var component	= reference[1];
			var clase1	= reference[2];
			var getback	= reference[3];
			var opener_form	= reference[4];
			var left	= reference[5];
			var top		= reference[6];
		}
	}
	if(id_field!=""){
		id_value = eval("document.form."+id_field+".value");
		if(id_value==""){
			alert('Please Choose ID Firts');
			return;
		}
	}
	if (!openwindow || openwindow.closed){
		win ="window.open('/cgi-bin/"+appid+"/pl/"+component+".pl?lan="+lan+"&appid="+appid+"&clase="+clase1+"&opener_form="+opener_form+"&id="+id_value+"','"+component+clase1+"','left="+left+",top="+top+",width=100,height=100,scrollbars=no,toolbar=no,menu=no,resizable=yes')";
		openwindow=eval(win);
	}else{openwindow.focus()}	
}
function GridOpenWindowPL(objRef,appid,id,name,paramswin){
    paramswin="left=100,top=100,width=600,height=400,scrollbars=yes,toolbar=no,menu=no,resizable=yes";
	var id_value	= "";
	var id_field	= objRef.name;
	if(id_field!=""){
		id_value = eval("parent.form.document.form."+id_field+".value");
		if(id_value==""){
			alert('Please Choose ID Firts');
			return;
		}
	}
	if (!openwindowPL || openwindowPL.closed){
	    win ="window.open('/cgi-bin/"+appid+"/pl/"+name+".pl?lan="+lan+"&appid="+appid+"&id="+id_value+"','"+name+"','"+paramswin+"')";
	    openwindowPL=eval(win);
    }else{
        openwindowPL.focus;
    }	
}
function GridClean(indx){
	var grid_header	= gh.split("~~");
	for (var i=0;i<grid_header.length-1;i++){
		var record = grid_header[i].split("|");
		eval("document.form."+record[0]+"__"+indx+".value=''");
	}
}
function Hilite(){
	var tname=event.srcElement.tagName;
	var eName = event.srcElement;
	var trName=eName.parentElement;
	eName.style.backgroundColor = "#D5D5D5";	
	trName.style.backgroundColor = "#D5D5D5";
	eName.style.cursor = "hand";
}
function Restore(Ref){
	var tname=event.srcElement.tagName;	
	var eName = event.srcElement;
	var trName=eName.parentElement;
	eName.style.backgroundColor = "#FFFFFF";
	trName.style.backgroundColor = "#FFFFFF";
	eName.style.cursor = "";	
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////