function vSetCookie(strCookieName, strCookieValue) {
    var strExpiryDate = new Date("1/1/3003");
    document.cookie = strCookieName + "=" + escape(strCookieValue) + "; expires=" + strExpiryDate.toGMTString() + ";"
}

function vGetCookie(strCookie) {
	var iBeginCookie, iEndCookie;
	if (document.cookie.length > 0) {
		iBeginCookie = document.cookie.indexOf(strCookie+"=");
		if (iBeginCookie != -1) {
			iBeginCookie += strCookie.length+1;
			iEndCookie = document.cookie.indexOf(";", iBeginCookie);
			if (iEndCookie == -1) iEndCookie = document.cookie.length;
			return unescape(document.cookie.substring(iBeginCookie, iEndCookie));
		}
	}
	return "";
}

function bIsValidEmail(strEmail) {
	var iAtIndex;
	var iDotIndex;
	var iSemiColonIndex;

	iAtIndex=strEmail.indexOf("@",0);
	iDotIndex=strEmail.indexOf(".",0);
	iSemiColonIndex=strEmail.indexOf(";",0);

	if ((iAtIndex==-1)||(iDotIndex==-1)||(strEmail.length<5)||(iSemiColonIndex>-1)) return false; else return true;
}

//------------------------------------------------------------------------------------------------------
//This function left trims a string. The only assumption it makes is that the value that will be passed
//is of string type.
//------------------------------------------------------------------------------------------------------
function ltrim(strTemp) {
	var iLen;
    var iPosn;
    var iLoop;
    strTemp=strTemp+"";

    iLen=strTemp.length;
    iPosn=-2;
    for (iLoop=0;iLoop<iLen;iLoop++) {
    	if (strTemp.substr(iLoop,1)!=" ") {iPosn=iLoop;break;}
    	iPosn=-1;
    }
	//iPosn=-2 means the string did not contain any characters
	//iPosn=-1 means string contained only spaces
	//iPosn>=0 means that there were leading spaces in a mixed string with the 
	//         first non space character occuring at iPosn (indexed from 0)
	if ((iPosn==-1)||(iPosn==-2)) return "";
	else return strTemp.substr(iPosn,iLen-iPosn);
}


//------------------------------------------------------------------------------------------------------
//This function right trims a string. The only assumption it makes is that the value that will be passed
//is of string type.
//------------------------------------------------------------------------------------------------------
function rtrim(strTemp) {
	var iLen;
    var iPosn;
    var iLoop;
    strTemp=strTemp+"";

    iLen=strTemp.length;
    iPosn=-2;
    for (iLoop=(iLen-1);iLoop>=0;iLoop--) {
    	if (strTemp.substr(iLoop,1)!=" ") {iPosn=iLoop;break;}
    	iPosn=-1;
    }
	//iPosn=-2 means the string did not contain any characters
	//iPosn=-1 means string contained only spaces
	//iPosn>=0 means that there were trailing spaces in a mixed string with the 
	//         first non space character occuring at iPosn (indexed from 0)
	if ((iPosn==-1)||(iPosn==-2)) return "";
	else return strTemp.substr(0,iPosn+1);
}

//------------------------------------------------------------------------------------------------------
//This function trims the string of spaces using the ltrim and rtrim functions.
//------------------------------------------------------------------------------------------------------
function trimString(strTemp) {
	return ltrim(rtrim(strTemp));
}

function strTrim(strTemp) {
	return trimString(strTemp);
}

//------------------------------------------------------------------------------------------------------
//This function will accept a string and test whether it is of type m(m)/d(d)/(cc)yy format
//If year is detected to be a two digit year the check is assumed using the century as 2000.
//Also dates below 1900 are considered to be invalid
//------------------------------------------------------------------------------------------------------
function isDate(strTemp) {
	var daysInMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var iDay;
	var iMonth;
	var iYear;
	var iPosn1, iPosn2, bExceeded;
	var iLen, iLoopA, iAdjust;
	        
	bExceeded=false;
	iPosn1=-1;
	iPosn2=-1;
	//--------------------------------------------------------------------------------------------------
	//Searching for /
	//--------------------------------------------------------------------------------------------------
	iLen=strTemp.length;
	for (iLoopA=0;iLoopA<iLen;iLoopA++) {
		if (strTemp.substring(iLoopA,iLoopA+1)=='/') {
			if (iPosn1==-1) iPosn1=iLoopA; 
			else {if (iPosn2!=-1) bExceeded=true; else iPosn2=iLoopA;}
		}
		if (bExceeded) break;
	}
	//--------------------------------------------------------------------------------------------------
	//At this point, if bExceeded is true then there were more than one /.
	//if iPosn1 or 2 are -1 then there are not enough /
	//--------------------------------------------------------------------------------------------------
	if ((bExceeded)||(iPosn1==-1)||(iPosn2==-1)) return "Incorrect number of slashes";
	//--------------------------------------------------------------------------------------------------
	//Now extracting the numbers
	//--------------------------------------------------------------------------------------------------
	iDay=parseInt(strTemp.substring(iPosn1+1,iPosn2),10);
	iMonth=parseInt(strTemp.substring(0,iPosn2),10);
	iYear=parseInt(strTemp.substring(iPosn2+1,iLen),10);        
	if ((isNaN(iDay))||(isNaN(iMonth))||(isNaN(iYear))) return "Values are not proper numbers";
	if ((iDay<1)||(iMonth<1)||(iYear<1)) return "Values are negative";
    
	//--------------------------------------------------------------------------------------------------
	//Performing core date validations
	//--------------------------------------------------------------------------------------------------
      
	//Year Range
	if (iYear<100) iYear+=2000;
	else {if (iYear<1900) return "Year smaller than 1900";}
	//Month Range
	if (iMonth>12) return "Month greater than 12";
	//Adjusting February value
	iAdjust=0;

	if (!(iYear%4)) {
		//----------------------------------------------------------------------------------------------
		//Divisible by 4. Here we should add one. But this depends on divisiblity by 100
		//----------------------------------------------------------------------------------------------
		iAdjust=1;
		if (!(iYear%100)) {
			//------------------------------------------------------------------------------------------
			//Divisble by 100 i.e. 400 since here the year is already divisble by 4.
			//Now ideally a year divisible by 400 does not need adjustment except for 200
			//------------------------------------------------------------------------------------------
			if (iYear==200) iAdjust=1; else iAdjust=0;
		}
	}  
	daysInMonth[1]+=iAdjust;
	//--------------------------------------------------------------------------------------------------
	//At this point, we know that the year is valid and month is valid. Proceeding to check whether
	//the day falls inside the month. This will be the last validation
	//--------------------------------------------------------------------------------------------------
	if (iDay>daysInMonth[iMonth-1]) return "Day out of month";

	return "";
}

