// Save values chosen into cookie and retrieve filtered form
// **************************************************************
function doSaveChosen (url, chosetype, changetype) {
	showNotification();
	var chvalue = changetype; // CHANGED VALUE
	var ctypev = document.getElementById('ctype').value; // ECAST
	if (chvalue == "change") {
		var cvalue = chosetype; 
		eraseCookie(chosetype); }
	else { 
		var cvalue = document.getElementById(chosetype).value; 
		createCookie(chosetype, cvalue, 1);}
	var postData = 'chosetype=' + chosetype + '&cvalue=' + cvalue + '&chvalue=' + chvalue + '&ctype=' + ctypev;
	//document.cookie = chosetype + "=" + cvalue;
	callAjax(url,'POST',postData,doListConfigCallback);
}

// Save configured part and/or user info
// **************************************************************
function doSaveConfigured (url, fields) {
	showNotification();
	var emailadd = document.getElementById("emaila").value;
	var postData = 'emailaddress=' + emailadd;
	if (fields == "yes")
	{
	var fname = document.getElementById('fname').value;
	var lname = document.getElementById('lname').value;
	var addr = document.getElementById('addr').value;
	var city = document.getElementById('city').value;
	var state = document.getElementById('state').value;
	var zip = document.getElementById('zip').value;
	var cname = document.getElementById('cname').value;
	postData = postData + '&fname=' + fname + '&lname=' + lname + '&addr=' + addr + '&city=' + city + '&state=' + state + '&zip=' + zip + '&cname=' + cname;
	}
	//document.cookie = "email=" + email;
	callAjax(url,'POST',postData,doListConfigCallback);
}

// Show Result
function doConfigResult (url) {
	showNotification();
	var postData = '';
	var base = readCookie('base');
	var spout = readCookie('spout');
	var handle = readCookie('handle');
	var outlet = readCookie('outlet');
	var ctypev = document.getElementById('ctype').value; // ECAST
	/*if (base == "" || spout == "" || handle == "" || outlet == "")
	{alert("All * are required");}
	else
	{*/	
	var postData = 'ctype=' + ctypev;	
	callAjax(url,'POST',postData,doListConfigCallback);//}
}

// Initial Configuration Set up/List
function doLoadInitialConfig(url) {
	showNotification();
	var ctypev = document.getElementById('ctype').value; // ECAST
	if (ctypev == "ECAST") {	document.cookie = "catalog=AB";}
	var postData = 'ctype=' + ctypev;
	callAjax(url,'POST',postData,doListConfigCallback);
}

//function checkCountry(country) checkCountry('{country}'); 
//{
//	if (country == "CA")
//	{
//		createCookie("country", country, 1);
//	}
//}

// CONFIGURATOR CALLBACK
// **************************************************************
function doListConfigCallback() {
	if (myRequest.readyState == 4) {
		if (myRequest.status == 200) {
			document.getElementById('configurator').innerHTML = myRequest.responseText;
		} else {
			alert("An error occurred: " + myRequest.statusText);
		}
	hideNotification();
	}
} 

// READ COOKIE
// **************************************************************
function readCookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return ""; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

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

function eraseAllCookies() {
	eraseCookie("catalog");
	eraseCookie("type");
	eraseCookie("mount");
	eraseCookie("center");
	eraseCookie("base");
	eraseCookie("spout");
	eraseCookie("handle");
	eraseCookie("outlet");
	eraseCookie("supply");
	eraseCookie("other");
	eraseCookie("finish");
	eraseCookie("potable");
	eraseCookie("power");
	eraseCookie("transformer");
	eraseCookie("secondary_transformer");
}

// Show USER NOTIFICATION icon while "working"
// **************************************************************
function getCheckedValue(radioObj) {
	var returnvalue;
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
}

// Show USER NOTIFICATION icon while "working"
// **************************************************************
function showNotification() {document.getElementById('notifier').innerHTML = "Filtering...";}
function hideNotification() {document.getElementById('notifier').innerHTML = '';}

// OPEN external window (for printer purposes)
// **************************************************************
function openWin(URL,width,height) {
   cprWindow = window.open(URL, "cprWin","width=" + width + ",height=" + height + ",scrollbars=yes,toolbar=no,menubar=yes");
   cprWindow.focus();
}
