/***************************************************************************************/
// 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) {
//alert(lowDate);
//alert(highDate);
	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(date1.getTime() < date2.getTime()) {
		return true;
	}
	else {
		return false;
	}
}
function checkFeedBack() {
	document.forms[0].visitorName.value = trimSpaces(document.forms[0].visitorName.value);
	if(document.forms[0].visitorName.value.length <= 0) {
		alert("Please enter your name");
		document.forms[0].visitorName.focus();
		return false;
	}

	document.forms[0].emailAddress.value = trimSpaces(document.forms[0].emailAddress.value);
	if(document.forms[0].emailAddress.value.length <= 0) {
		alert("Please enter your email address");
		document.forms[0].emailAddress.focus();
		return false;
	}
	if(!checkEmail(document.forms[0].emailAddress.value)) {
		document.forms[0].emailAddress.focus();
		document.forms[0].emailAddress.select();
		return false;
	}
	document.forms[0].visitorComments.value = trimSpaces(document.forms[0].visitorComments.value);
	if(document.forms[0].visitorComments.value.length <= 0) {
		alert("Please enter your comments");
		document.forms[0].visitorComments.focus();
		return false;
	}
	document.forms[0].frmAction.value = "update";
}
function checkMailing(){
	firstName = trimSpaces(document.forms[0].firstName.value);
	if(firstName.length <= 0) {
		alert("Please enter your first name.");
		document.forms[0].firstName.focus();
		return false;
	}
	
	lastName = trimSpaces(document.forms[0].lastName.value);
	if(lastName.length <= 0) {
		alert("Please enter your last name.");
		document.forms[0].lastName.focus();
		return false;
	}
	areacode = trimSpaces(document.forms[0].areacode.value);
	if(areacode.length <= 0) {
		alert("Please enter the areacode number.");
		document.forms[0].areacode.focus();
		return false;
	}
	phone = trimSpaces(document.forms[0].phone.value);
	if(phone.length <= 0) {
		alert("Please enter the phone number.");
		document.forms[0].phone.focus();
		return false;
	}
	subscriberEmail = trimSpaces(document.forms[0].subscriberEmail.value);
	if(subscriberEmail.length <= 0) {
		alert("Please enter your email.");
		document.forms[0].subscriberEmail.focus();
		return false;
	}
	fromDate = trimSpaces(document.forms[0].fromDate.value);
	if(fromDate.length <= 0) {
		alert("Please enter event starting date.");
		document.forms[0].fromDate.focus();
		return false;
	}
	toDate = trimSpaces(document.forms[0].toDate.value);
	if(toDate.length <= 0) {
		alert("Please enter event ending date.");
		document.forms[0].toDate.focus();
		return false;
	}
	if(subscriberEmail.length > 0){
		if(!checkEmail(subscriberEmail)){
			document.forms[0].subscriberEmail.focus();
			return false;
		}
	}
	
	document.forms[0].frmAction.value = "yes";

}
function checkNewsletterList() {

	mailFrom = trimSpaces(document.forms[0].mailFrom.value);
	if(mailFrom.length <= 0) {
		alert("You have to provide an email address of the sender");
		document.forms[0].mailFrom.focus();
		return false;
	}
	
	additionalRecipients = trimSpaces(document.forms[0].additionalRecipients.value);
	recList = additionalRecipients.split(";");
	mailExists = false;
	for(recCount = 0; recCount < recList.length; recCount ++) {
		if(trimSpaces(recList[recCount]).length > 0) {
			if(!checkEmail(trimSpaces(recList[recCount]))) {
				document.forms[0].additionalRecipients.focus();
				return false;
			}
			mailExists = true;
		}
	}
	if(!mailExists) {
		alert("Please specify the recipients for the news letter");
		document.forms[0].additionalRecipients.focus();
		return false;
	}

	
	mailSubject = trimSpaces(document.forms[0].mailSubject.value);
	if(mailSubject.length <= 0) {
		alert("You have to provide the subject of the mail message");
		document.forms[0].mailSubject.focus();
		return false;
	}

	document.forms[0].frmAction.value = "send";
}
function setMenuItem() {
	category = document.forms[0].category.options[document.forms[0].category.selectedIndex].value;
	hashIndex = category.indexOf("#");
	if(hashIndex < 0){
		anchorName = "";
		document.forms[0].submit();
	}
	else{
		anchorName = category.substr(hashIndex, category.length);
		window.location.href = anchorName;
	}
}
function showReservationDate(frmElement, dispElement) {
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;
	
	winWidth = 375;
	winHeight = 300;
	
	sLeft = (sWidth - winWidth) / 2;
	sTop = (sHeight - winHeight) / 2;

	window.open("../php/showCalendar.php?frmElement=" + frmElement + "&dispElement=" + dispElement + "&laterdate=1", "hispanic", "width=" + winWidth + ",height=" + winHeight + ",top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=0,resizable=0");
	return;
}
function getCalDate(frmElement, dispElement, dateValue, dispValue) {
	frmObject = eval("window.opener.document.forms[0]." + frmElement);
	frmObject.value = dateValue;
	frmObject = eval("window.opener.document.forms[0]." + dispElement);
	frmObject.value = dispValue;
	closeWindow();
}
function checkLinkURL() {
	linkURL = trimSpaces(document.forms[0].linkURL.value);
	if(linkURL.length <= 0) {
		alert("Please enter the link URL");
		document.forms[0].linkURL.focus();
		return false;
	}
	document.forms[0].frmAction.value = "update";
}
function cancelInput(frmElement) {
	frmObject = eval("window.opener.document.forms[0]." + frmElement);

	window.opener.focus();
	frmObject.focus();
	window.close();
}
function checkImageInput() {
	imageName = trimSpaces(document.forms[0].imageName.value);
	if(imageName.length <= 0) {
		alert("Please select an image");
		document.forms[0].imageName.focus();
		return false;
	}
	document.forms[0].frmAction.value = "update";
}
function addColor(frmElement, colorValue, rgbOnly) {
	frmObject = eval("window.opener.document.forms[0]." + frmElement);
	
	// Function to update the color values
	if(rgbOnly == 1) {
		frmObject.value = "#" + colorValue;
	}
	else {
		selText = window.opener.document.selection.createRange().text;
		frmObject.focus();
		selObject = trimSpaces(window.opener.document.selection.createRange());
		if(selObject.text.length > 0) {
			selObject.text = "<font color='#" + colorValue + "'>" + selObject.text + "</font> ";
		}
		else {
			if(frmObject.createTextRange && frmObject.caretPos) {
				var caretPos = frmObject.caretPos;
				caretPos.text = "<font color='#" + colorValue + "'></font>";
			}
			else {
				frmObject.value = "<font color='#" + colorValue + "'></font>";
			}
		}
	}
	frmObject.focus();
	window.close()
}
function checkEventMail(){
	recieverName2 = trimSpaces(document.forms[0].recieverName2.value);
	recieverEmail2 = trimSpaces(document.forms[0].recieverEmail2.value);
	visitorName = trimSpaces(document.forms[0].visitorName.value);
	visitorEmail = trimSpaces(document.forms[0].visitorEmail.value);
	visitorComments = trimSpaces(document.forms[0].visitorComments.value);

	if(recieverName2.length <= 0){
		alert("Please enter your friends name.");
		document.forms[0].recieverName2.focus();
		return false;
	}
	if(!checkEmail(recieverEmail2)) {
		document.forms[0].recieverEmail2.focus();
		return false;
	}
	if(visitorName.length <= 0){
		alert("Please enter your name.");
		document.forms[0].visitorName.focus();
		return false;
	}
	if(!checkEmail(visitorEmail)) {
		document.forms[0].visitorEmail.focus();
		return false;
	}
		if(visitorComments.length <= 0){
		alert("Please enter your comment on this event.");
		document.forms[0].visitorComments.focus();
		return false;
	}
	
	document.forms[0].frmAction.value = "yes";

}
///////////////////////////////////////////////////////
function printDate1(fday, fmonth, fyear, day1, month1, year1) {

        document.write('<FONT CLASS="frm"><SELECT NAME="' + fday + '" WIDTH="3" SIZE="1" CLASS="frm">');
        document.write('<OPTION VALUE="">Day</OPTION>');
        for(var i=1;i<=31;i++) {
            if (i == day1) {
                document.write('<OPTION VALUE="' + i + '" SELECTED="SELECTED">' + i + '</OPTION>');
            }else {
               document.write('<OPTION VALUE="' + i + '" >' + i + '</OPTION>');
            }
        }
        document.write('</SELECT></FONT><FONT CLASS="frm"><SELECT NAME="' + fmonth + '" WIDTH="5" SIZE="1" CLASS="frm">');
        document.write('<OPTION VALUE="">Month</OPTION>');
        var mon = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
        for(i=1;i<=12;i++) {
            if (i == month1) {
                document.write('<OPTION VALUE="' + i + '" SELECTED="SELECTED">' + mon[i-1] + '</OPTION>');
            }else {
                document.write('<OPTION VALUE="' + i + '">' + mon[i-1] + '</OPTION>');
            }

        }
        document.write('</SELECT></FONT><FONT CLASS="frm"><SELECT NAME="' + fyear + '" SIZE="1" WIDTH="5" CLASS="frm">');
        document.write('<OPTION VALUE="">Year</OPTION>');
        for(i=1920;i<=1985;i++) {
            if (i == year1) {
                document.write('<OPTION VALUE="' + i + '" SELECTED="SELECTED">' + i + '</OPTION>');
            }else {
                document.write('<OPTION VALUE="' + i + '">' + i + '</OPTION>');
            }

        }
        document.write('</SELECT></FONT>');
}
////////////////////////////////////////////
function trimSpace(frmElement)
{

     var stringToTrim = frmElement.value;
     var len = stringToTrim.length;
     var front;
     var back;
     for(front = 0; front < len && (stringToTrim.charAt(front) == ' ' || stringToTrim.charAt(front) == '\n' || stringToTrim.charAt(front) == '\r' || stringToTrim.charAt(front) == '\t'); front++);
     for(back = len; back > 0 && back > front && (stringToTrim.charAt(back - 1) == ' ' || stringToTrim.charAt(back - 1) == '\n' || stringToTrim.charAt(back - 1) == '\r' || stringToTrim.charAt(back - 1) == '\t'); back--);

     frmElement.value = stringToTrim.substring(front, back);
}

function isEmpty(formname, fieldname) {
        itemvalue = eval('document.' + formname + '.' + fieldname + '.value');
        if(itemvalue.length <= 0) {
                return true;
        }
        icount = 0;
        for(i=0;i<itemvalue.length;++i) {
                if(itemvalue.charAt(i) != ' ') {
                        ++icount;
                }
        }
        if(icount > 0) {
                return false;
        }
        else {
                return true;
        }
}
function countOccurance(str, charecter)
{
     var j;
     var count;
     for(j = -1, count = 0; -1 != (j = str.indexOf(charecter, j+1)); count++);
     return count;
}
function checkMail(email, mandatory)
{
     if(mandatory && !(email.length))
          return false;

     if(!(email.length))
          return true;

     if(!(checkAllowedChars(email, 'AN@-_.<>')
          && countOccurance(email, '@') == 1
          && email.indexOf('@') != 0
          && email.lastIndexOf('@') != (email.length - 1)
          && countOccurance(email, '<') <= 1
          && countOccurance(email, '>') <= 1
          && ((email.lastIndexOf('>') == (email.length - 1) && email.indexOf('<') != -1)
               || (email.indexOf('>') == -1 && email.indexOf('<') == -1))
          && countOccurance(email, '.') >= 1
          && email.indexOf('..') == -1
          && email.indexOf('.') != 0
          && email.lastIndexOf('.') != (email.length - 1)))
     {
          return false;
     }

     afterAt = email.substring(email.indexOf('@')+1);
     if(!(afterAt.indexOf('.') != 0 && afterAt.lastIndexOf('.') != (afterAt.length - 1)))
          return false;

     beforeAt = email.substring(0, email.indexOf('@'));
     if(!(beforeAt.indexOf('_') != 0
      && beforeAt.indexOf('-') != 0
      && beforeAt.indexOf('.') != 0
      && beforeAt.lastIndexOf('.') != (beforeAt.length - 1)))
     {
          return false;
     }
     return true;
}
function openWindow(type, page, wd, ht) {
	 windowFeatures = "";
     if (type == 'showContent') {
          window_width = wd;
          window_height = ht;
          window_top = (screen.availHeight-window_height)/2
          window_left = (screen.availWidth-window_width)/2
          windowFeatures += "width=" + window_width + ",height=" + window_height + ",top="
          windowFeatures += window_top
          windowFeatures += ",left="
          windowFeatures += window_left
          windowFeatures += ",scrollbars=1"
     }
     window.open(page,type,windowFeatures)
}