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

function checkSearch(frm)
{
 	if((frm.act_id.value=="0")&&(frm.skill_id.value=="0")&&(frm.loc_id.value=="0"))
	{
		alert("Please Select Any Search Criteria");
		frm.act_id.focus();
		return false
	}
	
}
function resetForm()
{
	document.frmSearch.act_id.value=0;
	document.frmSearch.skill_id.value=0;
	document.frmSearch.loc_id.value=0;
}
	
	
function MM_openBrWindow(theURL,winName,features){ 
	window.open(theURL, winName, features);
}

function checkValidDate(dateStr){
	if(dateStr.length<1) { return false; }
	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 year, invalid date
	for (var x=0; x < dateYear.length; x++) {
		digit = dateYear.substring(x, x+1);
		if ((digit < "0") || (digit > "9")) { 
			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;
		}
	}

	// 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 year to a number
	var numYear = parseInt(dateYear);
	if ((dateYear.length != 4)) { 
		return false; 
	}

	// convert the text month to a number
	var numMonth = parseInt(dateMonth);
	if ((numMonth <= 0) || (numMonth > 12)){
		return false;
	}
	
	// convert the text day to a number
	var numDay = parseInt(dateDay);
	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;
		}
	}

	// 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
	
	strDate = numYear + "-" + numMonth + "-" + numDay;
	return strDate;
}// checkValidDate()


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

