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;

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,callbackfunc){
	postcode=postcode.replace(/ /g,"");
	document.getElementById('addrid').style.display = 'none';
	if(postcode=="")
	{
		alert("Please Enter Postcode");
		return false;
	}
	else if(callbackfunc=="")
	{
		alert("Please Enter callback function name");
		return false;
	}

	/*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='<img src="loading.gif"  border="0" />';


 var url = 'postcodelook.php?postcode=' + postcode + '&callback='+callbackfunc; 

	//document.write(url);
	//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)
{
	
	//alert(count);
	count++;
	scriptTag=document.getElementById("maincontentspan");
	
	//if ADDRESS IS FOUND
	if(factObj.CONDITION=="1")
	{
		
		
    scriptTag.innerHTML="<select name=\"yell4postcode_drpAddress\" size=\"10\" class=\"input\" id=\"yell4postcode_drpAddress\" style=\"width:397px;\" onclick=\"callfunc(this.form,'"+factObj.CALLBACK_FUNCTION+"','"+factObj.POSTCODE+"');\" ></select>";
	
	
		var addressshow=factObj.SELECTADDRESSSHOW.split("@@@@@");
		
		var selectedbox_cntrl=document.getElementById("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;		
		scriptTag.innerHTML="No records found for the postcode provided.";	
	}

}

function callfunc(frm,callbackfunc,postcode)
 {
	sltfrm=frm.yell4postcode_drpAddress;
	id=sltfrm.value;
	
	var url="postcodelook.php?idnos="+id+"&callback="+callbackfunc+"&postcode=" + postcode;



	
	scriptTag=document.getElementById("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;
}