// Copyright 2006 WildStorms Digital.
// All Rights Reserved
//
// Ajax prototype
//
// Author: Ng Teng Yong
//


var req;			//xmlhttprequest object

//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();
	}
}

var shimmer;
var div;

function addShimmer(divid)
{
    shimmer = document.getElementById(divid + 'Shimmer');
    div = document.getElementById(divid);
    shimmer.style.position='absolute';
    shimmer.style.display='block';
    shimmer.style.top=div.offsetTop + "px";
    shimmer.style.left=div.offsetLeft + "px";
    shimmer.style.zIndex=div.style.zIndex - 1;
    
    var shimmerWidth = shimmer.style.width.replace(/px/g, '');
    shimmer.style.width = (parseInt(shimmerWidth) - 20) + "px";
    
}       

function removeShimmer(divid)
{
    var shimmer = document.getElementById(divid + 'Shimmer');
    shimmer.style.display='none';
}

function ShowDiv(divid)
{
	if (document.layers) 
		document.layers[divid].visibility="show";
	else
	{ 
		document.getElementById(divid).style.visibility="visible";
		document.getElementById(divid).style.borderStyle="solid";
		document.getElementById(divid).style.borderWidth="1px";
		document.getElementById(divid).scrollTop = 0;
		addShimmer(divid);
		window.setTimeout("shimmer.style.width = div.style.width;", 50);
	}
}

function HideDiv(divid)
{
	if (document.layers) 
		document.layers[divid].visibility="hide";
	else 
	{
		document.getElementById(divid).style.visibility="hidden";
		document.getElementById(divid).style.borderStyle="none";
		removeShimmer(divid);
	}
}

function hideAllSelects() {
    var collS = document.getElementsByTagName("SELECT");
    for (var i=0; i<collS.length; i++)
    collS[i].style.visibility = 'hidden';
}

function showAllSelects() {
    var collS = document.getElementsByTagName("SELECT");
    for (var i=0; i<collS.length; i++)
    collS[i].style.visibility = 'visible';
}

function movein(which,overcolor)
{
	which.style.background=overcolor;
}

function moveout(which,outcolor)
{
	which.style.background=outcolor;
}

//tbid - Textbox id
function press(tbid,text,dd_press)
{
    text = text.replace(/<B>/gi , "");
    text = text.replace(/<\/B>/gi , "");
    text = text.replace(/<B hasbox="2">/gi , "");
    
    text = text.replace(/&gt;/g , ">");
    text = text.replace(/&amp;/g, "&");
    
	el(tbid).value=text;
	HideDiv(dd_press);
}

//SendQuery function sends request to server asyncrhonously.

var controlKeyID = "";

function hideLoadingDiv()
{
    if(controlKeyID != "")
    {
        var loadingDiv = document.getElementById(controlKeyID + "_loadingDiv");
        loadingDiv.style.visibility = 'hidden';
    } 
}

function showLoadingDiv()
{
    if(controlKeyID != "")
    {
        var loadingDiv = document.getElementById(controlKeyID + "_loadingDiv");
        loadingDiv.style.visibility = "visible";
    } 
}

function SendQuery(arg,key,dd_,styshet_)
{
    if(key.match("_link") != null)
    {
        var keyID = key.replace("_link","");
        controlKeyID = keyID;
        var keyObject = document.getElementById(keyID);
        var keyValue;
        
        if(keyObject != null)
        {
            keyValue = keyObject.value;
        }

        try{
            showLoadingDiv();
        }catch(e){};
        
            key = keyValue;
    }
    
	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 + "=" + escape(key); //the get argument must be unique for each control.arg is a unique variable of a web page
	}
	else {
	    var url = baseurl.replace(/\?.+$/, '') + "?" + arg + "=" + escape(key);
	}

	if(req!=null)
	{
		req.onreadystatechange = function() {Process(dd_,styshet_,key);}; //event handler for each state change
		req.open("GET", url, true);
		req.send(null);
	}
}

    function teButtomImageSwap(link, imageFileName)
    {
       var imageId = link.id;
       imageId = imageId.replace("_link", "_img");
       var imagePath = "/Themes/hawaii/images/";
       var image = document.getElementById(imageId);
       
       if(image != null)
       {
            image.src = imagePath + imageFileName;
       }
    }

function checkKeyword(dd, keyword)
{
    var testString = "<div style=\"background-color:White;border-bottom: 1px solid #eeeeee;margin-bottom:0px; padding:1px; cursor:hand; font-family:Arial;font-size:10pt;\" onmouseover=\"movein(this,&quot;#809ab4&quot;)\" onmouseout=\"moveout(this,&quot;White&quot;)\" onclick=\"press('" + dd + "',this.innerHTML,'" + dd + "')\">test</div>";
    var command = eval('/' + keyword +'/gi');
    var matchArr = testString.match(command);

    
    if(matchArr == null)
	    return true;
    else
	    return false;
        
}

function Process(dd,styshet,keyword)
{
    
    
	if (req.readyState == 4)
	{
		if (req.status == 200)// only if "OK"
		{
		    
			if(req.responseText=="")
			{
				HideDiv(dd);//hide drop down area
				try{
					    window.setTimeout("hideLoadingDiv();",500);
					}catch(e){};
			}
			else
			{
			    if(keyword.length > 2)
				{
				    
				    ShowDiv(dd);//show drop down area
				    var xml = xmlParse(req.responseText);
				    var xslt = xmlParse(styshet);
				    var html = xsltProcess(xml, xslt);
				   
				    if (html=="")
					    HideDiv(dd);
				    else	
				        if(keyword.length > 2 && checkKeyword(dd,keyword))
				        {
				            var command = eval('/(' + keyword.toUpperCase() +')/gi');
				            html = html.replace(command,"<b>$1</b>");
				        }
    				   
				        //document.getElementById(dd).style.width="450px";		    

					    el(dd).innerHTML=html;
			        }
			        else if(keyword == "")
			        {
			            HideDiv(dd);
			        }
					
					try{
					    window.setTimeout("hideLoadingDiv();",500);
					}catch(e){};
			}
		}
		else
		{
			el(dd).innerHTML="There was a problem retrieving data:<br>"+ req.statusText;
		}
	}
}

