// Copyright 2006 WildStorms Digital.
// All Rights Reserved
//
// Ajax prototype
//
// Author: Ng Teng Yong
//

//alert('test ws.js');
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();
	}
}

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="1.5px";
		document.getElementById(divid).style.borderColor="#FF6600";
		document.getElementById(divid).style.position="absolute";
		//document.getElementById(divid).style.bottom="1.5em";
		//document.getElementById(divid).style.width="15em";
		
		var hdnSuggestWidth = document.getElementById("hdnSuggestWidth");
		if(hdnSuggestWidth != null)
		{
		    if(hdnSuggestWidth.value != "")
		    {
		        document.getElementById(divid).style.width=hdnSuggestWidth.value;
		    }
		}
	}
}

function HideDiv(divid)
{
	if (document.layers) 
		document.layers[divid].visibility="hide";
	else 
	{
		document.getElementById(divid).style.visibility="hidden";
		document.getElementById(divid).style.borderStyle="none";
	}
}

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)
{
    var temptextarr = new Array();
    temptextarr = text.split("|");
    var tempText = temptextarr[0];
    text = tempText;
    
    // For search
    if(tbid=='tb_ctl00_pag_tbPointName')
    {
        var hdnPointName = document.getElementById('ctl00_pag_hdnPointName');
        hdnPointName.value=tempText;
    }
    if(tbid=='tb_ctl00_pag_tbDepPoint')
    {
        var hdnPointName = document.getElementById('ctl00_pag_hdnDepPoint');
        hdnPointName.value=text;
    }
    if(tbid=='tb_ctl00_pag_tbArrivPoint')
    {
        var hdnPointName = document.getElementById('ctl00_pag_hdnArrivPoint');
        hdnPointName.value=text;
    }
    
    // For iframe
    if(tbid=='tb_tbDepPoint')
    {
        var hdnPointName = document.getElementById('hdnDepPoint');
        hdnPointName.value=text;
        EnableIframeButtons();
    }
    if(tbid=='tb_tbArrivPoint')
    {
        var hdnPointName = document.getElementById('hdnArrivPoint');
        hdnPointName.value=text;
        EnableIframeButtons();
    }
    
    el(tbid).value=text;
    HideDiv(dd_press);
    
    EnableButtons();
}

function EnableButtons()
{
    // For the page.
    
    // Enable the trek search button only if both of the point names have been inserted using the suggest control.
    var bSearchTreks = document.getElementById('ctl00_pag_bSearchTreks');
    var hdnDepPoint = document.getElementById('ctl00_pag_hdnDepPoint');
    var hdnArrivPoint = document.getElementById('ctl00_pag_hdnArrivPoint');
    if(hdnDepPoint.value != "" && hdnArrivPoint.value != "")
    {
        bSearchTreks.disabled = false;
        bSearchTreks.className = "buttons";
    }
    else
    {
        bSearchTreks.disabled = true;
        bSearchTreks.className = "buttonsDisabled";
    }    
    
    // Enable the button search button only if the point name has been inserted using the suggest control.
    var bSearchPoint = document.getElementById('ctl00_pag_bSearchPoint');
    var hdnPoint = document.getElementById('ctl00_pag_hdnPointName');
    if(hdnPoint.value != "")
    {
        bSearchPoint.disabled = false;
        bSearchPoint.className = "buttons";
    }
    else
    {
        bSearchPoint.disabled = true;
        bSearchPoint.className = "buttonsDisabled";
    }
}

function EnableIframeButtons()
{
    // For the iframe.
    var hSearchTreks_iframe = document.getElementById('hSearchTreks');
    var hdnDepPoint_iframe = document.getElementById('hdnDepPoint');
    var hdnArrivPoint_iframe = document.getElementById('hdnArrivPoint');
    if(hdnDepPoint_iframe.value != "" && hdnArrivPoint_iframe.value != "")
    {
        hSearchTreks_iframe.className = "hSearchTreks buttons";
        
        // Set the search url for the iframe button.
        var hdnHomePage = document.getElementById('hdnHomePage');
        hSearchTreks_iframe.href = hdnHomePage.value + "/Search/default.aspx?dep=" + hdnDepPoint_iframe.value.replace(/^\s+|\s+$/g,"").replace(' ' , '%20') + "&dest=" + hdnArrivPoint_iframe.value.replace(/^\s+|\s+$/g,"").replace(" ", "%20");
        
    }
    else
    {
        hSearchTreks_iframe.className = "hSearchTreks buttonsDisabled";
        hSearchTreks_iframe.href = "";
    }
}

function DisableButtons(arg)
{
    if(arg == "actb_ctl00_pag_tbDepPoint" || arg == "actb_ctl00_pag_tbArrivPoint")
    {
        var bSearchTreks = document.getElementById('ctl00_pag_bSearchTreks');
        bSearchTreks.disabled = true;
        bSearchTreks.className = "buttonsDisabled";
    }
    
    if(arg == "actb_ctl00_pag_tbPointName")
    {
        var bSearchPoint = document.getElementById('ctl00_pag_bSearchPoint');
        bSearchPoint.disabled = true;
        bSearchPoint.className = "buttonsDisabled";
    }
}

//SendQuery function sends request to server asyncrhonously.
function SendQuery(arg,key,dd_,styshet_)
{
    // Disable the buttons when the point name has been inserted using the suggest control and then modified.
    //DisableButtons(arg);
	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);
	}
}

function Process(dd,styshet)
{
	if (req.readyState == 4)
	{
		if (req.status == 200)// only if "OK"
		{
			if(req.responseText=="")
				HideDiv(dd);//hide drop down area
			else
			{
				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	
					el(dd).innerHTML=html;
			}
		}
		else
		{
			//el(dd).innerHTML="There was a problem retrieving data:<br>"+ req.statusText;
			el(dd).innerHTML="1 char min";
		}
	}
}