var cookie_today = new Date();
var cookie_expires = new Date(cookie_today.getTime() + (56 * 86400000));

function hideStatus() {
	if (document.layers && document.layers['StatusDisplay'])
		document.layers['StatusDisplay'].visibility = 'hidden';
	else if (document.all)
		if (document.all['StatusDisplay']) {
			document.all['StatusDisplay'].style.visibility = 'hidden';	
		}
}

function ClearForm(what) {
	document.location = document.location.href;
}

function getURLWithQS() {
	var URL = getURLWithoutQS();
	var QString = getQueryString(true);
	return URL + "?" + QString;	
}

function getURLWithEscapedQS() {
	var URL = getURLWithoutQS();
	var QString = getQueryString(false);
	return URL + "?" + escape(QString);	
}

function sendForm(media) {
	document.demoLaunch.qewEscEndpoint.value = media;
	document.demoLaunch.qewEscStartURL.value = getURLWithQS();
	document.demoLaunch.qewRedirectURL.value = "escalate.jsp" + 
		escape("?qewTenant=" + document.demoLaunch.qewTenant.value + 
		"&qewEscEndpoint=" + document.demoLaunch.qewEscEndpoint.value);
	if (Get_Cookie('cc') == 1) {
  		alert("You are already in Live Chat!");
	} else {
		document.demoLaunch.submit();
	}
}

function writeURLandQSToCookie() {
	var myQS = getURLWithEscapedQS();
	//alert(myQS);
	document.cookie = "redirectURL=" + myQS + ";path=/;expires=" + cookie_expires.toGMTString();
}

function testCookie() {
	alert(document.cookie);
}

function getQueryString(includeChat) {
	var myQS = "";
  	for (var x=0, y=document.forms.length; x<y; x++) {
		if (document.forms[x].name != "demoLaunch" && document.forms[x].name != "frmLogin" ) {
		  	for (var i=0, j=document.forms[x].elements.length; i<j; i++) {
				doit = false;
      				myType = document.forms[x].elements[i].type;
				myName = "";
				myValue = "";
				if (myType == 'hidden' || myType == 'password' || myType == 'text' || myType == 'textarea') {
					myName = document.forms[x].elements[i].name;
					myValue = document.forms[x].elements[i].value;
					doit = true;
				}
				if (myType == 'radio' || myType == 'checkbox') {
					if (document.forms[x].elements[i].checked) {
						myName = document.forms[x].elements[i].name;
						myValue = document.forms[x].elements[i].value;
						doit = true;
					}
		        	}
				if (myType == 'select-one' || myType == 'select-multiple') {
					myName = document.forms[x].elements[i].name;
					//myValue = document.forms[x].elements[i].value;
					myValue = document.forms[x].elements[i].options[document.forms[x].elements[i].selectedIndex].value;
					doit = true;
		        	}
				if (doit) {
					if (myValue != "") {
						if (myQS == "") {
							myQS = escape(myName) + "=" + escape(myValue);
						} else {
							myQS = myQS + "&" + escape(myName) + "=" + escape(myValue);
						}
					}
				}
			}
		}
	}
	//Append a value saying we are in chat to the querystring
	if (includeChat) {
		if (myQS == "") {
			myQS = myQS + "InChat=1";
		} else {
			myQS = myQS + "&InChat=1";
		}
	}
	//Append the Session Key Value to the QS
	//if (myQS == "") {
	//	myQS = myQS + "?setSessionKey=" + escape(getSessionKey());
	//} else {
	//	myQS = myQS + "&setSessionKey=" + escape(getSessionKey());
	//}
	return myQS;
}

function getURLWithoutQS() {
	var questionmarkPOS = document.location.toString().indexOf("?");
	if (questionmarkPOS == -1) {
		myURL = document.location;
	} else {
		myURL = document.location.toString().substr(0, questionmarkPOS);
	}
	return myURL;
}

function Get_Cookie(name) {
	var start = document.cookie.indexOf(name+"=");
	var len = start+name.length+1;
	if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
	if (start == -1) return null;
	var end = document.cookie.indexOf(";",len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len,end));
}

