function setFocus() {
	mainLen = document.forms.length;
	if(mainLen > 0) {
		frmLen = document.forms[0].elements.length;
		if(!isNaN(frmLen) && frmLen > 0) {
			for(frmCount = 0; frmCount < frmLen; frmCount ++) {
				if(document.forms[0].elements[frmCount].type == "text") {
					document.forms[0].elements[frmCount].focus();
					return;
				}
			}
		}
	}
}

function crewshow(id){
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;

	sTop = (sHeight - 400) / 2;
	sLeft = (sWidth - 650) / 2;
	
	window.open("crewFrame.asp?empid=" + id, "shipmateWin", "width=650,height=400,top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=1,resizable=1");
	return;
}

function openBiodata() {
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;

	sTop = (sHeight - 400) / 2;
	sLeft = (sWidth - 650) / 2;
	
	window.open("biodataFrame.asp", "shipmateWin", "width=650,height=400,top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=1,resizable=1");
	return;
}

function getMonthValue(monthName) {
	var mValArray = new Array("", "January","February","March","April","May","June","July", "August","September","October","November","December");
	for(mId = 0; mId <= 12; mId ++) {
		if(mValArray[mId] == monthName) {
			return mId;
		}
	}
}

function setDD(mainDD, subDD, defValue, theForm) {
	mainElement = eval("document." + theForm + "." + mainDD);
	subElement = eval("document." + theForm + "." + subDD);
	mainIndex = mainElement.selectedIndex;

	if(mainIndex == 0) {
		subArray = fullData.split("|");
	}
	else {
		itemArray = itemSet.split("~");
		
		subArray = itemArray[mainIndex - 1].split("|");
	}
	
	subElement.length = subArray.length + 1;
	subCount = 0;
	subIndex = 0;
	for(i = 0; i < subArray.length; i ++) {
		nameValue = subArray[i].split("^");

		if(nameValue[0] != "undefined" && nameValue[0].length > 0 && nameValue[1].length > 0) {
			subCount ++;
			subElement[subCount].value = nameValue[0];
			subElement[subCount].text = nameValue[1];
			if(nameValue[0] == defValue) {
				subIndex = subCount;
			}
		}
	}
	if(subCount == 0) {
		subElement.length = 1;
	}
	subElement.selectedIndex = subIndex;
}

/////////////////////////////////////////////////////////////////////////////////////////
// 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;
}


/////////////////////////////////////////////////////////////////////////////////////////
// Validates a email string passed and returns true if it is a valid email ID 
// and false other wise.
/////////////////////////////////////////////////////////////////////////////////////////
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;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Checks the date values passed into this function and validates the date
/////////////////////////////////////////////////////////////////////////////////////////
function checkDate(ddVal, mmVal, yyVal) {
	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 the specified string contains only allowed characters
/////////////////////////////////////////////////////////////////////////////////////////
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) {
	lowDateSplit = lowDate.split('/');
	highDateSplit = highDate.split('/');

	date1 = new Date();
	date2 = new Date();
	
	if(lowDateSplit[0] < 10) {
		lowDateSplit[0] = "0" + lowDateSplit[0];
	}

	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;
	}
}

