var fullURL = parent.document.URL;
var objXML, objXSLT;
var xsltProcessor;
var intBrowser;

var strURL, strXSLTURL, strMainURL, strParam, strXMLData, strXSLTData, strOutputData, strOutputHTML, strTableOutput;

//get the original url
strMainURL = fullURL.split("?")[0];

//constant values, proxy_url and search_engine_url need to setup accordingly
var PROXY_URL				= "proxy.php";
var SEARCH_ENGINE_URL		= "http://search3.jobstreet.com/mobile/searchGoXML.asp";
var DEFAULT_TEMPLATE		= "_template/template_default.xsl";
var FOOTNOTE_LOADING		= "<table border='0' style='WIDTH: 100px; HEIGHT: 400px' align='center'><tr><td align='center'><img src='ajax-loader.gif' border='0'></td><td><font size='2'><b>Loading...</b></font></td></tr></table>";
var FOOTNOTE_CONN_ERROR		= "<table border='0' align='center'><tr><td><font size='2'><b>Job listing is currently not available.</b></font></td></tr></table>";
var FOOTNOTE_BROWSER_ERROR	= "<table border='0' align='center'><tr><td><font size='2'><b>Your browser is not supported.</b><br>Browsers that compatible are Internet Explorer 5.0+, Safari 1.2, Mozilla 1.0 / Firefox, Opera 8+, and Netscape 7.</font></td></tr></table>";
var FOOTNOTE_NORESULT		= "<table border='0' style='WIDTH: 350px; HEIGHT: 200px' align='center'><tr><td><font size='2'><b>There are no jobs matching your requirements.</b></font><br>We suggest that you,<br><li><a href='" + strMainURL + "'>View back the original listed job(s)</a></li><li><a href='http://my.jobstreet.com/myjobstreet/registration.htm'>Sign Up LiNa's Job Alert</a></li><li>Refine your current search (View the <a onclick=\"javascript:ow('http://sg.jobstreet.com/jobs/search_help.htm',this);return false;\" href='http://sg.jobstreet.com/jobs/search_help.htm'>search tips</a>)</li><li>Try a new search</li></td></tr></table>";
var DEFAULT_TABLE			= "<table border='0' cellpadding='5' cellspacing='0' width='100%'>";

//function to perform search
function query(strAction, strTemplateName, strTable, strProxyDirectory, strLoading, strNoResult, strSite, strBy, strFr, strSourceid, strLoc, strLoc1, strSpe, strLvl, strQua, strQuaOp, intDy, strEm, strKey, strTyp, intIndustry, intPid, intRow, intXMLNodes, strCls, strAid, strAsid, intPage, strDp, intC, strCl, strOvs, strTle, intPt, intGd, intCd){

	intBrowser = GetXmlHttpObject();

	if(intBrowser == -1){ //if client browser not compatible
		getFootNotesText("result", FOOTNOTE_BROWSER_ERROR);
		return;
	}else{
		//set proxy directory
		if (strProxyDirectory != ""){
			PROXY_URL = strProxyDirectory + PROXY_URL;
		}

		//set loading footnotes
		if (strLoading != "") {
			FOOTNOTE_LOADING = strLoading;
		}

		//set no result footnotes
		if (strNoResult != "") {
			FOOTNOTE_NORESULT = strNoResult;
		}

		//show loading footnotes
		getFootNotesText("result", FOOTNOTE_LOADING);

		//constructing url for ajaxing
		strURL = PROXY_URL + "?url=";
		strParam = SEARCH_ENGINE_URL + "?Action=" + strAction + "&site=" + strSite + "&by=" + strBy + "&fr=" + strFr + "&sourceid=" + strSourceid + "&loc=" + strLoc + "&loc1=" + strLoc1 + "&spe=" + strSpe + "&lvl=" + strLvl + "&qua=" + strQua + "&qua_op=" + strQuaOp + "&dy=" + intDy + "&em=" + strEm + "&keyword=" + parameter("keyword") + "&key=" + strKey + "&typ=" + strTyp + "&ind=" + intIndustry + "&pid=" + intPid + "&rows=" + intRow + "&nodes=" + intXMLNodes + "&cls=" + strCls + "&aid=" + strAid + "&asid=" + strAsid + "&pg=" + intPage + "&dp=" + strDp + "&c=" + intC + "&cl=" + strCl + "&ovs=" + strOvs + "&tle=" + strTle + "&pt=" + intPt + "&gd=" + intGd + "&cd=" + intCd;

		//build url for ajaxing
		strURL = strURL + escape(strParam);

		//assigning template for remoting
		strXSLTURL = getExists(strTemplateName, DEFAULT_TEMPLATE);
		strTableOutput = getExists(strTable, DEFAULT_TABLE);
		
		//Connect to remote server to get the XML data
		objXML.onreadystatechange = stateChanged;
		getTemplate(objXML, strURL, true);
	}
} 

//function on changing state
function stateChanged(){
	if (objXML.readyState==4 || objXML.readyState=="complete") { 
		strXMLData = objXML.responseXML;

		//do operation based on status
		if (strXMLData == null || objXML.responseText == "" || (!objXML.responseText.match("result"))){
			getFootNotesText("result", FOOTNOTE_CONN_ERROR);
		}else if (strXMLData.getElementsByTagName("total")[0].childNodes[0].nodeValue == 0){
			getFootNotesText("result", FOOTNOTE_NORESULT);
		}else{
			//try to get the requested template
			getTemplate(objXSLT, strXSLTURL, false);
			strXSLTData = objXSLT.responseXML;
			strMatch = objXSLT.responseText.match("xml");

			if(!strMatch){
				//get the default template
				getTemplate(objXSLT, DEFAULT_TEMPLATE, false);
				strXSLTData = objXSLT.responseXML;
				strMatch = objXSLT.responseText.match("xml");
				if(!strMatch){
					getFootNotesText("result", FOOTNOTE_CONN_ERROR);
					return;
				}
			}
			
			//append content based on browser compatibility
			if(intBrowser == 0){
				xsltProcessor.importStylesheet(strXSLTData);
				elByI("result").innerHTML = "";

				strOutputData = xsltProcessor.transformToFragment(strXMLData, document);
				strOutputHTML = dataCleaner(strOutputData.childNodes[0].innerHTML);

				elByI("result").innerHTML = strTableOutput + strOutputHTML + "</table>";
				
			}else{
			
				strOutputHTML = dataCleaner(strXMLData.transformNode(strXSLTData));
				elByI("result").innerHTML = strOutputHTML;
			}
		}
	}
} 

//function to get the templates and xml data
function getTemplate(objTemplate, strURL, blnAsync){
	objTemplate.open("GET", strURL, blnAsync);
	objTemplate.send(null);
}

//function to get the XMLHTTP object
function GetXmlHttpObject(){ 
	if (document.implementation && document.implementation.createDocument){
		xsltProcessor = new XSLTProcessor();
		objXML = new XMLHttpRequest();
		objXSLT = new XMLHttpRequest();
		return 0;
	}
	else if (window.ActiveXObject){
		objXML = new ActiveXObject("Microsoft.XMLHTTP");
		objXSLT = new ActiveXObject("Microsoft.XMLHTTP");
		return 1;
	}
	return -1;
}

//function to get the footnotes
function getFootNotesText(objName, strText){
	elByI(objName).innerHTML = strText;
}

//function to get the query string
function parameter(queryString){
	var strAllParam;
	var strParam;
	var strValue;
	
	if(fullURL.indexOf("?") != -1){
		strAllParam = fullURL.split("?");
		strParam = strAllParam[1].split("&");
		for(i in strParam){
			strValue = strParam[i].split("=");
			if(strValue[0] == queryString){
				return strValue[1];
			}
		}
	}
	return "";
}

//function to set the selected options
function setSelectOptions(objName, strValue){
	var intTotal;
	
	strValue = unescape(strValue);
	intTotal = elByN(objName)[0].options.length;
	
	for (var i = 0;i < intTotal; i++) {
		if(elByN(objName)[0].options[i].value == strValue){
			elByN(objName)[0].selectedIndex = i;
			break;
		}
	}
}

//function to set the keyword
function setKeywordValue(objName, strValue){
	elByN(objName)[0].value = strValue;
}

//function to set the hash value
function setHash(strValue){
	window.location.hash = strValue;
}

//function to duplicate and encode the key value when submitting a form
function submittingForm(objNameCopyFrom, objNameCopyTo){
	elByN(objNameCopyTo)[0].value = escape(elByN(objNameCopyFrom)[0].value);
}

//function to clean data (will require to add more)
function dataCleaner(strValue){
	
	var strFilterData = new Array();
	var strCleanData = new Array();
	var i;
	
	strFilterData[0] = /&amp;#/g;		strCleanData[0] = "&#";
	strFilterData[1] = /&amp;rsquo;/g;	strCleanData[1] = "'";
	strFilterData[2] = /&amp;ndash;/g;	strCleanData[2] = "-";
	
	for (i in strFilterData){
			strValue = strValue.replace(strFilterData[i], strCleanData[i]);
	}

	return strValue;
}

//function to get the exists and not exists value
function getExists(strExists, strNotExists){

	if (strExists != ""){
		return strExists;
	}else{
		return strNotExists;
	}
}

//function to get elements by name
function elByN(x) { return document.getElementsByName(x); }

//function to get elements by ID
function elByI(x) { return document.getElementById(x); }
