/////////////////////////////
// Some String utils function

// Compare two strings (default=insensitive)
function strComp(sString1,sString2,bSensitive)
{
	if(IsNull(bSensitive)) bSensitive = false;
	if(!bSensitive)
	{
		sString1 = sString1.toLowerCase();
		sString2 = sString2.toLowerCase();
	}
	return (sStringG1==sString2);
}


// Check if null
function IsNull(oItem)
{
	return ( oItem == null || oItem == undefined )
}


// Trim functions
function Trim(str){ return str.replace(/^\s*|\s*$/g,""); }
//function RTrim(STRING){ return str.replace(/^\s*,""); }
//function LTrim(STRING){ return str.replace(\s*$/g,""); }


/////////////////////////////
// Some cookies stuffs

// set cookie reg info name
var cookieRegInfoName	= "CK_SDC_REG_INFO";

// Get Cookies function
function scGetCookie(sCookieName)
{
	var startCookiePos = document.cookie.indexOf(sCookieName + '=');
	if(startCookiePos == -1) return '';
	startCookiePos += sCookieName.length + 1;
	var endCookiePos = document.cookie.indexOf(';',startCookiePos);
	if(endCookiePos == -1) endCookiePos = document.cookie.length;
	return unescape(document.cookie.substring(startCookiePos,endCookiePos));
}

// Get URL Param function
function scGetURLParam(sParamName)
{
	var startParamPos = location.search.substring(1).indexOf(sParamName + '=');
	if(startParamPos == -1) return '';
	startParamPos += sParamName.length + 1;
	var endParamPos = location.search.substring(1).indexOf('&',startParamPos);
	if(endParamPos == -1) endParamPos = location.search.substring(1).length;
	return unescape(location.search.substring(1).substring(startParamPos,endParamPos));
}

// write in the division
function writeInnerDivRegInfoCk(sString)
{
	var objLoginBox = document.getElementById('loginBox');
	if(!IsNull(objLoginBox))
	{
		if(sString != '')
			objLoginBox.innerHTML='<A href="' + scLogOffUrl + '" class="loginRight">Logout</A><A href="' + scSecureUrl + 'myprofile" class="loginLeft">'  + sString + '  is logged in</A>';
		 else
		 	objLoginBox.innerHTML='<div class="custLogin"><A href="' + scSecureUrl + 'myprofile" class="loginLeft">Customer log in</A></div>';
	}
}

// Create cookie from Ajax response
function createCookieRegInfo(originalRequest)
{
	var cookieRegInfoValue='';
	var cookieNameValue = originalRequest.responseText;

	if(cookieNameValue.substring(0,cookieRegInfoName.length) == cookieRegInfoName)
	{
		if(cookieNameValue.split("=").length == 2)
		{
			cookieRegInfoValue = cookieNameValue.split("=")[1].toString();
			// remove the registration number for the revamp 2008
			if(cookieRegInfoValue.lastIndexOf(' ') != -1) cookieRegInfoValue = cookieRegInfoValue.substr(0,cookieRegInfoValue.lastIndexOf(' '));
			document.cookie = cookieRegInfoName + "=" + escape(cookieRegInfoValue) + "; domain=.swift.com; path=/";
		}
	}
	writeInnerDivRegInfoCk(cookieRegInfoValue);
}

// Calling this function will write registration info (first name, last name, registration number)
function displayRegInfoCookie()
{
	// get cookie Reg Num info
	var cookieRegInfoValue	= scGetCookie(cookieRegInfoName);

	if( cookieRegInfoValue == '' )
	{
		if ( (location.href.substr(0,5) == 'https') &&  (scGetCookie("ObSSOCookie") != '') )
		{
			// Reg info cookie does not exist; get it via ajax
			var url = '/myprofile/get_registration_number.jsp';
			var pars = 'fakeinfo=' + Math.random();
			var myAjax = new Ajax.Request(
				url,
				{
					method: 'get',
					parameters: pars,
					onComplete: createCookieRegInfo
				});
		} else writeInnerDivRegInfoCk(cookieRegInfoValue);
	} else writeInnerDivRegInfoCk(cookieRegInfoValue);
}

// Calling this function will populate the customer login box on the home page (div id="custLogin")
function displayCustLoginInfo()
{
	// get cookie Reg Num info & elements to update
	var cookieRegInfoValue	= scGetCookie(cookieRegInfoName);
	var custLoginBox = document.getElementById('custLogin');
	var custLoginLoggedIn = document.getElementById('custLoginLoggedIn');
	var custLoginNotLoggedIn = document.getElementById('custLoginNotLoggedIn');
	var custLoginLoggedInWelcome = document.getElementById('custLoginLoggedInWelcome');

	if( cookieRegInfoValue == '' )
	{
		//cookie doesn't exist or is empty, write "not logged in" block
		custLoginLoggedIn.style.display = 'none';
		custLoginNotLoggedIn.style.display = '';
	}
	else
	{
		//cookie exists and is not empty, write "logged in" block
		custLoginLoggedInWelcome.innerHTML = '<b>Welcome ' + cookieRegInfoValue + '</b>';
		custLoginLoggedIn.style.display = '';
		custLoginNotLoggedIn.style.display = 'none';
	};

}

