var kCode;
var ctryCode;

var http_request = false;
var c = 1;


window.onload=init;

if (window.XMLHttpRequest) 
{ // Mozilla, Safari,...
	http_request = new XMLHttpRequest();
	if (http_request.overrideMimeType) 
	{
		http_request.overrideMimeType('text/xml');
                	// See note below about this line
	}
} 
else if (window.ActiveXObject) 
{ // IE
	try 
	{
                	http_request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) 
	{
                	try 
		{
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	}
}
if (!http_request) 
{
            alert('Giving up :( Cannot create an XMLHTTP instance');
            //return false;
}
function init(){
	document.onmousedown = closeSuggest;
	document.onkeyup = closeSuggest;
	ctryCode = aJCtry[document.frmSearchBox.ctry.selectedIndex];
}
function changeCtry(){
	ctryCode = aJCtry[document.frmSearchBox.ctry.selectedIndex];
}
function closeSuggest(){
	if (event.srcElement.id != "sug" && event.srcElement.id != "keyword")
		hideSuggest('dont');
}

function getSuggest(objRef,e,eGuideURL){
	kCode = e.keyCode;
	if (objRef.value.length > 1 && kCode != 13)
	{ 
		var k = objRef.value;
		
		document.getElementById("frame2").style.left=objRef.offsetLeft+81;
		document.getElementById("frame2").style.top=objRef.offsetTop+141;
		document.getElementById("frame2").style.width=objRef.offsetWidth;
		document.getElementById("frame2").style.height=objRef.offsetHeight + 50;
		document.getElementById("frame2").style.visibility="visible";
		//document.getElementById("frame2").focus();
		objRef.focus();

		var srchType = '';
		for(j=0; j<document.frmSearchBox.searchType.length;j++)
			if (document.frmSearchBox.searchType[j].checked)
				srchType = srchType + document.frmSearchBox.searchType[j].value + ',';

		var url = eGuideURL + "suggest.asp?keyword=" + escape(k) + '&ctry=' + ctryCode + '&searchType='+srchType;
		http_request.open('GET', url, true);
		http_request.onreadystatechange = fillSuggest;
		http_request.send(null);

	}
	else
		document.getElementById("frame2").style.visibility="hidden";
}


function fillSuggest() {
	if (http_request.readyState == 4) 
	{
		if (http_request.status == 200) 
		{
			document.getElementById('frame2').innerHTML=http_request.responseText;
			if ((kCode == 40 || kCode == 38) &&  document.getElementById("sug") != null)
				document.getElementById('sug').focus();
		} 
		else 
		{
			//alert('There was a problem with the request.');
            		}
        	}
}
function initTxtBox(){
	document.frmSearchBox.keyword.value = document.getElementById('sug').value;
}
function hideSuggest(focusKey){
	
	if (focusKey != "dont"){
		document.getElementById('keyword').focus();
	}
	setTimeout("document.getElementById(\"frame2\").style.visibility='hidden'",100);
}
function chkEnter(e){
	if (e.keyCode == 13){
		hideSuggest();
	}
}
function validateSearch(objForm)
{
	var searchTypeSelected = false;
	var keyword = objForm.keyword.value;
	if (trimStr(keyword) == "" || keyword.length < 2)
	{
		alert ("Please enter at least 2 characters to search.");
		return false;
	}
	else
	{
		with (objForm)
		{
			for(i=0; i<elements.length; i++)
			{
				if(elements[i].type == "checkbox")
				{
					if(elements[i].checked)
						searchTypeSelected = true;
				}
			}
		}
		if (!searchTypeSelected)
		{
			alert ('Please select at least one option from underneath to perform a search.')
			return false;
		}
		else
		{
			objForm.action = objForm.ctry.value + "/search.asp";
			return true;
		}
	}
}
function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
   	Char = sText.charAt(i); 
      	if (ValidChars.indexOf(Char) == -1) 
        {
        	IsNumber = false;
        }
   }
   return IsNumber;
   
}
function addBookmarkForBrowser() {
	if(document.all){
		window.external.AddFavorite(document.location.href, document.title);
	}else{
		window.sidebar.addPanel(document.title, document.location.href,"");
	}
}
function makeHomepg(obj,homepageURL)
{
	obj.style.behavior='url(#default#homepage)';
	obj.setHomePage(homepageURL);
	return false;
}
function trimStr(sStr){
	var retStr   = "" ;
	var lLoop = true;
	var first = -1;
	var last  = sStr.length;
	for(var i=0; i < sStr.length && lLoop; i++){
		if(sStr.charAt(i) != " ") lLoop = false;
		else	first = i;
	}
	lLoop = true;
	for(var i=sStr.length-1; i>= 0 && lLoop; i--){
		if(sStr.charAt(i) != " ") lLoop = false;
		else	last = i;
	}
	for(var i=first+1; i<last; i++)
		retStr += sStr.charAt(i);
	return (retStr);
}
function checkEnquiry(objForm)
{
	var returnVal = true;
	var reg = new RegExp();
	reg.compile("[A-Za-z0-9._-]+@[^.]+\..+"); 
	with (objForm)
	{
		if (trimStr(enquiry.value) == "" || trimStr(name.value) == "" || trimStr(email.value) == "" || trimStr(phone.value) == "" || trimStr(vericode.value) == "")
		{
			alert ("Please make sure that all those fields marked with * must be supplied.");
			returnVal = false;
		}
		else if (!reg.test(email.value))
		{
			alert ("Please enter a valid email address.");
			returnVal = false;
		}
		else if (!IsNumeric(phone.value) || !IsNumeric(postal.value) || !IsNumeric(fax.value))
		{
			alert ("Please make sure that the postal code, tel and fax must be in numeric values.");
			returnVal = false;
		}	
					
	}
	return returnVal;
}
function validateFields(objForm){
	var returnVal	= true;
	var objFunc	= validateFields;
	with (objForm){
		for(i=1; i < objFunc.arguments.length; i++){
			var fldname	= objFunc.arguments[i];
			var fldvalue	= trimStr(objForm.elements[fldname].value);
			if(fldvalue.length == 0){
				i 		= objFunc.arguments.length;
				returnVal	= false;
				alert("Please fill in all the fields provided, thank you.")
			}
		}
			
	}

	return returnVal;
}

function showMore(objMore, objLink){
	
	var txtLink = (document.all) ? trimStr(objLink.innerText) : trimStr(objLink.textContent);
	if(txtLink == "Show All"){
		objMore.style.display 	= "";
		if(document.all) 	objLink.innerText	= "Hide";
		else			objLink.textContent	= "Hide";
	}else{
		objMore.style.display 	= "none";
		if(document.all) 	objLink.innerText	= "Show All";
		else			objLink.textContent	= "Show All";
	}
}

function enlargeLook(path)
{
	window.open('enlargeLook.asp?p='+path,'largeimage','menubar=no,scrollbars=yes,width=450,height=450,resizable=no');

}
function changeLocationBrand(changeType)
{
	if (changeType == "brand" && document.getElementById("location_id"))
	{
		document.relatedLocationBrandFrm.location_id.value = "";
	}
	document.relatedLocationBrandFrm.submit();
}
function changeStateBrand(changeType)
{
	if (changeType == "brand" && document.getElementById("state_no"))
	{
		document.relatedLocationBrandFrm.state_no.value = "";
		if (document.getElementById("substate_no"))
			document.relatedLocationBrandFrm.substate_no.value = "";
	}
	if (changeType == "state" && document.getElementById("substate_no")) 
		document.relatedLocationBrandFrm.substate_no.value = "";

	document.relatedLocationBrandFrm.submit();
}
