// CargoSmart Cookie Management JScript

// Variable to hold CargoSmart domain name

var cargosmart_domain = "www.cargosmart.com";

// Variable to hold Terms & Condition Version number

var tc_ver = "6.0";

// Variable to hold Terms & Condition url

var tc_url = "http://www.cargosmart.com/about/terms_of_use_v1.htm";

// Variable to hold Terms & Condition cookie name

var tc_cookie = "cstc";

// Variable to hold Terms & Condition cookie's expiration days

var tc_expire_day = 1500;

// Variable to hold cookie usage url

var cookie_url = "http://www.cargosmart.com/about/cookieu.htm";

// Variable to hold the last User ID cookie name

var csuid_cookie = "csuid";

// Variable to hold the last Product News language version

var cspsl_cookie = "cspsl";

//  Function to correct for 2.x Mac date bug

function fixCookieDate (date) 

{

	var base = new Date(0);

	var skew = base.getTime(); // dawn of (Unix) time - should be 0

	if (skew > 0)  // Except on the Mac - ahead of its time

	date.setTime (date.getTime() - skew);

}

// Function to open the cookie usage page

function showCookieUsage()

{

	window.location.href = cookie_url;

}

// Function to open Terms & Conditions page

function navigate_openTC(url,windowname,winsize) {

	winsize = winsize.toUpperCase();

	var ht = screen.availHeight;

	var wd = screen.availWidth;

	var left = 0;

	var top = 10;

	if (winsize == "S") scalar = 0.33;

	 else if (winsize == "M") scalar = 0.50;

	  else scalar = 0.75;

	w_ht = Math.round(scalar * ht);

	w_wd = Math.round(scalar * wd);

	left = (wd - w_wd) - 20;

	var windowparms = "resizable=yes,status=yes,scrollbars=yes,";

	windowparms += "left=" + left + ",top=" + top + ",screenX=" + left + ",screenY=" + top + ",";

	windowparms += "height=" + w_ht + ",width=" + w_wd;

	if (window.NewWindow) {

		if (!NewWindow.closed) NewWindow.close();

		}

	NewWindow = window.open(url,windowname,windowparms);

	NewWindow.window.focus();

	return;

}

function showTermsAndConditions()

{

	

	if (!isCookieEnabled())

	{

		showCookieUsage();

	}

	if (isTermsAndConditionsRead() == false)

	{

		var w_ht = screen.availHeight/2;

		var w_wd = screen.availWidth/2;

		var windowType = "left=" + w_ht/2 + ",top=" + w_wd/2 + ",screenX=10,screenY=10,";

		windowType += "height=" + w_ht + ",width=" + w_wd;

		windowType +=  ",scrollbars=yes," ;

		//newWindow = window.open(tc_url, "termsNcond", windowType);

		//newWindow.window.focus();

		navigate_openTC(tc_url,"termsNcond","l");

		

	}



}

function checkTermsAndConditions(toPage)

{

	proceed = "t";

	if (!isCookieEnabled())

	{

		showCookieUsage();

		proceed = "f";

			

	}

	if (isTermsAndConditionsRead() == false)

	{

		var w_ht = screen.availHeight/2;

		var w_wd = screen.availWidth/2;

		var windowType = "left=" + w_ht/2 + ",top=" + w_wd/2 + ",screenX=10,screenY=10,";

		windowType += "height=" + w_ht + ",width=" + w_wd;

		windowType +=  ",scrollbars=yes," ;

		//newWindow = window.open(tc_url, "termsNcond", windowType);

		//newWindow.window.focus();

		navigate_openTC(tc_url+'?'+toPage, "termsNcond","l");

		proceed = "f";

	}

	return proceed;

}

// Function to check if CargoSmart's cookie on T&C holds the right version or not

function isTermsAndConditionsRead()

{

	var tc_version = getCookie(tc_cookie);

	if (tc_version == null) 

		return false;

	if (tc_version == tc_ver) 

		return true;

	else

		return false;

}

//  Function to set cookie to indicate Terms & Conditions has been read

function setTermsAndConditionsRead(toPage) 

{

	var expdate = new Date();

	// Correct for Mac date bug - call only once for given Date object!

	fixCookieDate(expdate); 

	// tc_expire_day hrs from now

	expdate.setTime(expdate.getTime() + (tc_expire_day * 24 * 60 * 60 * 1000)); 

	// set cookie  

	setCookie(tc_cookie, tc_ver, expdate, "/");//, cargosmart_domain);

	// close current window

	//window.close()

	if (!self.opener) self.opener = self;
	if (toPage=="undefined") 
	{ 
		if (self.opener.location.href.indexOf('register/default.htm') > -1 ) {
			self.opener.registerURL();
		}
	}

	if (!(toPage=="undefined")) 

	{ 
		if (toPage.indexOf('csApp=') > -1)  { 
		       self.opener.location.href=tc_url.substring(0,tc_url.length-19)+"access.htm?"+toPage+"&isWait=1";
		}
		else { self.opener.location.href=toPage; 
		}

	}

	self.close();	

}

// Function to determine if cookie has been enabled in the client browser

function isCookieEnabled() 

{

	var test_cookie = "cs_test1";

	deleteCookie(test_cookie);

	setCookie(test_cookie, "1");

	var exists = getCookie(test_cookie);

	return exists;

}

// Function to return the value of the cookie specified by "name".

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 to return the decoded value of a cookie

function getCookieVal (offset) 

{

	var endstr = document.cookie.indexOf (";", offset);

	if (endstr == -1) endstr = document.cookie.length;

	return unescape(document.cookie.substring(offset, endstr));

}

// Function to create or update a cookie.

function setCookie(name,value,expires,path,domain,secure) 

{

	document.cookie = name + "=" + escape (value) +

		((expires) ? "; expires=" + expires.toGMTString() : "") +

		((path) ? "; path=" + path : "") +

		((domain) ? "; domain=" + domain : "") +

		((secure) ? "; secure" : "");

}

// Function to delete a cookie. (Sets expiration date to start of epoch)

function deleteCookie (name,path,domain) 

{

	if (getCookie(name)) 

	{

		document.cookie = name + "=" +

		((path) ? "; path=" + path : "") +

		((domain) ? "; domain=" + domain : "") +

		"; expires=Thu, 01-Jan-70 00:00:01 GMT";

	}

}

/*
 * Function to check cookie csuid to indicate whether or not show the message page
 */
function checkCSUIDCookie(formElement, formName) {
	defStr = "";
	if (isCookieEnabled()) {
		defStr = getCookie(csuid_cookie)
	}
	if ( defStr == null ) { defStr = "" }
	document.forms[formName].elements[formElement].value = defStr;
}

function setCSUIDCookie(formElement, formName) {
	var expdate = new Date();
	fixCookieDate(expdate); 
	expdate.setTime(expdate.getTime() + (tc_expire_day * 24 * 60 * 60 * 1000)); 
	setCookie(csuid_cookie, document.forms[formName].elements[formElement].value, expdate, "/");//, cargosmart_domain);
}

function checkCSPSLCookie(formElement, formName) {
	defStr = "";
	if (isCookieEnabled()) {
		defStr = getCookie(cspsl_cookie)
	}
	if ( defStr == null ) { defStr = "EN" }
	document.forms[formName].elements[formElement].value = defStr;
}

function setCSPSLCookie(formElement, formName) {
	var expdate = new Date();
	fixCookieDate(expdate); 
	expdate.setTime(expdate.getTime() + (tc_expire_day * 24 * 60 * 60 * 1000)); 
	setCookie(cspsl_cookie, document.forms[formName].elements[formElement].value, expdate, "/");//, cargosmart_domain);
}
