/***************************************************************************************/
// General functions
/***************************************************************************************/

/////////////////////////////////////////////////////////////////////////////////////////
// Checks whether a date entered is a valid date or not.
/////////////////////////////////////////////////////////////////////////////////////////
function checkDate(ddVal, mmVal, yyVal) {
	if(ddVal <= 0 || ddVal > 31 || mmVal <= 0 || mmVal > 12 || yyVal <= 0 || yyVal.length != 4) {
		alert("Please enter a valid date");
		return false;
	}
	monthArray = new Array("January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	if(mmVal == 2) {
		if(yyVal % 4 == 0) {
			if(ddVal > 29) {
				alert("February " + yyVal + " has only 29 days");
				return false;
			}
		}
		else {
			if(ddVal > 28) {
				alert("February " + yyVal + " has only 28 days");
				return false;
			}
		}
	}
	else {
		if(mmVal == 4 || mmVal == 6 || mmVal == 9 || mmVal == 11) {
			if(ddVal > 30) {
				alert(monthArray[mmVal-1] + " has only 30 days");
				return false;
			}
		}
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Checks whether a string is a valid email address.
/////////////////////////////////////////////////////////////////////////////////////////
function checkEmail(emailString) {
	splitVal = emailString.split('@');
	
	if(splitVal.length <= 1) {
		alert("Please enter a valid email address");
		return false;
	}
	if(splitVal[0].length <= 0 || splitVal[1].length <= 0) {
		alert("Please enter a valid email address");
		return false;
	}
	
	splitDomain = splitVal[1].split('.');
	if(splitDomain.length <= 1) {
		alert("Please enter a valid email address");
		return false;
	}
	if(splitDomain[0].length <= 0 || splitDomain[1].length <= 1) {
		alert("Please enter a valid email address");
		return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Removes the leading and trailing spaces in a strings and returns the trimmed string
/////////////////////////////////////////////////////////////////////////////////////////
function trimSpaces(stringValue) {
	// Checks the first occurance of spaces and removes them
	for(i = 0; i < stringValue.length; i++) {
		if(stringValue.charAt(i) != " ") {
			break;
		}
	}
	if(i > 0) {
		stringValue = stringValue.substring(i);
	}
	
	// Checks the last occurance of spaces and removes them
	strLength = stringValue.length - 1;
	for(i = strLength; i >= 0; i--) {
		if(stringValue.charAt(i) != " ") {
			break;
		}
	}
	if(i < strLength) {
		stringValue = stringValue.substring(0, i + 1);
	}
	
	// Returns the string after removing leading and trailing spaces.
	return stringValue;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Check whether a string contain permitted characters only
/////////////////////////////////////////////////////////////////////////////////////////
function checkAllowedChars(strToCheck, allowedChars)
{
     var acLen     = allowedChars.length;
     var stcLen     = strToCheck.length;
     strToCheck     = strToCheck.toLowerCase();
     var i;
     var j;
     var rightCount = 0;
     for(i = 0; i < acLen; i++)
     {
          switch(allowedChars.charAt(i))
          {
          case 'A':
               for(j = 0; j< stcLen; j++)
               {
                    rightCount += strToCheck.charAt(j) >= 'a' && strToCheck.charAt(j) <= 'z';
               }
               break;
          case 'N':
               for(j = 0; j< stcLen; j++)
               {
                    rightCount += strToCheck.charAt(j) >= '0' && strToCheck.charAt(j) <= '9';
               }
               break;
          default:
               for(j = -1; -1 != (j = strToCheck.indexOf(allowedChars.charAt(i), j + 1)); rightCount++);
               break;
          }
     }
     if(rightCount == stcLen)
     {
          return true;
     }
     return false;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Checks whether the first date argument is less than the second date argument
// Date arguments should be in the format - dd/mm/yyyy
/////////////////////////////////////////////////////////////////////////////////////////
function checkDateDifference(lowDate, highDate, comparison) {
	lowDateSplit = lowDate.split('/');
	highDateSplit = highDate.split('/');

	date1 = new Date();
	date2 = new Date();

	date1.setDate(lowDateSplit[0]);
	date1.setMonth(lowDateSplit[1] - 1);
	date1.setYear(lowDateSplit[2]);

	date2.setDate(highDateSplit[0]);
	date2.setMonth(highDateSplit[1] - 1);
	date2.setYear(highDateSplit[2]);

	if(comparison == "eq") {
		if(date1.getTime() == date2.getTime()) {
			return true;
		}
		else {
			return false;
		}
	}
	else if(comparison == "lt") {
		if(date1.getTime() < date2.getTime()) {
			return true;
		}
		else {
			return false;
		}
	}
	else if(comparison == "gt") {
		if(date1.getTime() > date2.getTime()) {
			return true;
		}
		else {
			return false;
		}
	}
	else if(comparison == "le") {
		if(date1.getTime() <= date2.getTime()) {
			return true;
		}
		else {
			return false;
		}
	}
	else if(comparison == "ge") {
		if(date1.getTime() >= date2.getTime()) {
			return true;
		}
		else {
			return false;
		}
	}
}

function formatCheck(dateStr){
	dateSplit = dateStr.split("/");
	if(dateSplit.length != 3){
		return false;
	}
	if(trimSpaces(dateSplit[0]).length <= 0 || trimSpaces(dateSplit[1]).length <= 0 || trimSpaces(dateSplit[2]).length <= 0) {
		return false;
	}
	return true;
}
function checkNews(frmName,elemName1,elemName2) {
	frmObject = eval("document." + frmName);
	frmElement = eval("document." + frmName + "." + elemName1);
	frmElement.value =eval(elemName2 + ".document.body.innerHTML");
	
	document.frmNews.ne_title.value = trimSpaces(document.frmNews.ne_title.value);
	if(document.frmNews.ne_title.value==""){
			alert("Please enter a title for the news")
			frmNews.ne_title.focus();
			return false;
	}
	if(trimSpaces(frmElement.value).length<=0){
		alert("Please make sure all fields are filled!");
		return false;
	}
	document.frmNews.frmAction.value = 'update'; 
}
function checkContent(frmName,elemName1,elemName2) {

	frmObject = eval("document." + frmName);
	frmElement = eval("document." + frmName + "." + elemName1);
	frmElement.value =eval(elemName2 + ".document.body.innerHTML");
	
	if(trimSpaces(frmElement.value).length<=0){
		alert("Please make sure that the page contents are duly filled");
		return false;
	}
	document.pageContentManager.frmAction.value = 'update'; 
}

function checkNoticeboard(frmName,elemName1,elemName2) {

	frmObject 	= 	eval("document." + frmName);
	frmElement 	= 	eval("document." + frmName + "." + elemName1);
	frmElement.value =	eval(elemName2 + ".document.body.innerHTML");
	
	document.pageContentManager.nb_title.value = trimSpaces(document.pageContentManager.nb_title.value);
	if(document.pageContentManager.nb_title.value==""){
		alert("Please enter the notice title");
		document.pageContentManager.nb_title.focus();
		return false;
	}
	if(trimSpaces(frmElement.value).length<=0){
		alert("Please make sure all fields are filled!");
		return false;
	}
	document.pageContentManager.frmAction.value = 'update'; 
}

function checkNewsletter(frmName,elemName1,elemName2){

	frmObject = eval("document." + frmName);
	frmElement = eval("document." + frmName + "." + elemName1);
	frmElement.value =eval(elemName2 + ".document.body.innerHTML");
	
	document.pageContentManager.nl_title.value = trimSpaces(document.pageContentManager.nl_title.value);
	if(document.pageContentManager.nl_title.value==""){
		alert("Please enter the newsletter title");
		document.pageContentManager.nl_title.focus();
		return false;
	}
	if(trimSpaces(frmElement.value).length<=0){
		alert("Please make sure all fields are filled!");
		return false;
	}
	document.pageContentManager.frmAction.value = 'update'; 
}

function checkValidDate(dateStr){

	    var slash1 = dateStr.indexOf("/");
	    if (slash1 == -1){
				slash1 = dateStr.indexOf("-");
	    }
	    // if no slashes or dashes, invalid date
	    if (slash1 == -1){
			return false;
	    }
	    var dateDay = dateStr.substring(0, slash1)
	    var dateMonthAndYear = dateStr.substring(slash1+1, dateStr.length);
	    var slash2 = dateMonthAndYear.indexOf("/");
	    if (slash2 == -1){
			slash2 = dateMonthAndYear.indexOf("-");
	    }
	    // if not a second slash or dash, invalid date

	   if (slash2 == -1){
			return false;
	   }
	    var dateMonth = dateMonthAndYear.substring(0, slash2);
	    var dateYear = dateMonthAndYear.substring(slash2+1, dateMonthAndYear.length);
	    if ( (dateMonth == "") || (dateDay == "") || (dateYear == "") ){
			return false;
	    }
	    // if any non-digits in the month, invalid date
	    for (var x=0; x < dateMonth.length; x++) {
			var digit = dateMonth.substring(x, x+1);
			if ((digit < "0") || (digit > "9")){
				return false;
			}
	    }

	    // convert the text month to a number
	    var numMonth = 0;
	    for (var x=0; x < dateMonth.length; x++) {
			digit = dateMonth.substring(x, x+1);
			numMonth *= 10;
			numMonth += parseInt(digit);
	    }

	    if ((numMonth <= 0) || (numMonth > 12)){
			return false;
	    }
	    // if any non-digits in the day, invalid date
	    for (var x=0; x < dateDay.length; x++) {
			digit = dateDay.substring(x, x+1);
			if ((digit < "0") || (digit > "9")) { return false; }
	    }
	    // convert the text day to a number
	    var numDay = 0;
	    for (var x=0; x < dateDay.length; x++){
			digit = dateDay.substring(x, x+1);
			numDay *= 10;
			numDay += parseInt(digit);
	    }

	    if ((numDay <= 0) || (numDay > 31)){
			return false;
	    }

	    // February can't be greater than 29 (leap year calculation comes later)
	    if ((numMonth == 2) && (numDay > 29)){
			return false;
	    }
	    // check for months with only 30 days
	    if ((numMonth == 4) || (numMonth == 6) || (numMonth == 9) || (numMonth == 11)){
		if (numDay > 30){
			return false;
		}

	   }
	    // if any non-digits in the year, invalid date
	    for (var x=0; x < dateYear.length; x++) {
			digit = dateYear.substring(x, x+1);
			if ((digit < "0") || (digit > "9")) { return false; }
	    }
	    // convert the text year to a number
	    var numYear = 0;
	    for (var x=0; x < dateYear.length; x++) {
			digit = dateYear.substring(x, x+1);
			numYear *= 10;
			numYear += parseInt(digit);
	    }
	    // Year must be a 2-digit year or a 4-digit year
	    if ( (dateYear.length != 2) && (dateYear.length != 4) ) { return false; }
	    // if 2-digit year, use 50 as a pivot date
	    if ( (numYear < 50) && (dateYear.length == 2) ) { numYear += 2000; }
	    if ( (numYear < 100) && (dateYear.length == 2) ) { numYear += 1900; }
	    if ((numYear <= 0) || (numYear > 9999)) { return false; }
	    // check for leap year if the month and day is Feb 29
	    if ((numMonth == 2) && (numDay == 29)) {
		var div4 = numYear % 4;
		var div100 = numYear % 100;
		var div400 = numYear % 400;
		// if not divisible by 4, then not a leap year so Feb 29 is invalid
		if (div4 != 0) { return false; }
		// at this point, year is divisible by 4. So if year is divisible by
		// 100 and not 400, then it's not a leap year so Feb 29 is invalid
		if ((div100 == 0) && (div400 != 0)) { return false; }
	    }
	    // date is valid
	    return true;
	}
	/*
On key Press check field data...
N - Allows Only Numerics
NDH - Allows Only Numerics, Dot, Hyphen
ND - Allows Only Numerics, Dot
A - Allows Omly Alphabets and spaces
CA - Allows Only capital letters
SA - Allows Only Small letters
AN - Allows Alphabets and numbers but no special charecters..
E - Allows emailaddress only
W - Allows web Url only
ANHD- Allows Alphabets,spaces and numbers but no special charecters except dot,Hyphen..

Eg:--  onKeyPress="return CheckField(this, event, 'ANHD')"
*/
function CheckField(myfield, e, type)
{
    var key;
    var keychar;
 
    if (window.event)
    {
        key = window.event.keyCode;
    }
    else if (e)
    {
        key = e.which;
    }
    else
    {
        return true;
    }
	if(type == 'NDH')
	{
		//key != 0 && key != 8 && key != 32 && 
		if(key != 0 && key != 8 && key != 32 && key != 46 && key != 45 && key > 31 && (key < 48 || key > 57))
		{ return false; }
		else
		{ return true; }
	}
	if(type == 'ND')
	{
		if(key != 0 && key != 8 && key != 32 && key != 46 && (key < 48 || key > 57))
		{ return false; }
		else
		{ return true; }
	}
	if(type == 'N')
	{
		if(key != 0 && key != 8 && key != 32 && (key < 48 || key > 57))
		{ return false; }
		else
		{ return true; }
	}
	if(type == 'A')
	{
		if(key != 0 && key != 8 && key != 32 && (key < 65 || key > 90) && (key < 97 || key > 122))
		{ return false; }
		else
		{ return true; }
	}
	if(type == 'CA')
	{
		if(key != 0 && key != 8 && key != 32 && (key < 65 || key > 90))
		{ return false; }
		else
		{ return true; }
	}
	if(type == 'SA')
	{
		if(key != 0 && key != 8 && key != 32 && (key < 97 || key > 122))
		{ return false; }
		else
		{ return true; }
	}
	if(type == 'AN')
	{
		if(key != 0 && key != 8 && key != 32 && (key < 65 || key > 90) && (key < 97 || key > 122) && (key < 48 || key > 57))
		{ return false; }
		else
		{ return true; }
	}
	if(type == 'ANHD')
	{
		if(key != 0 && key != 8 && key != 32 && key != 46 && key != 45 && (key < 65 || key > 90) && (key < 97 || key > 122) && (key < 48 || key > 57))
		{ return false; }
		else
		{ return true; }
	}
	if(type == 'E')
	{
		if(key != 0 && key != 8 && key != 32 && key != 46 && key != 95 && key != 64 && (key < 65 || key > 90) && (key < 97 || key > 122) && (key < 48 || key > 57))
		{ return false; }
		else
		{ return true; }
	}
	if(type == 'W')
	{
		if(key != 0 && key != 8 && key != 32 && key != 46 && key != 45 && key != 95 && (key < 65 || key > 90) && (key < 97 || key > 122) && (key < 48 || key > 57))
		{ return false; }
		else
		{ return true; }
	}
	if(type == 'TEST')
	{
		alert(key);
		if(key != 0 && key != 8 && key != 32 && (key < 65 || key > 90) && (key < 97 || key > 122) && (key < 48 || key > 57))
		{ return false; }
		else
		{ return true; }
	}
}

