
var userMax   = 50; //max length of a username
var minLength = 2;  //min length of a password
var maxLength = 15; //max length of a password

function ValidEmail(strEmail){
//MD recoded Nov 05,2009 to be used in the next release
/*
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if (strEmail.match(emailRegEx))
	{	
			//strEmail.focus();
			return true;

	}

	alert("Please enter a valid email address.");
	return false;  */
		
	if (strEmail.length >= userMax){		
		//invalid Email length
		return false;
	} else {
		if (strEmail.match(/^([a-zA-Z0-9_\-\.]+)@{1}([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,})$/)){
			//valid
			return true;
		} else {
			//invalid
			return false;
		}
	}
	
	
}

function ValidateEmailWithLangauage(strEmail,EnglishFlag){
//MD Created Nov 05,2009

	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if (strEmail.match(emailRegEx))
	{	
			//strEmail.focus();
			return true;

	}

	if (EnglishFlag)
		alert("Please enter a valid email address.");
	else
		alert("Veuillez entrer une adresse de courriel valid.");
		
	return false;
	
}


function ValidateEmailWithLangauageWhenEntered(strEmail,EnglishFlag){
//MD Created Nov 05,2009

	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	if(strEmail.length > 0)
	{

		if (strEmail.match(emailRegEx))
		{	
				//strEmail.focus();
				return true;

		}

		if (EnglishFlag)
			alert("Please enter a valid email address.");
		else
			alert("Veuillez entrer une adresse de courriel valid.");
			
		return false;
	}
	else
	{
		return true;
	}
	
}

function DoubleEmailValidation(objEmail1,objEmail2,EnglishFlag){
//MD created Nov 05,2009
	var strEmail1= objEmail1.value;
	var strEmail2= objEmail2.value;
	
	if (ValidateEmailWithLangauage(strEmail1,EnglishFlag) == false)
	{
		objEmail1.focus();
		return false;
		
		
	}
		
	if (ValidateEmailWithLangauage(strEmail2,EnglishFlag) == false)
	{
		objEmail2.focus();		
		return false;
		
	}		
	

	if (strEmail1 != strEmail2)
	{
		if (EnglishFlag)
			alert("The two email addresses you have entered do not match.");
		else
			alert("Les deux adresses courriels que vous avez entres ne correspondent pas.");
			
			
			objEmail2.focus();			
			return false;

	}
	
	return true;
	
}


function ValidURL(strURL){

if (strURL.indexOf(".")<0){
		
		//required characters in url address
		return false;
	}
	if (strURL.match(/[`@!#$%\*\(\)+=\|\{\}\[\]'":;\?><,]/)){
				 
		//invalid characters in an email address
		return false;
	}
	
	return true;
}

function ValidUserName(strUserName){

	if (strUserName.match(/[^a-zA-Z0-9@._]/)){
				 
		//invalid characters in Username
		return false;
	}
	if (strUserName.length >= userMax || strUserName.length < minLength){
		
		//invalid Username length
		return false;
	}
	return true;
}

function ValidUserNameNew(strUserName){


	if (strUserName.length >= userMax || strUserName.length < minLength){
		
		//invalid Username length
		return false;
	}
	return true;
}

function ValidChannelName(strChannelName){

	if (strChannelName.match(/[^a-zA-Z0-9@._-]/)){
				 
		//invalid characters in Username
		return false;
	}
	
	return true;
}

function CheckBoxChecked(objChk){

	if (objChk.checked == false){
		return false;
	}
	
	return true;
}


function LegalLocalPhone(strPhoneNumber){
	
	var strVal;
	
	strVal = strPhoneNumber;
	if (strPhoneNumber.length != strVal.length) {
		return false;
	}
	
	if (strVal.length < 10) {
		 return false;
	}
	if (strVal.length > 10) {
		 return false;
	}
	
	if ((strVal.indexOf("0")== 0)){
		return false;
	}
	
	return true;	
}

function LegalSecondaryPhone(strPhoneNumber){
	
	var strVal;
	
	strVal = strPhoneNumber;
	if (strPhoneNumber.length != strVal.length) {
		return false;
	}
	
	if (strVal.length == 0) {
		 return true;
	}	
	
	if (strVal.length < 10) {
		 return false;
	}
	if (strVal.length > 10) {
		 return false;
	}
	
	if ((strVal.indexOf("0")== 0)){
		return false;
	}
	
	return true;	
}


function ValidQuantity(strQty){
//MD created March, 2010
	var intVal = strQty;
	
	intVal = intVal.replace(/[^0-9]/g,"");
	if ( (intVal != strQty) || (intVal == 0) || (intVal.length > 2) ) {
		return false;
	}
	return true;
	
}

function ValidateHardware(strQty319,strQty505){
//MD created March, 2010

	var intVal319 = strQty319.value;
	var intVal505 = strQty505.value;
	
	intVal319 = intVal319.replace(/[^0-9]/g,"");
	intVal505 = intVal505.replace(/[^0-9]/g,"");

	if ( (intVal319 != strQty319.value)  || (intVal319.length > 2) || (intVal319 > 6)) {
		strQty319.focus();
		return false;
	}
	else if ( (intVal505 != strQty505.value) || (intVal505.length > 2) || (intVal505 > 6)) {
		strQty505.focus();
		return false;		
	}
	else if (intVal319 == 0 && intVal505 == 0){
		strQty319.focus();
		return false;
	}
	
	return true;
	
}


function LegalModem(strSerialNumber){
	
	var strVal;
	
	strVal = strSerialNumber;
	strVal = strVal.replace (/[^0-9A-Fa-f]/g,"");
	
	if (strSerialNumber.length != strVal.length) {
		return false;
	}
	
	if (strVal.length < 7  || strVal.length > 12) {
		 return false;
	}
	
	return true;	
}

function LegalPassword(strPsswd){
	
	var strVal;
	strVal = strPsswd;
	
	strVal = strVal.replace(/[^a-zA-Z0-9]/g,"");
	if (strPsswd.length != strVal.length) { 
		
		return false;
	}
	if (strPsswd.length < minLength) { 
		
		return false;
	}
	return true;
}	


function LegalPasswordOld(strPsswd){
	
	if ((strPsswd.length < minLength)||(strPsswd.length > maxLength)){
		//invalid password length
		return false;
	}
	return true;
}

function ValidCustomerName(strCName){
	
	if ((strCName.length < 1)||(strCName.length > 50)){
		//invalid Customer Name length
		return false;
	}
	return true;
}

function ValidAccountNumber2(strAcctNo){
	var intVal = strAcctNo;
	
	intVal = intVal.replace(/[^0-9]/g,"");
	if ( (intVal != strAcctNo) || (strAcctNo.length < 5) || (strAcctNo.length > 7) ) {
		return false;
	}
	return true;
}

function ValidPartAccountNumber(strAcctNo){
	var intVal = strAcctNo;
	
	intVal = intVal.replace(/[^0-9]/g,"");
	if ( (intVal != strAcctNo) || (strAcctNo.length < 5) || (strAcctNo.length > 8) ) {
		return false;
	}
	return true;
}

function ValidPartAcctNumLength4(strAcctNo){
	var intVal = strAcctNo;
	
	intVal = intVal.replace(/[^0-9]/g,"");
	if ( (intVal != strAcctNo) || (strAcctNo.length < 4) || (strAcctNo.length > 4) ) {
		return false;
	}
	return true;
}


function ValidDescription(strDesc){
	
	if ((strDesc.length < 1)||(strDesc.length > 500)){
		//invalid length
		return false;
	}
	return true;
}


function ValidAddress(strAddress){
	
	if ((strAddress.length < 1)||(strAddress.length > 60)){
		//invalid Address length
		return false;
	}
	return true;
}

function ValidChoice(strChoice){
	
	if (strChoice=="None"){
		return false;
	}
	return true;
}

function ValidOption(listBox){

	if (listBox.selectedIndex==0 ){
		return false;
	}
	
	return true;
}


function ValidAccountNumber(strAcct){

	var intVal = strAcct;
	
	intVal = intVal.replace(/[^0-9]/g,"");
	if ( (intVal != strAcct) || (strAcct.length < 11) ) {
		return false;
	}
	return true;
	
}

function ValidSuiteNumber(strSuite){
	
	if (strSuite == "NA")
	{
		return true;
	}
	
	if ((strSuite != "") || (strSuite != "NA"))
	{
		if ((strSuite.length < 1) || (strSuite.length > 5)) 
		{
			return false;
		}
	}
	
	return true;
}

function ValidPostalCode(strPCode){
	
	if (strPCode.length > 6){
		strPCode = strPCode.replace(/[^a-zA-Z0-9]/g,"");
		if (strPCode.length > 6){
			return false;
		}
	}
	if (strPCode.match(/[A-Za-z]\d[A-Za-z]\d[A-Za-z]\d/)){
		return true;
	}
	return false;
}

function ValidCorporateNumber(strCorpNumb){
	
	if (strCorpNumb.length > 14){
		strCorpNumb = strCorpNumb.replace(/[^a-zA-Z0-9]/g,"");
		if (strCorpNumb.length > 14){
			return false;
		}
	}
	if (strCorpNumb.match(/\d\d\d\d\d\d\d\d\d[A-Z][A-Z]\d\d\d/)){
		return true;
	}
	return false;
}

function AmountREQ(strAMT){
	
	var strVal;
	
	strVal = strAMT;
	if (strAMT.length != strVal.length) {
		return false;
	}
	
	if (strVal.length < 2) {
		 return false;
	}
	
	if ((strVal.indexOf("0")== 0)){
		return false;
	}
	
	return true;
}

function LegalPIN(strPIN){
	
	var intVal = strPIN;
	
	intVal = intVal.replace(/[^0-9]/g,"");
	if ( (intVal != strPIN) || (strPIN.length != 4) || (parseInt(strPIN,10) < 1000) || (parseInt(strPIN,10) > 9999) ) {
		return false;
	}
	return true;
}



function LegalFriendlyName(strFName){
	var strTemp = strFName;
	
	if (strTemp.length > 30){
		return false;
	}
	
	strTemp = strTemp.replace(/[^a-zA-Z0-9 @]/g,"");
	if (strTemp!= strFName){
		return false;
	}
	return true;
}

function ValidQty(intQty){

	if (intQty.match(/[^0-9]/)){
				 
		//invalid characters in intQty
		return false;
	}
	if (intQty.length < 1) {
		 return false;
	}
	return true;
}

// Returns true if character c is a digit (0 .. 9).
function isDigit (c)

{
    return ((c >= "0") && (c <= "9"))
}

// Check whether string s is empty.
function isEmpty(s)

{
    return ((s == null) || (s.length == 0))
}

function isInteger (strInput)
{
   var i;

    // Search through string's char one by one until we find a non-numeric char
    // When we do, return false; if we don't, return true

    for (i = 0; i < strInput.length; i++)
    {   
        // Check that current character is number.
        var c = strInput.charAt(i);

        if (!isDigit(c)) return false;
    }
    // All characters are numbers.
    return true;
}

function isValidTime(timeStr)
{
	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds and AM/PM are optional.

	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

	var matchArray = timeStr.match(timePat);
	
	if (matchArray == null)
	{
	   return false;
	}

	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];

	if (second=="")
	{  
	   second = null;
	}
	
	if (ampm=="")
	{
	   ampm = null
	}

	if (hour < 0 || hour > 23)
	{
	   return false;
	}

	if (hour <= 12 && ampm == null)
	{
	   return false;
	}

	if (hour > 12 && ampm != null)
	{
	   return false;
	}

	if (minute < 0 || minute > 59)
	{
	   return false;
	}

	if (second != null && (second < 0 || second > 59))
	{
	   return false;
	}

	return true;
}



function ValidPart4DigitAccountNumber(strAcctNo){
//Validating for 4 digit Account number
	var intVal;
	var bReturn;
	intVal = strAcctNo;
	bReturn = false;
	
	intVal = intVal.replace(/[^0-9]/g,"");
	if ( (intVal != strAcctNo) || (strAcctNo.length < 3) || (strAcctNo.length > 4) ) 
		bReturn= false;
	else
		bReturn= true;
		
		
		return bReturn;

}

function TextControlIsFilledIn(objControl) {
	if (objControl == null) {
		return false;
	}
	var strVal = objControl.value
	if ((strVal != null) && (strVal.length != null) && (strVal.length > 0)) {
		return true;
	}
	return false;
}

function TextControlIsBlank(objControl) {
	if (objControl == null) {
		return false;
	}
	var strVal = objControl.value
	if ((strVal != null) && (strVal.length != null) && (strVal.length == 0)) {
		return true;
	}
	return false;
}

function DropDownControlIsFilledIn(objControl) {
	if (objControl == null) {
		return false;
	}
	var strVal = objControl.value
	if ((strVal != null) && (strVal.length != null) && (strVal.length > 0) && (strVal != "None") && (strVal != "Aucun")) {
		return true;
	}
	return false;
}

function PhoneControlIsValid(objPhone, isRequired) {
	if ((! isRequired) && (TextControlIsBlank(objPhone))) {
		return true;
	}
	
	if (! TextControlIsFilledIn(objPhone)) {
		return false;
	}
	
	var stripped = objPhone.value.replace(/[\(\)\.\-\ ]/g, '');
	if ((isInteger(stripped)) && (stripped.length == 10)){
		return true;
	}
	return false;
}

function AccountNumberControlIsValid(objAccountNumber, isRequired) {
	if ((! isRequired) && (TextControlIsBlank(objAccountNumber))) {
		return true;
	}
	
	if (( TextControlIsFilledIn(objAccountNumber)) && (isInteger(objAccountNumber.value)) && (objAccountNumber.value.length == 8)){
		return true;
	}
	return false;
}

function PostalCodeControlIsValid(objPostalCode, isRequired) {
	if ((! isRequired) && (TextControlIsBlank(objPostalCode))) {
		return true;
	}
	
	if (( TextControlIsFilledIn(objPostalCode)) && (ValidPostalCode(objPostalCode.value))) {
		return true;
	}
	return false;
}

function CityControlIsValid(objCity) {
	return TextControlIsFilledIn(objCity);
}

function ProvinceControlIsValid(objProvince) {
	return DropDownControlIsFilledIn(objProvince);
}

function EmailControlIsValid(objEmail) {
	if (( TextControlIsFilledIn(objEmail)) && (ValidEmail(objEmail.value))) {
		return true;
	}
	return false;
}

function DepartmentControlIsValid(objDepartment) {
	return DropDownControlIsFilledIn(objDepartment);
}

function CommentFieldControlIsFilledIn(objCommentField) {
	if (TextControlIsFilledIn(objCommentField)) {
		return true;
	}
	return false;
}

function CommentFieldControlIsValid(objCommentField) {
	// we confirm the field is filled in because we don't trust the calling code to do so
	if (( TextControlIsFilledIn(objCommentField)) && (objCommentField.value.length <= 20000)){
		return true;
	}
	return false;
}

function DealerNumberControlIsValid(objDealerNumber) {
	if (( TextControlIsFilledIn(objDealerNumber)) && (objDealerNumber.value.length == 8) && (isInteger(objDealerNumber.value))){
		return true;
	}
	return false;
}

function InstallerNumberControlIsValid(objInstallerNumber) {
	if (( TextControlIsFilledIn(objInstallerNumber)) && (objInstallerNumber.value.length == 4) && (isInteger(objInstallerNumber.value))){
		return true;
	}
	return false;
}

function ZeroPad(stringToPad, lengthRequested) {
	if ((stringToPad == null) || (stringToPad.length >= lengthRequested)){
		return stringToPad;
	}
	while (stringToPad.length < lengthRequested) {
		stringToPad = "0" + stringToPad;
	}
	return stringToPad;
};

// this will remove leading zeros leaving the requested length string
// (it doesn't remove anything except zeros)
function UnZeroPad(stringToUnPad, lengthRequested) {
	if ((stringToUnPad == null) || (stringToUnPad.length <= lengthRequested)){
		return stringToUnPad;
	}
	while (stringToUnPad.length > lengthRequested) {
		if (stringToUnPad.substring(0,1) == "0") {
			stringToUnPad = stringToUnPad.substring(1,stringToUnPad.length);
		} else {
			break;
		}
	}
	return stringToUnPad;
};

function DateControlIsValid(objDate, isRequired) {
	if ((! isRequired) && (TextControlIsBlank(objDate))) {
		return true;
	}
	
	if ( ! TextControlIsFilledIn(objDate)) {
		return false;
	}
	
	var strDate = objDate.value;
	if (strDate.length == 10)  {
		// they may have skipped the leading zero on the day field so add it in 
		strDate = ZeroPad(strDate, 11);
	}
	
	if (strDate.length != 11) {
		return false;
	}
	
	// check format is dd-MMM-yyyy
	var dateRegEx = /^[0-9][0-9]-[a-zA-Z][a-zA-Z][a-zA-Z]-[0-9][0-9][0-9][0-9]$/;

	if (! strDate.match(dateRegEx)) {
		return false;
	}
	
	var day=strDate.substring(0,2);
	var month=strDate.substring(3,6);
	var year=strDate.substring(7,11);
	
	// parse the date
	var myDateMilliseconds = Date.parse(month + " " + day + ", " + year);
	var myDate = new Date(myDateMilliseconds);
	// make sure it is valid
	// note: 30-Feb-2010 will get parsed as March 2nd so ensure the day matches to avoid this case
	if (( Object.prototype.toString.call(myDate) != "[object Date]" ) || (myDate.getDate() != parseInt(day))) {
		return false;
	}
	
	return true;
}

function SerialNumberControlIsValid(objSerialNumber, isRequired) {
	if ((! isRequired) && (TextControlIsBlank(objSerialNumber))) {
		return true;
	}
	
	if (! TextControlIsFilledIn(objSerialNumber)) {
		return false;
	}
	
	var intRegEx = /^[0-9]*$/;
	if (! objSerialNumber.value.match(intRegEx)) {
		return false;
	}
	return true;
}
