/////////////////////////////
// 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));
}

// write in the division
function writeInnerDivRegInfoCk(sString)
{
	var objDivRegInfoCk = document.getElementById('divRegInfoCk');
	if(!IsNull(objDivRegInfoCk)) objDivRegInfoCk.innerHTML=sString;
}

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

	if(cookieNameValue.substring(0,cookieRegInfoName.length) == cookieRegInfoName)
	{
		if(cookieNameValue.split("=").length == 2) 
		{
			cookieRegInfoValue = cookieNameValue.split("=")[1].toString();
			document.cookie = cookieRegInfoName + "=" + escape(cookieRegInfoValue) + "; domain=.swift.com; secure; 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 SM cookie exists, get info
		if(scGetCookie("SMSESSION") != '')
		{
			// Reg info cookie does not exist; get it via ajax
			var url = '/swift/myprofile/get_registration_number.cfm';
			var pars = 'fakeinfo=' + Math.random();
			var myAjax = new Ajax.Request(
				url, 
				{
					method: 'get', 
					parameters: pars, 
					onComplete: createCookieRegInfo
				});
		}
	}
	else writeInnerDivRegInfoCk(cookieRegInfoValue);
	 
}
