// JavaScript Document

//spg   -- serverpage
//eid   -- elementid
function getAJAX(spg, eid)
{
var xmlhttp=false;

 try {  xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');   }
    catch (e) 
    {
        try {   xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');    }
        catch (e2) 
        {
          try {  xmlhttp = new XMLHttpRequest();     }
          catch (e3) {  xmlhttp = false;   }
        }
     }
	 
	if(!xmlhttp && typeof XMLHttpRequest !='undefined'){
		xmlhttp = new XMLHttpRequest();
	}

	var obj = document.getElementById(eid);
	
	serverPage = spg;
	xmlhttp.open("POST", serverPage, true);
	
	xmlhttp.onreadystatechange  = function()
    { 
         if(xmlhttp.readyState  == 4)
         {
              if(xmlhttp.status  == 200) 
                  obj.innerHTML=xmlhttp.responseText; 
              else 
                 obj.innerHTML="Error code " + xmlhttp.status;
         }
    } 
	xmlhttp.send(null);
}

function setCookie(name, value)
{
	var d = new Date();
	var t = new Date(d.getTime() + 60*60*24*365*5);
	t = t.toGMTString();
	document.cookie = name + "=" + escape(value) + ";expires=" + t + ";path=/;domain=.theglutensolution.com";
}

function getSubcat(index)
{
	getAJAX("subcat.php?id="+index, "subcat");
}

function getSubcat2(index)
{
	getAJAX("search_subcat2.php?id="+index, "subcat2");
}
	
function getProvince(index)
{
	getAJAX( "province.php?id="+index, "subcat");
}

function getProvince2(index)
{
//	getAJAX( "province.php?id="+index, "subcat");
}

function getProvince3(index)
{
	getAJAX( "province3.php?id="+index, "subcat");
}

function getCity(index)
{
	getAJAX( "city.php?id="+index, "subcat2");
}

function getCity3(index)
{
	getAJAX( "city3.php?id="+index, "subcat2");
}

function getSubbrand(index)
{
	getAJAX("subbrand.php?id="+index, "subbrand");
}

function getSubbrand2(index, bcode)
{
	getAJAX("subbrand.php?id="+index+'&bcode='+bcode, "subbrand");
}

function getProvince2(index)
{
	getAJAX("province2.php?id="+index, "subcat3");
}

//function getCitiesOfRetailer(index)
function getCitiesOfRetailer(index, city)
{
	getAJAX("CitiesOfRetailer.php?pid="+index+"&city="+city, "Cities");
}

function getSubcatEdMem(index)
{
	getAJAX("subcatEdMem.php?id="+index, "subcatEdMem");
}

function getProvinceEdMem(index)
{
	getAJAX("province.php?id="+index, "subcatEdMem");
}

var scrolling = false;

function getCity4(region, city)
{
	if (!scrolling)
		getAJAX("getCity4.php?region="+region+"&city="+city, "cities");
}

function checkFocus()
{
	var dropDown = document.getElementById('cities');
	var table = document.getElementById('city2');
	if (table.selectedIndex == -1)
		dropDown.innerHTML = "";
}

function checkKey(event)
{
	var key;
	if (window.event)
		key = window.event.keyCode;
	else if (event)
		key = event.which;
	
	if (key == 38)
		scroll(-1);
	else if (key == 40)
		scroll(1);
	else if (key == 13)
	{
		AJAXClick();
		return false;
	}
	else
		scrolling = false;
}

function scroll(dir)
{
	scrolling = true;
	var sel = document.getElementById('city2');
	var index;
	
	index = sel.selectedIndex + dir;
	if (index < 0)
		index = sel.size - 1;
	else if (index >= sel.size)
		index = 0;
	
	sel.options[index].selected = true;
}

function AJAXClick()
{
	document.getElementById('City').value = document.getElementById('city2').value;
	document.getElementById('cities').innerHTML = '';
}