function GetSoftAdCookie() {

	var sCurrentCookie = GetCookie("ASP.NET_SessionId");
	
	return sCurrentCookie;

}

function GetCookie(name) { // use: getCookie("name");
	var bikky = document.cookie;
	var index = bikky.indexOf(name + "=");
	if (index == -1) return null;
	index = bikky.indexOf("=", index) + 1; // first character

	var endstr = bikky.indexOf(";", index);
	if (endstr == -1) endstr = bikky.length; // last character
	return unescape(bikky.substring(index, endstr));
	
}

function SetCookie(name, value) {
         var argv = SetCookie.arguments;
         var argc = SetCookie.arguments.length;
         var expires = (argc > 2) ? argv[2] : null;
         var path = (argc > 3) ? argv[3] : "/"; //SETS COOKIE AT ROOT NOT PAGE. Previously was 'null'
         var domain = (argc > 4) ? argv[4] : null;
         var secure = (argc > 5) ? argv[5] : false;
         document.cookie = name + "=" + escape (value) +
         ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
         ((path == null) ? "" : ("; path=" + path)) +
         ((domain == null) ? "" : ("; domain=" + domain)) +
         ((secure == true) ? "; secure" : "");
}

function DeleteCookie(name) { 
	var exp = new Date(); 
	exp.setTime (exp.getTime() - 1); 
	// This cookie is history 
	var cval = GetCookie (name); 
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString(); 
} 

function preloadquizform() { 

	var bHasRightstoView = false;
	
	if(eval("opener")){
		var currentwindowlocation = window.location.href;
		var openerwindowlocation = opener.window.location.href;
		
		var currentwindowdomain = currentwindowlocation.replace("http://", "");
		var openerwindowdomain = openerwindowlocation.replace("http://", "");
		
		currentwindowdomain = currentwindowdomain.substring(0, currentwindowdomain.indexOf("/"));
		openerwindowdomain = openerwindowdomain.substring(0, openerwindowdomain.indexOf("/"));
		
		//alert(currentwindowdomain + ' ' + openerwindowdomain);
		
		if(currentwindowdomain ==openerwindowdomain){
			bHasRightstoView = true;
		}
	}
	
	if(bHasRightstoView == true){
	
		var ContactUsForm = document.forms.quizform;
		
		var HunterDouglasContactUsCookie = GetCookie("HunterDouglasContactUs");
	
		var sCurrentSoftADCookie = GetSoftAdCookie();	
		
		
		if (HunterDouglasContactUsCookie == sCurrentSoftADCookie) {
			
			ContactUsForm.firstname.value = (GetCookie("FirstName") == "") ? "" : GetCookie("FirstName");
			ContactUsForm.lastname.value = (GetCookie("LastName") == "") ? "" : GetCookie("LastName");
	
			
			ContactUsForm.address.value = (GetCookie("Address") == "" || GetCookie("Address") == null ) ? "" : GetCookie("Address");
			ContactUsForm.city.value = (GetCookie("City") == "" || GetCookie("City") == null) ? "" : GetCookie("City");
			if (GetCookie("State") != "" && GetCookie("State") != null) {
				for (var i = 0; i < ContactUsForm.state.length; i++) {
					if (ContactUsForm.state.options[i].value == GetCookie("State")) {
						ContactUsForm.state.options[i].selected = true;
						break;
					} 
				}		
			}
			
			ContactUsForm.zip.value = (GetCookie("Zip") == "" || GetCookie("Zip") == null) ? "" : GetCookie("Zip");
			ContactUsForm.email.value = (GetCookie("Email") == "" || GetCookie("Email") == null) ? "" : GetCookie("Email");
			ContactUsForm.phone.value = (GetCookie("Phone") == "" || GetCookie("Phone") == null) ? "" : GetCookie("Phone");
			ContactUsForm.mobilephone.value = (GetCookie("Mobile") == "" || GetCookie("Mobile") == null) ? "" : GetCookie("Mobile");
		}	
	}
	else{
		document.getElementById("maincontent").innerHTML  = "";
		alert("Sorry, this web site is not registered");
		window.close();
		return false;
	}
}

function Open360quiz(vanity,sitekey,pagekey) {

	var URL;
	
	if (vanity != "") {
		URL = "/" + vanity + "/sb.cn?crt=pagekey=" + pagekey + "&pageid=hdsitebuilder.360quiz";
	
	} else {
		URL = "/channelnet.aspx?cn=sitebuilder&act=view&crt=sitekey=" + sitekey + "%26pagekey=" + pagekey + "&pageid=sitebuilder.360quiz";
	
	}

	var popup = window.open(URL,"360Quiz","width=800,height=650,scrollbars=no,location=no,menubar=no,resizable=yes,toolbar=no,status=no");

}

function handleOver() { 
 if (document.images) document.images["360quiz"].src=img_on.src;
}

function handleOut() {
 if (document.images) document.images["360quiz"].src=img_off.src;
}

function SubmitQuizForm()
{
  var oForm = document.forms.quizform;
   if (document.getElementById('firstname').value.length < 1)
   {
       alert("First Name is required.");
       return false;
   } else if (document.getElementById('lastname').value.length < 1) {
       alert("Last Name is required.");
       return false;
    } else if  (isEmail(document.getElementById('email').value) == false) {
	alert("Email Address is invalid.");
       return false;
    } else if  (document.getElementById('contact').checked == true & document.getElementById('contact_phone').checked == true & document.getElementById('contact_phone_type').value == 'phone' & document.getElementById('phone').value.length < 1) {
       alert("Phone number is required because you've selected phone as your prefered contact method.");
       return false;
    } else if  (document.getElementById('contact').checked == true & document.getElementById('contact_phone').checked == true & document.getElementById('contact_phone_type').value == 'mobilephone' & document.getElementById('mobilephone').value.length < 1) {
       alert("Mobile phone number is required because you've selected mobile phone as your prefered contact method.");
       return false;
   } else {
   
	var a1 = "No";
	var a1_1 = "Please do not contact."; 

	oForm.hdnetsession.value = GetSoftAdCookie();
	if (document.getElementById('contact').checked == true) {		           		
		a1 = "Yes";
	} 

	for (var i = 0; i < oForm.contact_by.length; i++) {
		if (oForm.contact_by[i].checked) {
			if (oForm.contact_by[i].value == "email") {
				a1_1 = "Contact me by email";
			} else {

				if (oForm.contact_phone_type.options[oForm.contact_phone_type.selectedIndex].value == "phone") {

					a1_1 = "Call my home phone";

				} else {

					a1_1 = "Call my mobile phone";
				}
			}
		}            
	}		           		
	oForm.a1.value = a1;
	oForm.a1_1.value = a1_1;
	if(GetCookie("iMagine") !=null)
	    oForm.a6.value = "Yes"
	else
	    oForm.a6.value = "No"
	
	var expDays = 30; 
	var exp = new Date(); 	
	var cpath = "/";
	
	exp.setTime(exp.getTime() + (expDays*24*60*60*1000));	
	
	sSoftAdCookie = GetSoftAdCookie();
	SetCookie('HunterDouglasContactUs', sSoftAdCookie, exp);
	SetCookie('FirstName', oForm.firstname.value, exp); 
	SetCookie('LastName', oForm.lastname.value, exp); 
	SetCookie('Email', oForm.email.value, exp);
	SetCookie('Phone', oForm.phone.value, exp);
	SetCookie('Mobile', oForm.mobilephone.value, exp);
	SetCookie('Address', oForm.address.value, exp);
	SetCookie('City', oForm.city.value, exp);
	SetCookie('State', oForm.state.value, exp);
	SetCookie('Zip', oForm.zip.value, exp);		
	return true;
   }
}

function redircttoquiz() {

	document.forms.LifeTraitsQuiz.submit();

}

function firstToUpper(formobj)
{
	var caseCheck = formobj;
	var caseVal = caseCheck.value;
		
	if (caseVal != "")
	{
		var c = new RegExp("^[A-Z]");
		var r = c.exec(caseVal);
		
		if (r == null)
		{
			var CurVal = caseVal.substr(0,1);
			var RetVal = CurVal.toUpperCase();
			var NewVal = caseVal.replace(CurVal, RetVal);
			
			caseCheck.value = NewVal;
		}
	}
}

