// Copyright 2006 WildStorms Digital.
// All Rights Reserved
//
// Ajax prototype
//
// Author: Ng Teng Yong
//


var req;			//xmlhttprequest object
var dropdownid,strxsl;
var lasttable='';
var GLOBALSmartDivID="";
var TimeOut_Variable='';

//initialize cross browser xmlhttprequest object
function Initialize()
{
	try
	{
	    
		req=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			req=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			req=null;
		}
	}

	if(!req && typeof XMLHttpRequest!="undefined")
	{
		req=new XMLHttpRequest();
	}
}

function ShowDiv(divid)
{
var objtxtbox=null;
var objdiv=null;
var txtboxid=divid.replace("da_","tb_");
objtxtbox=el(txtboxid);

	if (document.layers)
	{ 
		document.layers[divid].visibility="show";
		if(document.getElementById("smartSearchIframe")!=null)
		{
		document.getElementById("smartSearchIframe").style.display="inline";
		 document.getElementById("smartSearchIframe").visibility="show";
//		 if(objtxtbox==null)
//		 {
		     document.getElementById("smartSearchIframe").style.pixelWidth=document.layers[divid].style.pixelWidth;
		     document.getElementById("smartSearchIframe").style.pixelHeight=document.layers[divid].offsetHeight;
		     document.getElementById("smartSearchIframe").style.pixelTop= document.layers[divid].offsetTop;
		     document.getElementById("smartSearchIframe").style.pixelLeft=document.layers[divid].offsetLeft;
//		 }
//		 else
//		 {
//		     document.getElementById("smartSearchIframe").style.pixelWidth=objtxtbox.style.pixelWidth;
//		     document.getElementById("smartSearchIframe").style.pixelHeight=document.layers[divid].offsetHeight;
//		     document.getElementById("smartSearchIframe").style.pixelTop= objtxtbox.offsetTop;
//		     document.getElementById("smartSearchIframe").style.pixelLeft=objtxtbox.offsetLeft;
//		 }
		 //document.getElementById("smartSearchIframe").style.pixelWidth=document.layers[divid].style.pixelWidth;
		}
	}
	else
	{ 
	
		document.getElementById(divid).style.visibility="visible";
		document.getElementById(divid).style.borderStyle="solid";
		document.getElementById(divid).style.borderWidth="1px";
//		if(objtxtbox==null)
//		{
            document.getElementById("smartSearchIframe").style.display="inline";
		    document.getElementById("smartSearchIframe").visibility="visible";
		    document.getElementById("smartSearchIframe").style.width=document.getElementById(divid).offsetWidth;
		    document.getElementById("smartSearchIframe").style.height=document.getElementById(divid).offsetHeight;
	         document.getElementById("smartSearchIframe").style.top= document.getElementById(divid).offsetTop;
	         document.getElementById("smartSearchIframe").style.left=document.getElementById(divid).offsetLeft;
		     
//	   }
//	   else
//	   {
//            document.getElementById("smartSearchIframe").visibility="visible";
//            document.getElementById("smartSearchIframe").style.width=objtxtbox.offsetWidth;
//            document.getElementById("smartSearchIframe").style.height=document.getElementById(divid).offsetHeight;
//            document.getElementById("smartSearchIframe").style.top= objtxtbox.offsetTop;
//            document.getElementById("smartSearchIframe").style.left=objtxtbox.offsetLeft;
//	   }
	}
}

function HideDiv(divid)
{
	if (document.layers)
	{
	if(document.layers[divid]!=null)
	{
		document.layers[divid].visibility="hide";
	}
	if(document.getElementById("smartSearchIframe")!=null)
	   {
	   document.getElementById("smartSearchIframe").style.display="none";
		document.getElementById("smartSearchIframe").visibility="hide";
		 document.getElementById("smartSearchIframe").style.pixelWidth="0";
		 document.getElementById("smartSearchIframe").style.pixelHeight="0";
	   }
    }
	else 
	{
	   if(document.getElementById(divid)!=null)
	   {
		document.getElementById(divid).style.visibility="hidden";
		document.getElementById(divid).style.borderStyle="none";
	   }
	   if(document.getElementById("smartSearchIframe")!=null)
	   {
	   document.getElementById("smartSearchIframe").style.display="none";
		document.getElementById("smartSearchIframe").visibility="hidden";
		 document.getElementById("smartSearchIframe").style.pixelWidth="0";
		 document.getElementById("smartSearchIframe").style.pixelHeight="0";
	   }
	}
	//GLOBALSmartDivID="";
	
}
function HideIFrame()
{
if (document.layers)
	{
	   if(document.getElementById("smartSearchIframe")!=null)
	   {
		document.getElementById("smartSearchIframe").visibility="hide";
		 document.getElementById("smartSearchIframe").style.pixelWidth="0";
		 document.getElementById("smartSearchIframe").style.pixelHeight="0";
	   }
    }
	else 
	{
	  if(document.getElementById("smartSearchIframe")!=null)
	   {
		document.getElementById("smartSearchIframe").visibility="hidden";
		 document.getElementById("smartSearchIframe").style.pixelWidth="0";
		 document.getElementById("smartSearchIframe").style.pixelHeight="0";
		 }
	}
}

function movein(which,overcolor,txtuniqueid,outcolor,position)
{
  //alert(which.id);
	//which.style.background=overcolor;
	if(which!=null)
	{
	if(txtuniqueid!=null && eval(txtuniqueid+"prevcontrol")!=null)
	{
	    if(eval(txtuniqueid+"prevcontrol")!='' && el(eval(txtuniqueid+"prevcontrol"))!=null)
	    {
	        
		    moveout(el(eval(txtuniqueid+"prevcontrol")),outcolor);
		    this[txtuniqueid+"prevcontrol"]=which.id;
	        this[txtuniqueid+"prevcontrolno"]=position;
	    }
	}
	which.title=which.innerHTML;
	which.className=overcolor
	//alert(which.id + "::" + which.innerHTML);
	}
}

function moveout(which,outcolor)
{
	//which.style.background=outcolor;
	if(which!=null)
		which.className=outcolor;
}
function DelayCall(objname,text,dd_press,postback,defaultbutton)
{
             if(TimeOut_Variable!='' && text != "")
             {
                 press(objname,text,dd_press,postback);
    		   
		         if(defaultbutton!='' && el(defaultbutton)!=null)
		        {
		         el(defaultbutton).click();
		         return false;
		        }
                else
                {
                return false;
                }
                clearTimeout(TimeOut_Variable);
                TimeOut_Variable='';
            }
}
/***********************************************
* Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
                
function handleEnter (field, event) {
var ua = navigator.userAgent.toLowerCase();
		   if ( ua.indexOf( "safari" ) != -1 )
		   {
		        var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		        if (keyCode == 13 || keyCode==3) {  // in safari enter key in number area returns 3
			        var i;
			       
			        for (i = 0; i < field.form.elements.length; i++)
				        if (field == field.form.elements[i])
					        break;
			        i = (i + 1) % field.form.elements.length;
        			
			        field.form.elements[i].focus();
			        return false;
		        } 
		        else
		        return true;
		}
		else
		        return true;
	}      

function NavigatePointer(objname,txtuniqueid,dd_press,overcolor,outcolor,defaultbutton,postback,e)
{

if(eval(txtuniqueid+"prevcontrol")!=null && eval(txtuniqueid+"prevcontrolno")!=null)
{
	var controltonavigate="";
	var keycode=(e.keyCode!=null?e.keyCode:(event.keyCode!=null?event.keyCode:(evt.keyCode!=null?evt.keyCode:e.which)));
	//alert(objname);
	
	if(objname!=null && el(objname)!=null)
		{
		//alert(eval(txtuniqueid+"prevcontrolno"));
		controltonavigate=objname;
		if(keycode==9) // tab out
	    {
		    if(el(objname)!=null)
		    {
		       var text=el(objname).value;
		       TimeOut_Variable= setTimeout("DelayCall('"+objname+"','"+text+"','"+dd_press+"','"+postback+"','"+defaultbutton+"');",1000);
		    }
	    }
		if(eval(txtuniqueid+"prevcontrol")=='')
		{
			controltonavigate+=1;
			this[txtuniqueid+"prevcontrolno"]=1
		}
		
		else if(keycode==38 || keycode==98)
		{
		controltonavigate+=eval(txtuniqueid+"prevcontrolno") - 1;
		this[txtuniqueid+"prevcontrolno"]=parseInt(eval(txtuniqueid+"prevcontrolno"),10)-1;
		}
		else if(keycode==40 || keycode==104)
		{
		controltonavigate+=eval(txtuniqueid+"prevcontrolno")+1;
		this[txtuniqueid+"prevcontrolno"]=parseInt(eval(txtuniqueid+"prevcontrolno"),10)+1;
		
		}
		
		else
		{
		controltonavigate=eval(txtuniqueid+"prevcontrol");
		
		}
		}  
	if(keycode==38 || keycode==40 || keycode==98 || keycode==104 || keycode==13)
	{
//alert(eval(txtuniqueid+"prevcontrolno"));
	if(eval(txtuniqueid+"prevcontrol")!='' && el(eval(txtuniqueid+"prevcontrol"))!=null)
	{
		//moveout();
	}
	if(el(controltonavigate)!=null)
	{
		
		movein(el(controltonavigate),overcolor,txtuniqueid,outcolor,eval(txtuniqueid+"prevcontrolno"));
	    this[txtuniqueid+"prevcontrol"]=controltonavigate;
	}
	else
	{
	    movein(el(objname+1),overcolor,txtuniqueid,outcolor,0);
		this[txtuniqueid+"prevcontrol"]=objname+1;
		this[txtuniqueid+"prevcontrolno"]=0;
	}
	if(keycode==13)
	{
		if(el(objname)!=null && el(controltonavigate)!=null)
		{
		   var text=el(controltonavigate).innerHTML;
		   press(objname,text,dd_press,postback);
		   var ua = navigator.userAgent.toLowerCase();
//		   if ( ua.indexOf( "safari" ) != -1 )
//		   {
//		  	    return false;
//		   }
//		   else
//		   {
                if(defaultbutton!='' && el(defaultbutton)!=null)
                {
                el(defaultbutton).click();
                return false;

                }
                else
                {
                return false;
                }
//          }
		}
	}

	}// end keycode
//	else if(keycode==13)
//	{
//		if(el(objname)!=null && el(controltonavigate)!=null)
//		{
//		   var text=el(controltonavigate).innerHTML;
//		   press(objname,text,dd_press);
//		   if(defaultbutton!='' && el(defaultbutton)!=null)
//		   {
//		    el(defaultbutton).click();
//		   }
//		}
//	}
 }
}

function keydown(tbid,text,dd_press,which,overcolor)
{
// call the function if arrow key is pressed

  movein(which,overcolor);
 // call the function when enter key is pressed
  press(tbid,text,dd_press);
}

//tbid - Textbox id
function press(tbid,text,dd_press,postback)
{
	
	try
	{
	if(el(tbid)!=null)
	{
	    el(tbid).value=text;
	    if(postback!=null && postback=='true')
	    {
	     __doPostBack(tbid,'');
	    }
	}
	HideDiv(dd_press);
	}
	catch(e)
	{
	}

}
function CallPress(tbid,dd_press)
{
try
{
 if(el(tbid)!=null && eval(tbid.replace("tb_","")+"prevcontrol")!=null)
 {
   press(tbid,eval(tbid.replace("tb_","")+"prevcontrol").innerHTML,dd_press);
 }
 }
 catch(e)
 {
 return false;
 }
}

//SendQuery function sends request to server asyncrhonously.
var glarg;
function SendQuery(arg,key,dd_,styshet_,tableBinded,obj,e,ctrlwidth,submitpage)
{

InitializeXSLForSmartSearch(arg.replace("actb_",""),ctrlwidth,submitpage);
	var keycode=(e.keyCode!=null?e.keyCode:(event.keyCode!=null?event.keyCode:(evt.keyCode!=null?evt.keyCode:e.which)));
	if(GLOBALSmartDivID!="" && GLOBALSmartDivID!=dd_)
	{
        HideDiv(GLOBALSmartDivID)
    }
	GLOBALSmartDivID=dd_;
	if(keycode!=38 && keycode!=40 && keycode!=98 && keycode!=104 && keycode!=13)
	{
	glarg=key;
//	Initialize();
//	var baseurl=window.location.href; //get the url of the current web page
//	
//	//check if other HTTP GET argument exist,if yes use "?" else use "&" to concatenate http request string
//	if (baseurl.indexOf("?")==-1)
//		var url=baseurl+"?"+arg+"="+key; //the get argument must be unique for each control.arg is a unique variable of a web page
//	else
//		var url=baseurl+"&"+arg+"="+key;	
//		
//	if(req!=null)
//	{
//		req.onreadystatechange = function() {Process(dd_,styshet_);}; //event handler for each state change
//		req.open("GET", url, true);
//		req.send(null);
//	}

if(lasttable!='' && tableBinded!=lasttable)
{
//stylshet_=stylshet.replace(lasttable+'[',tableBinded+'[');
lasttable=tableBinded;
}
dropdownid=dd_;
//strxsl=styshet_;
try
{
eval(obj+"_"+"DoTheCallback('"+key+"','');");
}
catch(e)
{
alert('unable to find callback function');
}
    //SmartSearch.AsyncSmartSearchData(key,Process)
	}
}
//dd,styshet
function Process(response,ctx)
{
try
{
	var dd=dropdownid;
	var styshet=strxsl;
	if (response!="")// only if "OK"
		{
			if(response=="")
				HideDiv(dd);//hide drop down area
			else
			{
			   
				
				var xml = xmlParse(response);
				//var xslt = xmlParse(styshet);
				var xslt = xmlParse(XslFor_SmartSearch);
				
				var html = xsltProcess(xml, xslt);
				if (html=="")
					HideDiv(dd);
				else
				{	
				//alert(html);
					el(dd).innerHTML=html;
				    ShowDiv(dd);//show drop down area	
				}
					
					
			}
		}
		else
		{
		HideDiv(dd);
		}
	}
	catch(e)
	{
	}	
	
}
function elDiv(id)
{
  if(document.layer!=null)
   {
    return document.layer[id];
   }
  else
  {
    return document.getElementById(id);
  }
}
function SmartTextChanged(divid)
{
//document.layers[divid].visibility="show"
    if (document.layers)
	{
	  if(elDiv(divid)!=null && elDiv(divid).visibility=="show")
          {
          return false;
          }
          else
          {
          return true;
          }
    }
	else 
	{
	 if(elDiv(divid)!=null && elDiv(divid).style.visibility=="visible")
      {
      return false;
      }
      else
      {
      return true;
      }
	}
  
}

var XslFor_SmartSearch="";
function InitializeXSLForSmartSearch(ctrlid,ctrlwidth,submitpage)
{
XslFor_SmartSearch="<xsl:stylesheet><xsl:template match=\"/\"> ";
XslFor_SmartSearch+="			<xsl:for-each select=\"GenericSmartSearch/SmartSearch\" >"; 
XslFor_SmartSearch+="				<div id='"+ctrlid+"{position()}' class='bg-smartsearch-000000' style='cursor:hand; font-family:Courier New;font-size:10pt;width:"+ctrlwidth+"px;z-index:200;'  onmouseover='movein(this,\"bg-smartsearch-000001\",\""+ctrlid+"\",\"bg-smartsearch-000000\",\"{position()}\")' onmouseout='moveout(this,\"bg-smartsearch-000000\")' onclick=\"press('"+ctrlid+"',this.innerHTML,'da_"+ctrlid+"','"+submitpage+"')\"  ><xsl:value-of select=\"SmartIDName\" /></div>"; 
XslFor_SmartSearch+="			</xsl:for-each>"; 
XslFor_SmartSearch+="	</xsl:template>";
XslFor_SmartSearch+="</xsl:stylesheet>";
}
////////////function overrideAjaxMethod()
////////////{
//////////////try
//////////////{
//////////////var ev=event;
//////////////}
//////////////catch(ex)
//////////////{
////////////try
////////////{
//////////////http://forums.asp.net/t/1106734.aspx
//////////////Error: [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE)
////////////    //MS Ajax isn't handling getstatus exception, so i have to insert my own try/catch
////////////    //I'm doing it by creating a new prototype method... instead of rewriting the whole
////////////    //function, i'm simply taking the text of the current function, changing the name
////////////    //via replace, and changing the line accessing status to have try/catch wrapped around
////////////    //it.  If the status call errors out, i'm returning error 999.  You can return whatever you
////////////    //want.
////////////    //PS.  I'm no javascript guru, so forgive me if I didn't use the most efficient
////////////    //code for creating a replacement function with the fixes.
////////////        //get the current function text
////////////        var executor = new Sys.Net.XMLHttpExecutor();
////////////        var funcText = executor.get_statusCode.toString();
////////////        //alert(funcText);
////////////        //our new function will have 'Patched_' appended to the start of the existing function
////////////        funcText = funcText.replace('Sys$Net$XMLHttpExecutor$get_statusCode', 'Patched_Sys$Net$XMLHttpExecutor$get_statusCode');
////////////        //replace status access with same line, except with try/catch block wrapped around it
////////////        funcText = funcText.replace('return this._xmlHttpRequest.status;', 'try {return this._xmlHttpRequest.status;} catch(e) {return 999;};');
////////////        //create new function using eval... to use funct = new Function or func = function() {...},
////////////        //I'd have to take the funcText and strip the function line and bracket from the beginning and the
////////////        //end.  I could have done it by removing the first and last lines, but I did it this way instead.
////////////        eval(funcText);
////////////        //have the prototype method point to the new function instead of the old one
////////////        Sys.Net.XMLHttpExecutor.prototype.get_statusCode = Patched_Sys$Net$XMLHttpExecutor$get_statusCode;
////////////        //alert(Sys.Net.XMLHttpExecutor.prototype.get_statusCode.toString());
//////////// }
//////////// catch(e)
//////////// {
//////////// alert('error:'+e.message);
//////////// }
//////////////}
////////////}
