
// Create a cookie with the specified name and value.
// The cookie expires at the end of the 20th century.
function SetCookie(sName, sValue) {
//	if (!navigator.cookieEnabled) 
//		alert("Cookie is not enabled. Unable to set cookies.");
//	else
	{
		var aCookie = document.cookie.split(";");  
		var sAllCookie = "";
		var bFound = false;
		for (var i = 0; i < aCookie.length; i++)  {
			// a name/value pair (a crumb) is separated by an equals sign      
			var aCrumb = aCookie[i].split("=");
			aCrumb[0] = LTrimSpace(aCrumb[0]);
			if ("KH" == aCrumb[0]) {
				var sTheCook = unescape(aCrumb[1]);
				var aRealCook = sTheCook.split(";");
				for (var j = 0; j < aRealCook.length; j++) {
					// a name/value pair (a crumb) is separated by an equals sign      
					var aBit = aRealCook[j].split("=");
					aBit[0] = LTrimSpace(aBit[0]);
					if (sName == aBit[0]) {
						aBit[1] = escape(sValue);
						bFound = true;
					}
					sAllCookie += aBit[0] + "=" + aBit[1] + ";";
				}
			}
		}
		if (!bFound) sAllCookie += sName + "=" + escape(sValue) + ";";
		sAllCookie = sAllCookie.substring(0, sAllCookie.length - 1);
		document.cookie = "KH=" + escape(sAllCookie) + "; expires=Mon, 31 Dec 1999 23:59:59 UTC;";
	}   
}

// Retrieve the value of the cookie with the specified name.
function GetCookie(sName){
//	if (!navigator.cookieEnabled) {
//		alert("Cookie is not enabled. Unable to get cookies.");
//		return;
//	}
	// cookies are separated by semicolons   
	var aCookie = document.cookie.split(";");   
	for (var i = 0; i < aCookie.length; i++)  {
		// a name/value pair (a crumb) is separated by an equals sign      
		var aCrumb = aCookie[i].split("=");
		aCrumb[0] = LTrimSpace(aCrumb[0]);
		if ("KH" == aCrumb[0]) {
			var sTheCook = unescape(aCrumb[1]);
			var aRealCook = sTheCook.split(";");
			for (var j = 0; j < aRealCook.length; j++) {
				// a name/value pair (a crumb) is separated by an equals sign      
				var aBit = aRealCook[j].split("=");
				aBit[0] = LTrimSpace(aBit[0]);
				if (sName == aBit[0]) return unescape(aBit[1]);
			}
		}
	}   
	// a cookie with the requested name does not exist
	return "";
}

function IsStrEmpty(str){
	var i, len;

	if (str == "" )
		return true

	len = str.length;
	
	for(i=0; i<len; i++){			
		if (str.charAt(i) != ' '){
		 	return false;
		}
	}
	return true;
}

function IsInteger(str){
	var i;
	
	if (IsStrEmpty(str) == true)
		return false;

	str = LTrimSpace(str);
	str = RTrimSpace(str);

	
		
	for(i=0; i<str.length; i++){
		if (str.charAt(i) < '0' || str.charAt(i) > '9')
			return false;
	}
	return true;
}

function IsNumber(str){
	return IsPositive(str);
}

function IsPositive(str){
	var i;
	
	
	if (IsStrEmpty(str) == true)
		return false;
	str = LTrimSpace(str);
	str = RTrimSpace(str);
	
	
	for(i=0; i<str.length; i++){
		if ((str.charAt(i) < '0' || str.charAt(i) > '9') && str.charAt(i) != '.')
			return false;
	}
	
	return true;
}

function IsPercentage(str){
	if (!IsPositive(str))
		return false;

	if (str > 100)
		return false;

	return true;
}

////////////////////////////////////////////////////////////



function ValidateTime(tType, timeObj, showError) {
	var limit, timeStr, isValid;
	isValid = true;
	if (tType == 'hour')
		limit = 12;
	else if (tType == 'minute')
		limit = 60;

	else
		return;



	timeStr = timeObj.value
	if (isWhitespace(timeStr))
		return false;

	if (!IsPositive(timeStr))
		isValid = false;

	if (timeStr > limit)
		isValid = false;

	if (isValid == false && showError == true){
		alert("Invalidate hour or Minute.");
		timeObj.focus();
		timeObj.select();
	}
	return isValid
	
}

function ShowInputError(dataType){
	if (dataType == "Integer")
		alert("Please enter a positive integer.");
	else if (dataType == "Positive")
		alert("Please enter a positive number.");
	else
		alert("Please enter a number between 0 and 100.");

}


// convert a number to integer(n=0) or rounding the number with n-1 decimal 
function CutNumber(str, n){
	var tmp, i, pos, strlen;
	
	tmp = str.toString();
	pos = SearchStr(tmp, ".");

	if (tmp.length > pos + n && pos != -1){
		tmp = tmp.substring(0, pos + n);
	}

	tmp = FormatNumber(tmp);

	if(n > 0){
		pos = SearchStr(tmp, ".");
		strlen = tmp.length;
		if(pos + n == strlen)
			return tmp;

		if(pos == -1){
			tmp = tmp +".";
			strlen = strlen +1;
		}

		for(i=strlen; i<pos+n; i++)
			tmp = tmp + "0";	
		
	}

		
	return tmp;		
}

// format number to the format of 100,000 (add ' , ')
function FormatNumber(str){
	var tmp, i, pos;
	
	pos = SearchStr(str, ".");
	
	if (pos == -1)
		pos = str.length;

	tmp = str;
	while (pos > 3){
		tmp = str.substring(0, pos-3) + ",";
		tmp =tmp + str.substring(pos-3, str.length);
		str = tmp;
		pos = pos - 3;
	}
	return tmp

}


function SearchStr(str1, str2){
	var i, len1, len2;

	len1 = str1.length;
	len2 = str2.length;

	if (len1 < len2)
		return -1;

	for(i=0; i<=len1-len2; i++)
		if (str1.substring(i, len2+i) == str2)
			return i;
	

	return -1;
}


function LTrimSpace(str){
	var i, len;
	var tmp;
	len = str.length;
	for(i=0; i<len; i++){			
		if (str.charAt(i) != ' '){
		 	tmp = str.substring(i, len-i+1);
			return tmp;
		}
	}
	return tmp;
}

function RTrimSpace(str){
	var i, len;
	var tmp;
	len = str.length;
	for(i=len-1; i>=0; i--){			
		if (str.charAt(i) != ' '){
		 	tmp = str.substring(0, i+1);
			return tmp;
		}
	}
	return tmp;
}







/////////////////////////////////////////////////////////////////////////////////////////////////
/**
 * DHTML date validation script for MM/DD/yyyy. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function bInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth = dtStr.substring(0, pos1)
	var strDay = dtStr.substring(pos1 + 1, pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || bInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function IsRxNos(str) {
    var i, len;

    str = LTrimSpace(str);
    str = RTrimSpace(str);

    if (str == "")
        return false;

    len = str.length;

    for (i = 0; i < len; i++) {
        if ((str.charAt(i) < '0' || str.charAt(i) > '9') && str.charAt(i) != ',' && str.charAt(i) != ' ')
            return false;
    }

    return true;
}

function IsValidPhoneNo(str) {
    var i, len;

    str = LTrimSpace(str);
    str = RTrimSpace(str);

    if (str == "")
        return false;

    len = str.length;
    if (len < 7)
        return false;

    for (i = 0; i < len; i++) {
        if ((str.charAt(i) < '0' || str.charAt(i) > '9') && str.charAt(i) != '(' && str.charAt(i) != ')' && str.charAt(i) != '-' && str.charAt(i) != ' ')
            return false;
    }

    return true;
}

function IsValidEmail(str) {
    var i, len;

    str = LTrimSpace(str);
    str = RTrimSpace(str);

    if (str == "")
        return false;

    len = str.length;
    if (len < 7)
        return false;

    for (i = 0; i < len; i++) {
        if (str.charAt(i) == '@')
            return true;
    }

    return false;
}


	









