﻿/*****************************************************
	Ajax Support Scripting.	
	Sears Roebuck de México.
	Sears Internet.

Version 1.1  Ene/2007
*******************************************************/

/*--------------------------------------
	 Creates a simple Pair Object
------------------------------------*/
function Pair(key,val){
		this.key=key;
		this.value=val;	
	}

/*---------------------------------------
	Calls a function in a threaded-alike way
----------------------------------------*/

function callAsyn(func){
	setTimeout(func,300);
}

/*********************************************
	Sort Order for Pair Objects based on the key
*********************************************/	
	function orderByKey(a,b){
		if(  (a.key.replace("$","").replace(",","").search("^\\d+(\\.\\d+)?$") == -1) || ((b.key.replace("$","").replace(",","").search("^\\d+(\\.\\d+)?$") == -1))    ){
			 if (a.key==b.key) return 0;	
			 return (a.key<b.key)?-1:1;
		}else
		{		
				return parseFloat(a.key)- parseFloat(b.key)
		}
	}

/*********************************************
	Sort Order for Pair Objects based on the value
*********************************************/	
	function orderByValue(a,b){
	
		if(a.value==null)return 0;
		if(  (a.value.replace("$","").replace(",","").search("^\\d+(\\.\\d+)?$") == -1) || ((b.value.replace("$","").replace(",","").search("^\\d+(\\.\\d+)?$") == -1))    ){
			 if (a.value==b.value) return 0;	
			 return (a.value<=b.value)?-1:1;
		}else
		{		
				return parseFloat(a.value.replace("$","").replace(",",""))- parseFloat(b.value.replace("$","").replace(",",""))
		}
	}
	

/****************************************************
Get's a HttpRequest Object 
*****************************************************/
function getRequester(){
	var req;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();			
        } catch(e) {
			if (e != null)req = null;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = null;
        	}
		}
    }
 return req;
}

/*****************************************************************
	Loads in a Synchronous way the given URL (including Parameters)
	with the given request Object, returning a DOM object
********************************************************************/
function executeXML(url,req,postContent,mimeType) {
	if(req!=null) {		
		req.open("POST", url, false);
		if(postContent==null || postContent=='undefined'){
			postContent="";}
		else{
			if(mimeType==null|| mimeType=="undefined")
				req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');			
			else
				req.setRequestHeader('Content-Type', mimeType);			
		}		
		req.send(postContent);

		return req.responseXML;
	}
	return null;
}

/*****************************************************************
	Loads in a ASynchronous way the given URL (including Parameters)
	with the given request Object, returning a DOM object
********************************************************************/
function beginExecuteXML(url,req,postContent,fn,optParams,timeOut,mimeType){
	if(req!=null) {		
		
		//Set the Asu
		req.onreadystatechange=function(){
		    // only if req shows "loaded"
		    if (req.readyState == 4) {
		        // only if "OK"
		        if (req.status == 200) {
				   //Execute the function
				  fn(req.responseXML,optParams);
			        } else {
					
				   fn(null,optParams);
			        }
		    }

		}
		req.open("POST", url, true);

		if(postContent==null || postContent=='undefined'){
			postContent="";}
		else{
						if(mimeType==null|| mimeType=="undefined")
				req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');			
			else
				req.setRequestHeader('Content-Type', mimeType);			

		}		

		req.send(postContent);
		
		
	}
	return null;

}




/**************************************************************
	Execute a request that is expecting a scalar result
**************************************************************/
function executeScalar(url,req,postContent,mimeType){
	if(req!=null) {
		req.open("POST", url, false);
		if(postContent==null || postContent=='undefined'){
				postContent="";}
		else{
			if(mimeType==null|| mimeType=="undefined")
				req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');			
			else
			 
				req.setRequestHeader('Content-Type', mimeType);			
		}

		req.send(postContent);
		return req.responseText;
	}
	return null;
}



/**************************************************************
	Execute a request that is expecting a scalar result
	in an asynchronous fashion
**************************************************************/
function beginExecuteScalar(url,req,postContent,funcallback,optParams,timeOut,mimeType){
	if(req!=null) {		
		
		//Set the Asu
		req.onreadystatechange=function(){

		    // only if req shows "loaded"
		    if (req.readyState == 4) {
		        // only if "OK"

		        if (req.status == 200) {
		        	        
							//Execute the function
							funcallback(req.responseText,optParams);
			        } else {					
							funcallback(null,optParams);
			        }
		    }

		}

		req.open("POST", url, true);

		if(postContent==null || postContent=='undefined'){
			postContent="";}
		else{
			if(mimeType==null|| mimeType=="undefined")
				req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');			
			else
				req.setRequestHeader('Content-Type', mimeType);			
		}		
 
		req.send(postContent);
 
	}
	return null;

}

/*************************************************************
	Databinds a DropDown (object) with the given array of nodes.
	Using the  "v" as value and the key "t" as the next .
	If v is null, the index is used
	
	'New.. UseElements is Used to force to use Child Elements
	instead of Attributrs
**************************************************************/
function dataBindDropDown(drop,nod,v,t,useElements){
		var it;
		var opt;
		var val;
		clearDropDown(drop);
		//Databind
		for (i=0;i<nod.length;i++){
			it=nod[i];
			if (it.nodeType==1){
					if(useElements){
							val=(v==null)?i:getNodeValue(it.getElementsByTagName(v)[0]);
							opt=  new Option(getNodeValue(it.getElementsByTagName(t)[0]),val);									
					}else{
							val=(v==null)?i:it.attributes.getNamedItem(v).value;
							opt=  new Option(it.attributes.getNamedItem(t).value,val);					
					}
				
				drop[drop.options.length]=opt;
			}
		}		
} 

/*************************************************************
	 	Get the content of a node in different Platforms
***************************************************************/
function getNodeValue(node){
	var res;
	res=node.text;

	

	if (res==null || res=="undefined") res=node.textContent;

	if (res==null || res=="undefined") res=node.nodeValue;

	if (res==null || res=="undefined") {

		for(i=0; i<node.childNodes.length;i++) if(node.childNodes[i].nodeType==3){

		res=node.childNodes[i].nodeValue;
		}

	}

	return res;
}
/***************************************************
 Returns the First Node with the given Type. Usefull
 when dealing with mozilla browsers.
****************************************************/
function getFirstNodeXType(nodes,type){
	for(i=0;i<nodes.length;i++){
		if (nodes[i].nodeType==type)return nodes[i];
	}
	return null;
}

/**************************************************************
	Clears Items from a DropDown
****************************************************************/
function clearDropDown(drop){
		if (drop==null)return;
		//Clean Previous
		while(drop.options.length>0)drop.options[0]=null;
}
/***********************************************************
Get current X Position
*****************************************************************/
function getXScroll(){
	if (window.pageXOffset!=null &&  window.pageXOffset!="undefined") return window.pageXOffset;
if (document.body.scrollLeft!=null && document.body.scrollLeft!="undefined") return document.body.scrollLeft;
	return 0;
}

/***********************************************************
Get current Y Position
*****************************************************************/
function getYScroll(){
	if (window.pageYOffset!=null &&  window.pageYOffset!="undefined") return window.pageYOffset;
		if (document.body.scrollTop!=null && document.body.scrollTop!="undefined") return document.body.scrollTop;
	
	return 0;
}


/**************************************************************
	Show a "Working Layer"
***************************************************************/

function showIDLE(callback,myPnl){
	var mydiv;
	var pnlOk;
	var myid;
	myid="IDLE_SEARS_LAYER";
	if (document.getElementById(myid)==null){

		mydiv=document.createElement("div");
		mydiv.id=myid;
		document.body.insertBefore(mydiv,null);
		mydiv.style.width=screen.availWidth+getXScroll() + "px";
		mydiv.style.height=screen.availHeight+getYScroll()+ "px";
		mydiv.style.left="-10px";
		mydiv.style.top="-20px";
		mydiv.style.position="absolute";
		mydiv.style.display="block"
		mydiv.style.zIndex=500;
		mydiv.innerHTML="<table width='100%' height='100%' align='center' valign='center'  style='cursor:wait;background-color:black;filter:alpha(opacity=50);opacity:0.5;z-index:501'><tr><td></td></tr></table>"	
		if (myPnl==null || myPnl=='undefined'){
				pnlOk=getWaitingPanel(callback);
		}
		else
		pnlOk=myPnl;				
		mydiv.insertBefore(pnlOk,null);
	

	}
}


/******************************************************
	Creates a Waiting Div to show for IDLE
*******************************************************/
function getWaitingPanel(callback){

	var pnlOk,table,th,td,tp,pb,interval;

				pnlOk=document.createElement("div");
//Anexion
	pnlOk.id="HelperWaitingPanel";				
				table=document.createElement("table");
				table.cellpadding=0;
				table.cellspacing=0;
				pb=document.createElement("table");
				pb.insertRow(-1);
				//Set Progress Bar
				for(i=0;i<20;i++){
						pb.rows[0].insertCell(-1).appendChild(document.createTextNode(' '));
						pb.rows[0].cells[i].className=(i%2==0)?"color02":"TituloCelda";
						pb.rows[0].cells[i].style.height="20";
					}
				
				/*var img;
				img=document.createElement("img");
				img.src='/SearsSite/loading.gif';
				pb.rows[0].insertCell(-1).insertBefore(img,null);*/
				
				pb.style.width="100%";
				//Alternate
				/*interval=setInterval(function(){

							for(j=0;j<pb.rows[0].cells.length;j++){
								
								pb.rows[0].cells[j].className=(pb.rows[0].cells[j].className=='color02')?"TituloCelda":"color02";
							}
							if(getIDLELayer()==null){
								clearInterval(interval);
							}
						},100);*/
				th=table.insertRow(-1).insertCell(-1);				
				th.appendChild(document.createTextNode(':: Procesando  ::'));
				th.className="TituloCelda"
				th.align="center"
				
				td=table.insertRow(-1).insertCell(-1);
				td.appendChild(document.createTextNode('Si desea dejar de esperar haga click en este cuadro.'));
				td.align="center"
				td.className="gris"														
				
				tp=table.insertRow(-1).insertCell(-1);
				tp.align="center"				
				tp.insertBefore(pb,null);
			
				
				pnlOk.style.width=200;
				pnlOk.style.height=200;
				pnlOk.style.left=(screen.availWidth/2)-100+getXScroll();
				pnlOk.style.top=(screen.availHeight/2)-100+getYScroll();
				pnlOk.style.position="absolute";			
				if(callback!="undefined" && callback!=null){
					pnlOk.onclick=callback;			
					table.onclick=pnlOk.onclick;			
					}
				pnlOk.insertBefore(table,null);

	return pnlOk;
}
/********************************

Returns the IDLE Layer
*********************************/
function getIDLELayer(){
	var myid;
	myid="IDLE_SEARS_LAYER";
	return document.getElementById(myid); 
}
/****************************
	hideIDLE() layer
******************************/
function hideIDLE(idRelease){
	if (getIDLELayer()!=null){
		
		var tlayer=getIDLELayer();

 
		if(idRelease!=null) {
			/*Releases a given Panel passed previously to showIDLE()
			 Otherwise it will be removed as well from the document*/

			var elem=document.getElementById(idRelease);
			tlayer.removeChild(elem);
			document.body.insertBefore(elem,null);
			hideElement(idRelease);
		}

		document.body.removeChild(tlayer);

	}
}

/*****************************************
	Builds a data table based on the given XML
	
	The structure expected is
	<containerTag>
		<row><col_1></col_1><col_2></col_2></row>
		<row><col_1></col_1><col_2></col_2></row>
		<row><col_1></col_1><col_2></col_2></row>
	</containerTag>
	
	A onSort function might be passed to this function
	to set a handler for sorting
	
	unsortable if set to true, doesn't apply sorting 
****************************************/
function buildDataTable(xmlData,onSort,unsortable){
	//Create the table
	var table; //Table to build
	var schema; //Header array
	var rootData; //Save a copy of original Root
	var contentString; //Temp Buffer
	rootData=xmlData; 
	schema=new Array();
	table=document.createElement("table");
	table.id="SearsAjaxTable"+Math.round(1000*Math.random())  + "_" + Math.round(1000*Math.random());

	contentString="";
	
	//Try to get xmlData Information
	if(xmlData!=null && xmlData.childNodes.length>0){
			//Get the Root Element
			xmlData=getFirstNodeXType(xmlData.childNodes,1);
			if(xmlData!=null && xmlData.childNodes.length>0){
						var row;		

						//Select first row
						row=getFirstNodeXType(xmlData.childNodes,1);
						rowName=row.nodeName;
						//Get columns
						var curColumn;
						curColumn=0;						
						for(i=0;i<row.childNodes.length;i++){
								if(row.childNodes[i].nodeType==1){
									schema[curColumn++]=row.childNodes[i].nodeName;						
									}
							}
			}
		
			//Now, build the Header
			if (schema.length >0){
					
					var hrow,hcell,helem,thescript,curColumn, hspan;
					hrow=table.insertRow(-1);
					curColumn=0;
					
					
					//Write for each Column
					
					for(i=0;i<schema.length;i++){
								thescript="sortTableByColumn(document.getElementById(" + '"'+ table.id + '"'+ ")," + curColumn+ ");" 
								hcell=hrow.insertCell(-1);
								helem=document.createElement ("a");		
								hspan=document.createElement("span");
								if(unsortable!=true){
														
								    helem.href="javascript:"+thescript  ;
								    helem.setAttribute("onclick",thescript+";return false");
								  }
								hspan.appendChild(document.createTextNode(schema[i]));
								helem.appendChild(hspan);
								hcell.appendChild(helem);
								curColumn++;
								
								if (onSort!=null && onSort!='undefined')
									table.onchange=onSort;


							}
				
					curColumn=0;
					//Write Rows
					for(k=0;k<xmlData.childNodes.length;k++){
							row=xmlData.childNodes[k];

							if (row.nodeType==1){
								hrow=table.insertRow(-1);
								for (j=0;j<row.childNodes.length;j++){
									if (row.childNodes[j].nodeType==1){
											hcell=hrow.insertCell(-1);
											hcell.appendChild(document.createTextNode(getNodeValue(row.childNodes[j]) ));

											
									}									
								}							
								
							}
							
					}

			}

	}		
	
	return table;	
}
/************************************************************
	Sorts a given table by the desired column
****************************************************************/
	function sortTableByColumn(table,column){

		var pair; //To create a pair
		var vals; //To Hold Pairs
		vals=new Array();
		for(j=1;j<table.rows.length;j++){
				pair=new Pair(table.rows[j],getNodeValue(table.rows[j].cells[column]));
				vals.push (pair);	
				
				
		}
		//Sort the Elements
		vals.sort (orderByValue);
		var counter;
		counter=1;
		//Append the rows in order
		for(l=0;l<vals.length;l++){
			var nrow,orow,ncell;
			nrow=table.insertRow(counter++);//Create New Row
			orow=vals[l].key; //The Old Row

			for(k=0;k<orow.cells.length;k++){					
					ncell=nrow.insertCell(-1);
					ncell.appendChild( (orow.cells[k].childNodes.length>0)?orow.cells[k].firstChild:document.createTextNode("")  );
				}

		}

		//Clear the Rows
		while(table.rows.length>vals.length+1)table.deleteRow(table.rows.length-1);		

		table.onchange(table,column);
	}

/*********************************
	Helper function for pagination
********************************/
function doTablePagination(table,vfrom,vto){

								//Show/Hide
								for(i=1;i<table.rows.length;i++){

										if (i>= vfrom && i<=vto){
												table.rows[i].style.display="";
												table.rows[i].style.visibility="visible";
																							
											}
										else
											{
												table.rows[i].style.display="none";
												table.rows[i].style.visibility="hidden";												
											}
									}		
}


/*********************************************
Returns a Paginated Table containing this table
and the controls to search
********************************************/
function paginateTable(table, recordsPerPage,cssStyle){
	
	//Wrap the OnChange for the Given Table, so when sorted we notice and paginate
	var prevHandler=null;
	prevHandler=table.onchange;
	table.onchange=function(tobj,col){
		
		
		//Call the Original Function 
		if(prevHandler!=null){
			prevHandler(tobj,col);
				doTablePagination(tobj,0,recordsPerPage);
			}
	};
	
	
	var container,c,pages,plink,pnumber;
	//Containing Table
	container=document.createElement("table");
	//First Row, the table itself
	container.insertRow(-1).insertCell(-1).appendChild(table);
	//Second Row, paginated area
	c=container.insertRow(-1).insertCell(-1);
	c.align="right";
		
	//Set the total Number of pages needed
	pages= Math.ceil ((table.rows.length-1)/recordsPerPage);
	pages=(pages==0)?1:pages;
	//Add Numbers with pagination
	for(j=1;j<=pages+1;j++){
		pnumber=document.createElement("span");
		plink=document.createElement("a");
		if(cssStyle!=null)plink.className=cssStyle;
		if(j==(pages+1)){
		
					pnumber.appendChild(document.createTextNode('[Todos]'));
					plink.alt="all";
				}
			else{
			
					pnumber.appendChild(document.createTextNode(j+' '));
					plink.alt=j;
				}
		
		plink.appendChild(pnumber)	
		plink.onchange=function(obj,perPage){
								var ppage, vfrom,vto;
								var curPage;
								curPage=obj.alt;
											
													
								//Calculate Indexes to display
								if(curPage=='all'){
									vfrom=1;
									vto=table.rows.length-1;
									}
									else{
									vfrom= 1+ perPage*(curPage-1);
									vto= perPage*curPage;
									}

								//Paginate
								doTablePagination(table,vfrom,vto);
								
								//Set all links to normal case
								var links;
								links=c.getElementsByTagName("span");								
								for(t=0;t<links.length;t++)links[t].style.fontWeight="normal";
								obj.getElementsByTagName("span")[0].style.fontWeight="bold";
								
														
					};			
					plink.href="#";
					plink.onclick=function(){this.onchange(this,recordsPerPage);return false;}
		c.insertBefore(plink,null);
	}

		
	//Call Pagination for first Page
	var hlinks;
	hlinks=	c.getElementsByTagName("a");
	if(hlinks.length>0)hlinks[0].onclick();
					
	return container;
}

/*****************************************************
	This function applies the "fnc" function transformation
	to the columns of the given table (except for the first row)
	fnc is supposed to accept the original content(the cell), the table,
	the row Index and Column Index,	and return the desired content .
*******************************************************/
function columnTransform(table,columnIndex,fnc){
	
	if(table.rows.length==0)return table;
	
	if(columnIndex>=table.rows[0].cells.length || columnIndex<0)return table;

	if(table.rows.length>1){
			//Apply the transformation 
			for(i=1;i<table.rows.length;i++){
				table.rows[i].cells[columnIndex].innerHTML=fnc(table.rows[i].cells[columnIndex],table,i,columnIndex);
			}
	}
	return table;
}

/************************************************
	Inserts a new column, in an existing table,
	setting the Header with the "name".
	
***********************************************/
function appendColumn(table,name){

	var cont,td;
	if(table.rows.length==0)return;
	for(i=0;i<table.rows.length;i++){
		cont=(i==0)?name:"";
		td=table.rows[i].insertCell(-1);
		td.appendChild(document.createTextNode(cont));		
	
			
	}	
}
/**************************************************************
	Removes a given column from the table.
	
	CHECKED: Jan 07. Previous implementation didn't care
	for Sorting links
*************************************************************/

function removeColumn(table,colIndex){
	var i;
	for(i=0;i<table.rows.length;i++){
		table.rows[i].deleteCell(colIndex);
	}
	
	//--New Part. Fix Headers
	if(table.rows.length>0)
		for(i=0;i<table.rows[0].cells.length;i++){
			//Get the link
			var link=table.rows[0].cells[i].getElementsByTagName("a");
			if(link.length>0){
				//Get the Onclick
				link=link[0];						
				
				var code="sortTableByColumn(document.getElementById(" + '"'+ table.id + '"'+ ")," + i+ ");" 
				link.setAttribute("onclick", code+";return false;");
				link.href="javascript:" + code;
				
			}
		}
}
/**************************************************************
	Set the column name 
*************************************************************/
function setColumnName(table,colIndex,newName){

	if (table.rows.length==0)return;
	var name;
	name=table.rows[0].cells[colIndex].getElementsByTagName("span");
	if (name.length>0)name[0].innerHTML=newName;
	
}

/**************************************************************
	Applies alternate stylessheets
*************************************************************/
function applyAlternateCSS(table,css1,css2){

	for(i=0;i<table.rows.length;i++){
		table.rows[i].className=(i%2==0)?css1:css2;
		
		var anchors;
		anchors=table.rows[i].getElementsByTagName("span");
		//Set Inner Text for Anchors
		for(j=0;j<anchors.length;j++)anchors[j].className=(i%2==0)?css1:css2;			

	}
}
/******************************************************
	Applies a CSS to the Header
***********************************************************/
function applyHeaderCSS(table,css){	
	if(table.rows.length>0){
		table.rows[0].className=css;
		var anchors;
		anchors=table.rows[0].getElementsByTagName("span");
		//Set Inner Text for Anchors
		for(i=0;i<anchors.length;i++)anchors[i].className=css;				
	}
}


/************************************************************
	Shows the Given Element
**************************************************************/
function showElement(elementID){
		document.getElementById(elementID).style.display="block";
		document.getElementById(elementID).style.visibility="visible";
}

/************************************************************
	Hides Element
**************************************************************/

function hideElement(elementID){
		document.getElementById(elementID).style.display="none";
		document.getElementById(elementID).style.visibility="hidden";

}

/************************************************************
	Test For Visibility
**************************************************************/
function isVisibleElement(elementID){
	
	return document.getElementById(elementID).style.display=="block" &&  document.getElementById(elementID).style.visibility=="visible";
}

/******************************************************************
	Creates a Dom object from the specified XML String
******************************************************************/
function createDocumentFromXMLString(text){

	// code for IE
	if (window.ActiveXObject)
	  {
	  var doc=new ActiveXObject("Microsoft.XMLDOM");
	  doc.async="false";
	  doc.loadXML(text);
	   return doc;
	  }
	// code for Mozilla, Firefox, Opera, etc.
	else
	  {
	  var parser=new DOMParser();
	  var doc=parser.parseFromString(text,"text/xml");
	   return doc;
	  }

}


/***************************************************

	Creates a Table Based on an Array of Arrays

***************************************************/

function buildJSONDataTable(jsonArray,onSort,unsortable,firstRowHasNames){
	//Create the table
	var table; //Table to build
	var schema; //Header array
	schema=new Array();
	var theScript;
	table=document.createElement("table");
	table.id="SearsJasonAjaxTable"+Math.round(1000*Math.random())  + "_" + Math.round(1000*Math.random());







	var hrow,hcell,helem,thescript,curColumn, hspan;
	
	/***********************************/	
	//Build Table
	for (var i=0;i<jsonArray.length;i++){
		
		var row=jsonArray[i];

			/************************************
				 Build Header
			**************************************/
		 if (i==0){
			hrow=table.insertRow(-1);
			for (var k=0;k<row.length;k++){
					
					 
					thescript="sortTableByColumn(document.getElementById(" + '"'+ table.id + '"'+ ")," + curColumn+ ");" 
					hcell=hrow.insertCell(-1);
					helem=document.createElement ("a");		
					hspan=document.createElement("span");
					if(unsortable!=true){
														
							helem.href="javascript:"+thescript  ;
							 helem.setAttribute("onclick",thescript+";return false");
							  }
					var columnName;
					columnName=(firstRowHasNames)?row[k]:"Columna " + k;
					hspan.appendChild(document.createTextNode(columnName));
					helem.appendChild(hspan);
					hcell.appendChild(helem);									
					if (onSort!=null && onSort!='undefined')
							table.onchange=onSort;

			}

			if (firstRowHasNames){
			 i++;
			 row=jsonArray[i];
			}

		}

		 hrow=table.insertRow(-1);
		  for (var j=0;j<row.length;j++){
			hcell=hrow.insertCell(-1);
			
			hcell.appendChild(document.createTextNode(row[j]));
		}
	}
	
	
	return table;	
}