function $G(id) { return document.getElementById(id); }
function GetXmlHttpObject() { return window.XMLHttpRequest ? new XMLHttpRequest() : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : null); }

function fetch(url, id)
{
    var xmlHttp;
    xmlHttp = GetXmlHttpObject();
    xmlHttp.onreadystatechange = function ()
	{
	    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
		{
		    if (xmlHttp.status == 200) 
			{
			    if (xmlHttp.responseText != "")
					{
						$G(id).style.visibility="visible";
						$G(id).innerHTML = xmlHttp.responseText;
					}
				else { $G(id).style.visibility="hidden"; }
			}
			else
			{
				$G(id).style.visibility="hidden";
			}
		}
};
url = url + "&sid=" + Math.random();
xmlHttp.open('GET', url, true);
xmlHttp.send(null); 
}

function getCityState(zipid, cityid, stateid)
{
//document.write(zipid + $G(zipid).value + $G(cityid).innerText  + $G(stateid).innerText );
	//if (($G(zipid).value).length == 5)
	//{
	var xmlHttp;
	var url;
	xmlHttp=GetXmlHttpObject();
	xmlHttp.onreadystatechange = function()
	
	{ $G('progress').className = "waiting"; 

		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			if(xmlHttp.status == 200) 
			{
				response  = xmlHttp.responseXML.documentElement;
				
                city = response.getElementsByTagName('City')[0].firstChild.data;
                state = response.getElementsByTagName('State')[0].firstChild.data;
                $G(cityid).value  = city;
                $G(stateid).value  = state;
                $G('progress').className = "notWaiting";
			}
			else {}
		} 
	};
url = 'zip.asmx/GetZip?zip=' + $G(zipid).value;
//url = 'zip.aspx?zip=' + $G(zipid).value;
url = url + "&sid=" + Math.random();
	xmlHttp.open('GET', url, true); 
	xmlHttp.send(null); 
//}
}

function validate_for_integers(inputfield, inputevent) {
	var key;
	var keychar;

	if (window.event) {
		key = window.event.keyCode;
	} else if (inputevent) {
		key = inputevent.which;
	} else {
		return true;
	}
	
	keychar = String.fromCharCode(key);
	// control keys
	if ((key==null) || (key==0) || (key==8) ||
    		(key==9) || (key==13) || (key==27)) {
		return true;

	// numbers
	} else if (("0123456789").indexOf(keychar) > -1) {
		return true;
		
	} else {
		return false;
	}
} // end of validate_for_integers()




function suggest()
{
	var curleft = curtop = 0;
	var obj = $G('thesearchbox');
	if (obj.offsetParent)
	{
		do
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
		while (obj = obj.offsetParent);
		curtop = curtop + $G('thesearchbox').offsetHeight;
		$G('suggestbox').style.top = curtop + 'px';
		$G('suggestbox').style.left = curleft + 'px';	
	}
	var str = escape($G('thesearchbox').value);
	fetch('services/getBrand.aspx?brand=' + str, 'suggestbox');
}

function suggest2()
{
	var curleft = curtop = 0;
	var obj = $G('txtBrand');
	if (obj.offsetParent)
	{
		do
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
		while (obj = obj.offsetParent);
		curtop = curtop + $G('txtBrand').offsetHeight;
		$G('suggestbox').style.top = curtop + 'px';
		$G('suggestbox').style.left = curleft + 'px';	
	}
	var str = $G('txtBrand').value;
	fetch('getBrands.aspx?brand=' + str, 'suggestbox');
}

//function selectBrand(brand) {$G('thesearchbox').value = brand;}

 function selectCigar(id) {
 //Document.Write(id);
 location.href="cigardetail.aspx?cid="+id;
 }
 
 function comment(id, length)
 {
 var obj = $G(id);
 if ((obj.value).length > 0)
 {
    $G('commentCount').innerHTML = (length - (obj.value).length + " remaining");
 }
   else { $G('commentCount').innerHTML = ""; }
   return true;
 }
 function comment2(id, length)
 {
 var obj = $G(id);
 if ((obj.value).length >= length)
 {
   event.returnValue=false;
   }
}


