function createCookie(name,value,days){
	if (value=="All Other States"){
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		eraseCookie(name);
		//value="National Editions";
		value="nationalproduct";
		document.cookie = name+"="+value+expires+"; path=/";
		window.location="/shopbystate.htm";
	}
	else if(value=="Select a State"){
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		eraseCookie(name);
		//value="National Editions";
		value="nationalproduct";
		document.cookie = name+"="+value+expires+"; path=/";
		window.location="/shopbystate.htm";
	}
	else{
		//alert(value);
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		//eraseCookie(name);
		document.cookie = name+"="+value+expires+"; path=/";
	}
}

var req;
function Initialize(){
	try
	{
		req=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			req=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			req=null;
		}
	}
	if(!req&&typeof XMLHttpRequest!="undefined")
	{
		req= new XMLHttpRequest();

	}
}

function SendQuery(key,submission,area){
	if (area!='productform'){
		if (key==null){
			key="nationalproduct";
		}
		//alert(submission);
		if (key.length>0) {
			Initialize(); 
			//alert(submission);
			if (submission==""){
				var url="/autocomplete.php?k="+key;
			}
			else{
				var url="/autocomplete.php?k="+key+"&submission="+submission;
			}
			if(req!=null)
			{
				req.onreadystatechange =function(){
					Process(area,key);
				}
				req.open("GET", url, true);
				req.send(null);
			}
		}
	}
	else{
			if (key.length>0) {
			Initialize(); 
			//alert(submission);
			var url="/autocompleteproduct.php?k="+key;
			if(req!=null){
				req.onreadystatechange =function(){
					Process(area,key);
				}
				req.open("GET", url, true);
				req.send(null);
			}
		}
	}
}
function Process(area,key){
	if (req.readyState == 4) {
		//alert(req.responseText);
		// only if "OK"
		if (area!='productform'){
			if (req.status == 200) {
					//alert(req.responseText);
					if (area=='reviewform'){				
						var obj=document.subjectreviewform.subjectreviewformselect;
						obj.options.length = 0;
						eval(req.responseText);
						var obj=document.subjectform.subject;
						obj.options.length = 0;
						eval(req.responseText);
					}
					else{
						var obj=document.subjectform.subject;
						obj.options.length = 0;
						eval(req.responseText);
					}
					//document.getElementById("name").innerHTML=req.responseText;
			}
			else
			{
				alert("There was a problem retrieving data:<br>"+req.statusText);
			}
		}
	}
}

function getProductListing(name){
	//alert('here');
	var key = readCookie(name);
	var x;
	//alert(key);
	if (key=="nationalproduct"){
		x = "national editions";
	}
	else{
		x = key;
	}
	document.getElementById('heading').innerHTML=document.getElementById('heading').innerHTML + " for " + toProperCase(x);
	if (key){
		if (key==null){
			key="nationalproduct";
		}
		if (key.length>0) {
			Initialize(); 
			var url="/productautocomplete.php?k="+key;
			if(req!=null){
				req.onreadystatechange =function(){
					ProcessProduct();
				}
				req.open("GET", url, true);
				req.send(null);
			}
		}
	}
}

function ProcessProduct(area,key){
	if (req.readyState == 4) {
		//alert(req.responseText);
		// only if "OK"
		if (area!='productform'){
			if (req.status == 200) {
					//alert(req.responseText);
					document.getElementById("products").innerHTML=req.responseText;
			}
			else{
				alert("There was a problem retrieving data:<br>"+req.statusText);
			}
		}
	}
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	//alert(ca.length);
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) {
			//alert(c.substring(nameEQ.length,c.length));
			return c.substring(nameEQ.length,c.length);
		}
//		else {
//			return "national product";
//		}
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function startCookie(name){
	var x = readCookie(name);
	if (x=="nationalproduct"){
		x="allother states";
	}
	if (x){
		//alert(x);
		var list=document.stateform.state;
		x=x.toLowerCase();
		for (var intI = 0; intI < list.length; intI++) {
			newlist=list.options[intI].text.toLowerCase();
			//reg = (/^\s+/g, ' ');
			//newlist=newlist.replace(reg,'');
			//alert(newlist);
			if (newlist==x) {
				list.options[intI].selected = true;
			}
		}
		//document.stateform.state.options[intI] = null;
	}
}

function startCookieForm(name){
	var x = readCookie(name);
	if (x){
		var list=document.Form.txtState;
		x=x.toLowerCase();
		for (var intI = 0; intI < list.length; intI++) {
			newlist=list.options[intI].text.toLowerCase();
			//reg = (/^\s+/g, ' ');
			//newlist=newlist.replace(reg,'');
			if (newlist == x) {
				list.options[intI].selected = true;
			}
		}
	}
}

function startCookieReviewForm(name){
	var x = readCookie(name);
	if (x){
		var list=document.reviewform.reviewstate;
		x=x.toLowerCase();
		for (var intI = 0; intI < list.length; intI++) {
			newlist=list.options[intI].text.toLowerCase();
			//reg = (/^\s+/g, ' ');
			//newlist=newlist.replace(reg,'');
			if (newlist == x) {
				list.options[intI].selected = true;
			}
		}
	}
}
function getCookie(name){
	var x=readCookie(name);
	//alert(x);
	if (x==null){
		x="nationalproduct";
	}
//	if (x=="nationalproduct"){
//		x="shopbystate";
//	}
	//else{
		x=x.toLowerCase();
		reg = (/^\s+/g, ' ');
		x=x.replace(reg,'')
		window.location="http://www.buckledown.com/"+ x +".htm";
	//}
}
function getCookieReviewCopy(name){
	var x=readCookie(name);
	if (x==null){
		window.location='/getareviewcopy.htm?state=1';
		startCookieReviewForm(x);
	}
	else{
		window.location='/getareviewcopy.htm?subject=1';
	}
}

function subjectNav(x){	
	x=x.toLowerCase();
	reg = (/^\s+/g, ' ');
	x=x.replace(reg,'')
	var c = readCookie('state_buckledown');
	if (c==null || c=="nationalproduct"){
		x="shopbystate";
	}
	//alert(x);
	window.location="http://www.buckledown.com/"+ x +".htm";
}

function startCookieSubject(name){
	var x = readCookie(name);
	if (x){
		var list=document.subjectform.subject;
		x=x.toLowerCase();
		for (var intI = 0; intI < list.length; intI++) {
		  newlist=list.options[intI].text.toLowerCase();
		  //reg = (/^\s+/g, ' ');
		  //newlist=newlist.replace(reg,'');
		  if (newlist == x) {
			list.options[intI].selected = true;
		  }
		}
	}
}

function toProperCase(s){
  return s.toLowerCase().replace(/^(.)|\s(.)/g, 
          function($1) { return $1.toUpperCase(); });
}
