var currenturl = unescape(location.href);
var opera = ((navigator.userAgent.indexOf(' Opera ') != -1) || (navigator.userAgent.indexOf('Opera/') != -1))?true:false;
var mscheck = (navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4 ) && (navigator.platform == "Win32")?true:false;
var http = getHTTPObject(); // We create the HTTP Object

var count=0;

var basic_countydet="a";

var basic_flgCounty=false;

function getHTTPObject() {
if (window.XMLHttpRequest) { // Mozilla, Safari,...
	
  http = new XMLHttpRequest();
    if (http.overrideMimeType) {
        http.overrideMimeType('text/html');
        //netscape.security.PrivilegeManager.enablePrivilege("UniversalPreferencesRead");
    }
} else if (window.ActiveXObject) { // IE
    try {
        http = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (i) {
        try {
            http = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (i) {}
    }
}
  return http;
}


//JSON IMPLEMENTATION
function getAddressyell4postcode(postcode,accountno,callbackfunc,CountyType,flgCounty){
	
	
	if(postcode=="")
	{
		alert("Please enter valid UK postcode...");
		return false;
	}
	else if(accountno=="")
	{
		alert("Please enter valid account number");
		return false;
	}
	else if(callbackfunc=="")
	{
		alert("Please enter callback function name");
		return false;
	}
	
	
	
	if(CountyType!="")
	{
		basic_countydet=CountyType;
	}
	
	
	if(flgCounty!="")
	{
		basic_flgCounty=flgCounty;
	}

	/*if(postcode.length < 6 || postcode.length > 9 )
	{
		alert("Postcode must be alteast 6 character and maximum 8 characters.");
		return false;	
	}*/
scriptTag=document.getElementById("maincontentspan");

            scriptTag.innerHTML="<table width='100%' height='100%' border='0' cellspacing='0' cellpadding='0'><tr><td align='center' valign='middle'><img src='http://lookup.yell4postcode.co.uk/yell4postcode/images/loading.gif'></td> </tr></table>";



var url = 'http://lookup.yell4postcode.co.uk/yell4postcode/inline3.ashx?accountnumber='+ accountno +'&postcode=' + postcode + '&callback='+callbackfunc+'&flgCounty='+flgCounty+'&CountyType='+CountyType; 
 


	//alert(url);
    headElement = document.getElementsByTagName("head").item(0);    
	if(getObj("factorialJSON"))
	{
		scriptTag=getObj("factorialJSON");
		headElement.removeChild(scriptTag);
	}
	
    var scriptTag = document.createElement("script");
    scriptTag.setAttribute("id", "factorialJSON");
    scriptTag.setAttribute("type", "text/javascript");
    scriptTag.setAttribute("src", url);
    headElement.appendChild(scriptTag);

	
    //headElement.removeChild(scriptTag);				
}


//SHOW ADDRESS LINE IN SELECT BOX
function SHOWADDDRESSLINES(factObj)
{
	scriptTag=getObj("maincontentspan");
	
	//if ADDRESS IS FOUND
	if(factObj.CONDITION=="1")
	{
		
		
    scriptTag.innerHTML="<select name=\"yell4postcode_drpAddress\" size=\"10\" class=\"label\" id=\"yell4postcode_drpAddress\" style=\"width:300px;\" onChange=\"callfunc(this.form,'"+factObj.CALLBACK_FUNCTION+"','"+factObj.POSTCODE+"','"+factObj.ACCOUNTNUMBER+"');\" ></select>";
	
	
		var addressshow=factObj.SELECTADDRESSSHOW.split("@@@@@");
		
		var selectedbox_cntrl=getObj("yell4postcode_drpAddress");
		
		for(i=0;i<addressshow.length;i++)
		{
			tmpvar=addressshow[i].split("@#@#@");
			
			addOption(selectedbox_cntrl,tmpvar[1],tmpvar[0]);
		}
		
			
	}
	else
	{
		scriptTag.innerHTML=factObj.ERRRORMESSAGE;		
	}

}

function callfunc(frm,callbackfunc,postcode,accountno)
 {
	sltfrm=frm.yell4postcode_drpAddress;
	id=sltfrm.value;
	//alert(basic_flgCounty);
	 var url="http://lookup.yell4postcode.co.uk/yell4postcode/inline3.ashx?idnos="+id+"&callback="+callbackfunc+"&accountnumber="+ accountno +"&postcode=" + postcode+'&flgCounty='+basic_flgCounty+'&CountyType='+basic_countydet; 
	//document.write(url)
	 

	
	scriptTag=getObj("maincontentspan");
	scriptTag.innerHTML="";
	
	headElement = document.getElementsByTagName("head").item(0);    
	if(getObj("showaddressfulldetails"))
	{
		scriptTag=getObj("showaddressfulldetails");
		headElement.removeChild(scriptTag);
	}
	
	
    var scriptTag = document.createElement("script");
    scriptTag.setAttribute("id", "showaddressfulldetails");
    scriptTag.setAttribute("type", "text/javascript");
    scriptTag.setAttribute("src", url);
    headElement.appendChild(scriptTag);
	
	
	
	
 }
 
 
function addOption(selectbox,text,value )
				{
					var optn = window.document.createElement("OPTION");
					optn.text = text;
					optn.value = value;
					selectbox.options.add(optn);
				}

//Function for Object with  type of Browser that are supported
function getObj(name)
{
	
  if (document.getElementById)
  {
 	this.obj = document.getElementById(name);	
  }
  else if (document.all)
  {
	this.obj = document.all[name];	
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];   	
  }
  return this.obj;
}