﻿// JScript File
var xmlHttpnew=null;
var _Curr = -1;
var _Currpcode=-1;
var NUMBER_OF_RESULTS = 10;
var NUMBER_OF_RESULTSpcode = 10;
var availableResults = 0;
var availableResultspcode = 0;
var CurrentEventName="";
var CurrentEventId="";
var GSearchstr="";
var resultspcode = new Array();
var results= new Array();
var CurrentCity="";
var CurrentCountry="";
var NoHide;
var chkEnter='y';
var city="";
var Country="";
function GetXmlHttpObjectNew()
{                
    try   
    { 
        // Internet Explorer
        xmlHttpnew=new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e)
    {              
        try
          {
            xmlHttpnew=new ActiveXObject("Microsoft.XMLHTTP");
          }
        catch (e)
          {
            // Firefox, Opera 8.0+, Safari
            xmlHttpnew=new XMLHttpRequest();
          
          }
    }

    if(!xmlHttpnew&&typeof XMLHttpRequest != 'undefined')
        {
            xmlHttpnew = new XMLHttpRequest();
        }
      
    return xmlHttpnew;
}

function reloadInfo(elem){
  
   elem.style.backgroundColor="Transparent";
   elem.style.color="#cccccc";

}

function fnSelectedElement(elem){
    
	elem.style.backgroundColor="#cccccc";
    elem.style.color="#FFFFFF";
}

function refillTextBox(currObj){
	var tokens; 
	var tokenisedInput = "";
	if ( CurrentEventName=="tbxHintpcodeType")
	 tokens = document.getElementById("pcode").value.split(",");
	else
	tokens = document.getElementById("Store").value.split(",");
	
	for(var k=0; k<tokens.length; k++){
		if(k != (tokens.length - 1)){
		
			tokenisedInput += Trim(tokens[k]) + ",";
		}
	}
	
	tokenisedInput += stripTags(currObj.innerHTML);
	if ( CurrentEventName=="tbxHintpcodeType")
	{
	document.getElementById("pcode").value = tokenisedInput;
	document.getElementById("pcode").focus();
	}
	else
	{
	document.getElementById("Store").value = tokenisedInput;
	document.getElementById("Store").value=document.getElementById('Store').value.replace("&amp;", "&") ;
	document.getElementById("Store").focus();
	}
}

function stripTags(str){
	var stripped = str.replace(/(<([^>]+)>)/ig,"");
	return stripped;
}
function fnClose()
{
  if (NoHide==0)
  {
   clearDropDownList();
  }
}
function clearDropDownList(){

	try{
	     
	      document.getElementsByTagName("body")[0].removeChild(document.getElementById(CurrentEventName));
				
	}catch(e){}
	
}
function xstooltip_show(tooltipId, parentId, posX, posY,wdT)
{
    it = document.getElementById(tooltipId);
    
    if ((it.style.top == '' || it.style.top == 0) 
        && (it.style.left == '' || it.style.left == 0))
    {
       
        it.style.width = it.offsetWidth + 'px';
        it.style.height = it.offsetHeight + 'px';
        
        img = document.getElementById(parentId); 
        
        posY=img.offsetHeight ;        
       
        if (posX + it.offsetWidth > img.offsetWidth) posX = img.offsetWidth - it.offsetWidth;
        if (posX < 0 ) posX = 0;
        if(navigator.appName.indexOf("Exp")>=0) //IE
        {
            it.style.width=wdT;
            posX=1;
	      x = xstooltip_findPosX(img) + posX;
        y = xstooltip_findPosY(img) + posY;
        
        it.style.top = y+1 ;
        it.style.left = x ;
        }
        else  if(BrowserDetect.browser.indexOf("Safari")>=0) //IE
        {
        
         posX=0;
	
	  it.style.width= wdT;
	   x = xstooltip_findPosX(img) + posX;
        y = xstooltip_findPosY(img) + posY;
        
        it.style.top = y + 'px';
        it.style.left = x + 'px';
        }
	else
	{
        
         posX=0;
	   x = xstooltip_findPosX(img) + posX;
        y = xstooltip_findPosY(img) + posY;
        
        it.style.top = y + 'px';
        it.style.left = x + 'px';
        }

        
     
      
    }
    
    it.style.visibility = 'visible'; 
}
function xstooltip_findPosY(obj) 
{
    var curtop = 0;
    if (obj.offsetParent) 
    {
        while (obj.offsetParent) 
        {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}
function xstooltip_findPosX(obj) 
{
  var curleft = 0;
  if (obj.offsetParent) 
  {
    while (obj.offsetParent) 
        {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}
function xstooltip_hide(id)
{
    it = document.getElementById(id); 
    it.style.visibility = 'hidden'; 
}

///////////////////////////////////////////////// Store Drop Down////////////////////////////////////////////////
function init(ev){
  
    
	var key = (window.event) ? window.event.keyCode : ev.keyCode;
	var RETURN = 13;
	var TAB = 9;
	var ESC = 27;
	var bubble = true;
	var Obj;
        NoHide=1;
	  document.getElementById("Store").className="textbox1change";

     if (results.length==0  || CurrentCountry!=Country.options[Country.selectedIndex].value)
                 {
                    
                    
                    CurrentCountry=Country.options[Country.selectedIndex].value; 
                   fillresultStore();
                 }
     chkTempClick='y';
     
	switch(key){
		case RETURN: 
		             chkTempClick='n';
		             chkKB="y";
		             clearDropDownList();
					 //alert("Hello");
			          CheckCity("false");
		              //document.getElementById("btnFind").click();

			         break;
		case TAB:     try{Obj =  document.getElementById("tbxHintStore");}catch(e){}
		           
                    if(Obj)
                    {
			   
                        clearDropDownList();
                    }  
		case ESC:
			bubble = false;
			clearDropDownList();
			break;
	}
	return bubble;
}
function suggestStore(ev){
	var key = (window.event) ? window.event.keyCode : ev.keyCode;
	var ARRUP = 38;
	var TAB = 9;
	var ARRDN = 40;
	var bubble = true;
     CurrentEventName="tbxHintStore";
     CurrentEventId="tdStore";
    if (key==27 ||key==13) 
     return;
	switch(key){
		case ARRUP:
		if (document.getElementById("tbxHintStore").innerHTML!="")
		{
			changeSelectedElement("up");
		}
			bubble = false;
			break;
		case ARRDN:
		if (document.getElementById("tbxHintStore").innerHTML!="")
		{
			changeSelectedElement("down");
		}
			bubble = false;
			break;
		case TAB:     try{Obj =  document.getElementById("tbxHintStore");}catch(e){}
		           
                    if(Obj)
                    {
			   
                        clearDropDownList();
                    }  
			      break;
		default:
			      
		         if (results.length==0  || CurrentCountry!=Country.options[Country.selectedIndex].value)
                 {
                    
                    
                    CurrentCountry=Country.options[Country.selectedIndex].value; 
                   fillresultStore();
                 }
                 else
                 {
			       getSuggestionsStore(document.getElementById("Store").value);
	         } 
	}
	return bubble;
}
function changeSelectedElement(key)
{
	var obj = false;
	try{obj = document.getElementById(CurrentEventName);}catch(e){}
	if(obj)
	{
			var currElem = CurrentEventId + "0";
			var currObj = false;
			try{currObj =  document.getElementById(currElem);}catch(e){}
			if(currObj)
			{
				var total=NUMBER_OF_RESULTS;

				for(var i=0; i<=total-1; i++)
				{
					try{
						reloadInfo(document.getElementById(CurrentEventId + i));
					}catch(e){}
				}

				if(key == "up")
				{
					if(_Curr != -1)_Curr -= 1;
				}
				else if(key == "down")
				{
					if(_Curr != total-1){_Curr += 1;}
				}
				currElem = CurrentEventId + _Curr;
				
				try{currObj = document.getElementById(currElem);}catch(e){}
				if(currObj)
				{   
					fnSelectedElement(currObj);
					refillTextBox(currObj);
				}
			}
			else
			{
				try{reloadInfo(document.getElementById(CurrentEventId + _Curr));}catch(e){}
				if(_Curr > 0){_Curr -= 1;}else{_Curr = -1;}
			}
	}
	
}
function fillresultStore()
{
  
  
  
  
 try {Country=document.getElementById("Country");} catch(e){ alert("dfdfd");}  
  if (Country)
  {
   
    xmlHttpnew=GetXmlHttpObjectNew()
    if (xmlHttpnew==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    } 
    	var dt =new Date();
	var dtime=dt.getSeconds();
    var url='DropdownStoreName.php';
      url= url + "?Country=" + Country.options[Country.selectedIndex].value+"&dt="+dtime;   
    //alert(url);
    xmlHttpnew.onreadystatechange=ShowStoreSuggestions;
    xmlHttpnew.open("GET",url,true);
    xmlHttpnew.send(null);
  } 
}
function ShowStoreSuggestions()    
 {
   
    if (xmlHttpnew.readyState==4)
    {                
        
        if(xmlHttpnew.responseText=="")
        {
            
        }

    
        _Curr = -1;
        results = new Array();
        var GData=xmlHttpnew.responseText;
        var GetData=GData.split(",");
	//alert(GetData);
	     var isstorename=0;
		// alert(CurrentCity);
        for (var i=0;i<GetData.length;i++)
        {
               results[i] = GetData[i]; 
			   if (CurrentCity==trim(results[i]))
			   isstorename=1;
        }
         if(isstorename==0)
		 {    //alert("fdfdf");
			 createCookie("store","",365);
			 document.getElementById("Store").value="ENTER STORE NAME";
		 }
		 else
		 {
			 document.getElementById("Store").value=CurrentCity;
		 }
        availableResults = results.length;
		CheckCity('falseOrderBy');
        if (results.lenght>0 && NoHide==1)
        {
          getSuggestionsStore(document.getElementById("Store").value);
        }
    }
}
function getSuggestionsStore(str){

   
    ///################## New Cord For Multiple Search###########################////
    var NewStr="";
    _Curr=-1;
    if (str.length==0)
      { 
          clearDropDownList();
          return;
      }
        str=trim(str);
        if(str!="")
        createDropDown(results,"Store","tbxHintStore","tdStore",str,"215px","xstooltip");
    
 }
 
 function createDropDown(results, ParentName,ChildName,SubChild,str,ParentLeftPosition,ClassName){
//	var y = posY("tbxFind");
//	var x = posX("tbxFind");
//	var w = bObject("tbxFind").clientWidth;

	try{
		document.getElementsByTagName("body")[0].removeChild(document.getElementById(ChildName));
	}catch(e){}

	var div = document.createElement("div");
	div.id = ChildName;
	div.className=ClassName;
	div.setAttribute('onmouseout','javascript:fnClose();');
	div.style.position = "absolute";
	document.getElementsByTagName("body")[0].appendChild(div);
	div.innerHTML = "";
	xstooltip_show(ChildName, ParentName,0,0,ParentLeftPosition);
    var k=0; 
    
       /* if (str.length>1)
        {
          if (ChildName=="tbxHintStore")
          {
              var newstr=str.split(",");
              if (newstr.length>1)
              {
               str=newstr[newstr.length-1];
              }
              else
              {
               str=newstr[0];
              }
          }
         str=str.substring(0,1).toUpperCase() + str.substring(1,str.length);
        }
        else
        {*/
          str=str.toUpperCase();
       // }
	   temparr1=new Array();
	   temparr2=new Array();
	   Mainresults=results;
	  // alert("Values :       " + Mainresults);
	   for(var ii=0,t1=0,t2=0; ii<results.length; ii++)
	   {
	      var newstr2=trim(results[ii]);
	 // alert(newstr);
         if  (newstr2.substring(0,str.length)==str)
		 {
			temparr1[t1]=results[ii];
			t1++;
		 }
		 else
		 {
			 temparr2[t2]=results[ii]; 
			 t2++;
		 }
	   }
	   results=new Array();
	   temparr1.sort();
	   temparr2.sort();
	   //alert("first: "+ temparr1);
	   //alert("Second :" + temparr2);
	   var ti=0;
	   var t2i=0; 
	   if(temparr1.length)
	   {
			for( ti=0;ti<temparr1.length;ti++)
			{ 
				 results[ti]=temparr1[ti];
			}
	   }
	   if(temparr2.length)
	   {
			for( t2i=0;t2i<temparr2.length;t2i++,ti++)
			{
				results[ti]=temparr2[t2i];
			}
	   }
		 //alert("Third :" + results);
   var show=false;
   var total;
   NUMBER_OF_RESULTS=10;
    if (ChildName=="tbxHintStore")
   	 total = NUMBER_OF_RESULTS >= availableResults ? availableResults : NUMBER_OF_RESULTS;
    else
	 total = NUMBER_OF_RESULTSpcode >= availableResultspcode ? availableResultspcode : NUMBER_OF_RESULTSpcode;
   var count=0;
  
	for(var i=0; i<results.length; i++){
	 var newstr=trim(results[i]);
	 // alert(newstr);
         if  (newstr.substring(0,str.length)==str &&  count<=parseInt(total))
		 {
			
            if (ChildName=="tbxHintStore")
	        {var mouseaction = "onmouseover='reloadInfoAll(); ";}
	        else
	        {var mouseaction = "onmouseover='reloadInfoAllpcode(); ";}
	        
	          mouseaction =mouseaction + "fnSelectedElement(this);_Curr="+ k +";' onmouseout='reloadInfo(this); ' onclick='refillTextBox(this);clearDropDownList()'";
	        div.innerHTML += "<div style='cursor:pointer;width:auto;color:#cccccc; font-family:Arial ;font-size:12px;' id='" + SubChild + k + "' " + mouseaction + ">" + "<b>"   + newstr.substring(0,str.length) + "</b>" + newstr.substring(str.length, newstr.length) + "</div>";
	        k++;
	        show=true;
		 count=parseInt(count)+1;
         }
		 
		 else if (CheckStoreName(newstr,str)==true && count<=parseInt(total) )
         {
		 
            if (ChildName=="tbxHintStore")
	        {var mouseaction = "onmouseover='reloadInfoAll(); ";}
	        else
	        {var mouseaction = "onmouseover='reloadInfoAllpcode(); ";}
	        
	          mouseaction =mouseaction + "fnSelectedElement(this);_Curr="+ k +";' onmouseout='reloadInfo(this); ' onclick='refillTextBox(this);clearDropDownList()'";
	        div.innerHTML += "<div style='cursor:pointer;width:auto;color:#cccccc; font-family:Arial ;font-size:12px;' id='" + SubChild + k + "' " + mouseaction + ">" +MakeBoldStoreName(newstr,str) + "</div>";/*"<b>"   + newstr.substring(0,str.length) + "</b>" + newstr.substring(str.length, newstr.length) + "</div>";*/
			
	        k++;
	        show=true;
		 count=parseInt(count)+1;
         }
	}
	//results=Mainresults;
	//alert(results);
	NUMBER_OF_RESULTS=count;
	div.style.height = parseInt(15*count) + "px";
    if(results.length == 0 ||show==false){
		div.style.display = "none";
	}else{
		div.style.display = "block";
	}
}
function MakeBoldStoreName(DataVal,strval)
{
	
var DataArr=DataVal.split(' ');
var newstr1;
var boldstr="";
for(var i=0;i<DataArr.length;i++)
{
	newstr1=DataArr[i];
	
	 if (newstr1.substring(0,strval.length)==strval)
	 {
		break;
	 }
}
//alert(i);
for(var j=0;j<DataArr.length;j++)
{
	newstr1=DataArr[j];
	
	 if (i==j)
	 {
	boldstr+="<b>"   + newstr1.substring(0,strval.length) + "</b>"+ newstr1.substring(strval.length, newstr1.length)+' ';
	 }
	 else
	 {
		 boldstr+=newstr1 + ' ';
	 }
	 
}
boldstr=boldstr.substring(0,boldstr.length-1)

return boldstr;
}

function CheckStoreName(DataVal,strval)
{
	
var DataArr=DataVal.split(' ');
var newstr1;
for(var i=0;i<DataArr.length;i++)
{
	newstr1=DataArr[i];
	
	 if (newstr1.substring(0,strval.length)==strval)
	 {
		 //alert(newstr1 +"   :   " + strval);
	   return true;
	 }
}
//alert(newstr1 +"   :   " + strval);
return false;
}
function reloadInfoAll(){
	var total = NUMBER_OF_RESULTS ;
	 
	for(var i=0; i<total; i++){
		try{
			reloadInfo(document.getElementById(CurrentEventId + i));
		}catch(e){}
	}
	
	
}

 
/////////////////////************************** pcode Type*******************************///////////////////////

var xmlHttpnewpcode=null;
function GetXmlHttpObjectNewpcode()
{                
    try   
    { 
        // Internet Explorer
        xmlHttpnewpcode=new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e)
    {              
        try
          {
            xmlHttpnewpcode=new ActiveXObject("Microsoft.XMLHTTP");
          }
        catch (e)
          {
            // Firefox, Opera 8.0+, Safari
            xmlHttpnewpcode=new XMLHttpRequest();
          
          }
    }

    if(!xmlHttpnewpcode&&typeof XMLHttpRequest != 'undefined')
        {
            xmlHttpnewpcode = new XMLHttpRequest();
        }
      
    return xmlHttpnewpcode;
}
function initpcode(ev){
	var key = (window.event) ? window.event.keyCode : ev.keyCode;
	var RETURN = 13;
	var TAB = 9;
	var ESC = 27;
	var bubble = true;
	var Obj;
	 
     
 document.getElementById("pcode").className="textbox2change";
	switch(key){
		case RETURN: 
		             clearDropDownList();
		               // finddata1();
					  /*  var Country=document.getElementById("Country" );
					   if(Country.value=="All")
						{
							alert("Select country from drop down then enter city or postcode");
							if(trim(document.getElementById("pcode").value)=="ENTER CITY OR POSTCODE" || document.getElementById("pcode").value=="" )
	                        {
								
								document.getElementById("pcode").value=="ENTER CITY OR POSTCODE";
							}
							else
							{
								//alert(readCookie("pcode")+"------------"+trim(document.getElementById("pcode").value))
								if (readCookie("pcode")!=null )
								document.getElementById("pcode").value=readCookie("pcode");
								else
								document.getElementById("pcode").value="ENTER CITY OR POSTCODE";
								
							}
							
							document.getElementById("Country" ).focus();
						  document.getElementById("pcode" ).disabled="disabled";
						  return false;
						}
						else
						{
						  document.getElementById("pcode" ).disabled=false;
						}
	*/
			       CheckCity("false");
		             bubble = false;
			         break;
		case TAB:  document.getElementById("btnFind").focus();
			  try{ Obj =  document.getElementById("tbxHintStore");}catch(e){}
                    if(Obj)
                    {
                      clearDropDownList();  

                    } 
			  
                    bubble = true;
		case ESC:
			bubble = false;
			clearDropDownList();
			break;
	}
	return bubble;
}
function suggestpcode(ev){
	var key = (window.event) ? window.event.keyCode : ev.keyCode;
	var ARRUP = 38;
	var ARRDN = 40;
	var bubble = true;

	Country=document.getElementById("Country" );
    CurrentEventName="tbxHintpcodeType";
    CurrentEventId="tdpcodeType";
  //alert("suggestpcode");
    if (key==27 ) 
     return;
	switch(key){
		case ARRUP:
		if (document.getElementById("tbxHintpcodeType").innerHTML!="")
		{
		    
			changeSelectedElementpcode("up");
	    }
			bubble = false;
			break;
		case ARRDN:
		if (document.getElementById("tbxHintpcodeType").innerHTML!="")
		{
		
			changeSelectedElementpcode("down");
	    }
			bubble = false;
			break;
		default:
	
		            if (resultspcode.length==0 || CurrentCountry!=Country.options[Country.selectedIndex].value)
                     {
                       
                       CurrentCity=city.options[city.selectedIndex].value; 
                       CurrentCountry=Country.options[Country.selectedIndex].value; 
                       fillpcodeTypeResult();
                        
                     }
                     else
                     {
			 if (key!=9) 
		          getSuggestionspcode(document.getElementById("pcode").value);
			 else
			 {
				 try{ Obj =  document.getElementById("tbxHintpcodeType");}catch(e){}
                    		 if(Obj)
                    		{
 		                     clearDropDownList();  
                		}  
			 }
	             } 
	}
	return bubble;
}
function reloadInfoAllpcode(){
	var total = NUMBER_OF_RESULTSpcode >= availableResultspcode ? availableResultspcode : NUMBER_OF_RESULTSpcode;
	
	for(var i=0; i<total; i++){
		try{
			reloadInfo(document.getElementById(CurrentEventId + i));
		}catch(e){}
	}

}

function changeSelectedElementpcode(key)
{
	var obj = false;
	try{obj = document.getElementById(CurrentEventName);}catch(e){}
	if(obj)
	{
			var currElem = CurrentEventId + "0";
			var currObj = false;
			try{currObj =  document.getElementById(currElem);}catch(e){}
		
		
			if(currObj)
			{
				var total=NUMBER_OF_RESULTSpcode >= availableResultspcode ? availableResultspcode : NUMBER_OF_RESULTSpcode;
				
	
				
				for(var i=0; i<=total-1; i++)
				{
					try{
						reloadInfo(document.getElementById(CurrentEventId + i));
					}catch(e){}
				}

				if(key == "up")
				{
					if(_Currpcode != -1)_Currpcode -= 1;
				}
				else if(key == "down")
				{
					if(_Currpcode != total-1){_Currpcode += 1;}
					 
				}
				currElem = CurrentEventId + _Currpcode;
				
				try{currObj = document.getElementById(currElem);}catch(e){}
				if(currObj)
				{   
				   
					fnSelectedElement(currObj);
					refillTextBox(currObj);
				}
			}
			else
			{
				try{reloadInfo(document.getElementById(CurrentEventId + _Currpcode));}catch(e){}
				if(_Currpcode > 0){_Currpcode -= 1;}else{_Currpcode = -1;}
			}
	}
	
}

function fillpcodeTypeResult()
{
    xmlHttpnewpcode=GetXmlHttpObjectNewpcode();

    if (xmlHttpnewpcode==null)
      {
          alert ("Your browser does not support AJAX!");
          return;
      } 
    
    Country=document.getElementById("Country");
   	var dt =new Date();
	var dtime=dt.getSeconds();
    var url='Dropdownpcode.php';
      url= url + "?Country=" + Country.options[Country.selectedIndex].value +"&dt="+dtime;   
     
       xmlHttpnewpcode.onreadystatechange=ShowpcodeSuggestions;
       xmlHttpnewpcode.open("GET",url,true);               
       xmlHttpnewpcode.send(null);
}
function ShowpcodeSuggestions()    
 {
    
    if (xmlHttpnewpcode.readyState==4)
    {                
        
        if(xmlHttpnewpcode.responseText=="")
        {
           
        }
       
        _Currpcode = -1;
         resultspcode= new Array();
       
            var GData=xmlHttpnewpcode.responseText;
	   
            var GetData=GData.split(",");
	   // alert(GetData);
            for (var i=0;i<GetData.length;i++)
            {
                resultspcode[i] = GetData[i] ;
              
            }
            availableResultspcode = resultspcode.length;
     // alert(availableResultspcode); 
        if (resultspcode.length>0 && NoHide==1)
        {
         getSuggestionspcode(document.getElementById("pcode").value);
        }
    }
}
function getSuggestionspcode(str){
     _Currpcode=-1;
     //alert(resultspcode.length);
    if (str.length==0)
      { 
          clearDropDownList();
          return;
      }
        str=trim(str);
//	alert(resultspcode);
        if(str!="")
            createDropDown(resultspcode,"pcode","tbxHintpcodeType","tdpcodeType",str,"72px","xstooltippcode");
     
 }

