/* THIS FILE CONTAINS FUNCTIONS FOR CREATING A TEMP COOKIE ON THE CLIENTS BROWSER
   IF THEY ARE COMING FROM .SHTML PAGES.
   JUST ADD THE FOLLOWING LINES OF CODE TO THE PAGE YOU WANT TO HAVE THIS FUNCTIONALITY ON:

<!-- Beginning of code -->
   <SCRIPT LANGUAGE="JavaScript">
	var SERVERIP = '<!--#echo var="LOCAL_ADDR"-->';
	var SOURCE = document.referrer;
   </SCRIPT>

   <SCRIPT LANGUAGE="JavaScript" src="tempid.js">
   </SCRIPT>   
<!-- End of code -->
   
*/

function addZeros(thisstring, thislength){
	if (thislength == '') thislength = 0;

	for (var i=0; i<thislength; i++){
		thisstring = "0" + thisstring;
	}

	thisstring = thisstring.substr(thisstring.length-thislength);

	return thisstring;
}

function getTempID() {
	SERVERIP = addZeros((SERVERIP.split("."))[3], 3);
	var random_num = (Math.random()*1000000);
	random_num = (""+random_num).substr(0,3);
	var thisdate = new Date();
	var today = "" + thisdate.getFullYear() + addZeros(thisdate.getMonth()+1, 2) + addZeros(thisdate.getDate(), 2);
	var now = "" + addZeros(thisdate.getHours(), 2) + addZeros(thisdate.getMinutes(), 2) + addZeros(thisdate.getSeconds(), 2);

	var thisid = "T" + today + now + SERVERIP + random_num;

	return thisid;
}

function setTempCookie(){
	var ID = getTempID();
	var expdate = new Date();
	expdate.setTime(expdate.getTime() + (1095*24*60*60*1000));

	var parts = new Array();
	parts = window.location.hostname.split(".");

	var domain = parts[parts.length-2]+"."+parts[parts.length-1];

	document.cookie = "temp="+escape(ID)+"; expires="+expdate.toGMTString()+"; domain=."+domain;
	document.cookie = "source="+escape(getSource())+"; expires="+expdate.toGMTString()+"; domain=."+domain;
	//alert("temp="+escape(ID)+"; expires="+expdate.toGMTString()+"; domain="+domain);
	//alert("source="+escape(getSource())+"; expires="+expdate.toGMTString()+"; domain="+domain);
	//alert(ID);
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1){
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg){
			return (getCookieVal(j));
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}

function getSource(){
	if (SOURCE != '' && SOURCE != undefined){
		SOURCE = SOURCE.replace("http://", "");
		SOURCE = SOURCE.replace(/\/.*/, "");
	
		var parts = SOURCE.split(".");
	
		SOURCE = parts[parts.length-2]+"."+parts[parts.length-1];
	} else {
		SOURCE = 'theknot.com';
	}
	
	return SOURCE;
}

function AuthenticateVsdRequest() {
	
	if (GetCookie("temp") == null){
			//If there is no temp cookie assume that user doesn't have cookies enabled.
			//temp cookie is set by global ISAPI filter, so all users should have one.
			//We can't send users who don't accept cookies through MSD as they will error
	
			return true;
	} else if (GetCookie("TMPAUTHTIX") != null && GetCookie("TMPAUTHTIX").indexOf("MsdVisit=1") != -1){
		//we've already been cookied for this browser session by the Master Security Domain (Msd)

		return true;
	} else if (location.search.indexOf("MsdVisit=1") != -1){
		//we've already visited the Master Security Domain (Msd) but a cookie was 
		//not available (perhaps client doesn't accept them)...just process request.
		
		return true; 
	} else {
		return false;
	}

}

function GetExternalSite() {
	var externalSite;
	
	if (GetCookie("Referrer") != null && GetCookie("Referrer") != ''){
		externalSite = GetCookie("Referrer"); 
	} else if (document.referrer != null && document.referrer != ''){
		externalSite = document.referrer;
	} else {
		externalSite = document.URL;
	}
	
	return externalSite;
}


// We are going to let ISAPI filter take care of setting visitor cookie
//if (GetCookie('temp') == null){
	//setTempCookie();
//}

//##      WE MUST CHECK MSD (MASTER SECURITY DOMAIN) ONCE EACH SESSION     ##
//##        MSD HANDLES SINGLE SIGN-ON PROCESS AND COOKIE VALIDATION       ##
//##   VSD (VANITY SITE DOMAIN) COOKIE WILL ONLY BE SET IF THEY VISTED MSD ##

if (! AuthenticateVsdRequest()){
	//## User Needs to be Authenticated
	
	//#get the external referrer
	var externalSite = GetExternalSite();
	var hostname_array = window.location.hostname.split('.');
	
	var landing_domain = 'www.'+hostname_array[hostname_array.length-2]+'.'+hostname_array[hostname_array.length-1];
	
	//#redirect to the master domain passing in the following
	//# 1. landing Url for redirect back to virtual security domain
	//# 2. original Url of requested page
	//# 3. external site that referred the user here

	window.location = 'http://sso.theknot.com/Secure/msdcheck.aspx?landingUrl=' + escape('http://'+landing_domain+'/Join/Security/Landing.aspx') + '&originalTarget=' + escape(document.URL) + '&externalSite=' + escape(externalSite);
	
}