// Program Name: validate.js
// Author      : Ricardo Perez
// Date        : January 27, 2005
// Purpose     : Contains all the Javascript Program for Calabro Stores

  function isDate(dateString)
  {
		if (dateString.length != 10) return false;
				
		if ((dateString.substring(2,3) == "-") && (dateString.substring(5,6) == "-"))
		{
			var intDD, intMM, intYYYY;
			intDD = parseInt(dateString.substring(0, 2),10);
			intMM = parseInt(dateString.substring(3, 5),10);
			intYYYY = parseInt(dateString.substring(6, 10),10);
			if (intDD < 1) return false;
			if (isNaN(intYYYY)) return false;
			if (intYYYY < 2005) return false;
			switch (intMM)
			{
						case 1:
						case 3:
						case 5:
						case 7:
						case 8:
						case 10:
						case 12:
							if (intDD <= 31) return true;
							break;
						case 4:
						case 6:
						case 9:
						case 11:
							if (intDD <= 30) return true;
						case 2:
							if ((intYYYY % 4 == 0) && !((intYYYY % 100 == 0) && (intYYYY % 400 != 0))) 
							{
								if (intDD <= 29) return true;
							} 
							else 
							{
								if (intDD <= 28) return true;
							}
							break;
							
			}
					
		}
						
		return false;
  }
  function validWord(word)
  {
           if (word == "")
           {
            return false
           }
             return true
  }
  function validEmail(email)
  {
	  invalidChars = " /:,;"
	  if(email =="") 
	  {
		  return false
	  }
	  for (i=0; i<invalidChars.length; i++)
	  {
		  badChar = invalidChars.charAt(i)
		  if (email.indexOf(badChar,0) > -1)
		  {
			  return false
		  }
	  }
	  atPos = email.indexOf("@",1)
	  if (atPos == -1)
	  {
		  return false
	  }
	  if (email.indexOf("@",atPos+1) > -1)
	  {
		  return false
	  
	  }
	  periodPos = email.indexOf(".",atPos)
	  if (periodPos == -1)
	  {
		  return false
	  }
	  if (periodPos+3 > email.length)
	  {
		  return false
	  }
	  return true
  }
  function validPostCode(postC)
  {
           if (postC == "")
           {
            return false
           }
           if (postC.length < 4)
           {
			return false
		   }
           for (i=0; i<postC.length; i++)
           {
            if (postC.charAt(i) < "0" || postC.charAt(i) >  "9")
            {
             return false
            }
           }
            return true
  }
  function validCC3Digit(CardN)
  {
           if (CardN == "")
           {
            return false
           }
           if (CardN.length < 3)
           {
			return false
		   }
           for (i=0; i<CardN.length; i++)
           {
            if (CardN.charAt(i) < "0" || CardN.charAt(i) >  "9")
            {
             return false
            }
           }
            return true
  }  
  function validCardNo(CardN)
  {
           if (CardN == "")
           {
            return false
           }
           if (CardN.length < 16)
           {
			return false
		   }
           for (i=0; i<CardN.length; i++)
           {
            if (CardN.charAt(i) < "0" || CardN.charAt(i) >  "9")
            {
             return false
            }
           }
            return true
  }
 function validNumeric(numbr)
  {
           if (numbr == "" || numbr =="0")
           {
            return false
           }
           for (i=0; i<numbr.length; i++)
           {
            if ((numbr.charAt(i) !=  "." ) && (numbr.charAt(i) < "0" || numbr.charAt(i) >  "9"))
            {
             return false
            }
           }
            return true
  }
function validQuantity(qty)
  {
           if ( qty == "0" || qty == "")
           {
            return false
           }
           return true
  }

 function validQuantityChar(qty)
  {
            if ( qty == "")
           {
            return false
           }
          number1 = "T"
           number2 = "T"
           for (i=0; i<qty.length; i++)
           {
				if (qty.charAt(i) >= "0" && qty.charAt(i) <=  "9" )
				{
					number1 = "T"
				}
                                else
				{
					number2 = "F"
				}
           }
           if (number1 == number2)
           {
				return true
           }
           return false
  }
function FranchiseLocation(myInput1,myInput2,myInput3,myInput4,myInput5,myInput6,myInput7,myInput8,myInput9,myInput10,myInput11,myError) { //check input match
	myError=initError(myError,myInput2.name);
	inputValid=false;
	if (myInput1.checked||myInput2.checked||myInput3.checked||myInput4.checked||myInput5.checked||myInput6.checked||myInput7.checked||myInput8.checked||myInput9.checked||myInput10.checked||(trim(myInput11.value)!= ''))
		inputValid=true;
	setError(inputValid,myError);
}
function typesOfFranchise(myInput1,myInput2,myInput3,myError) { //check input match
	myError=initError(myError,myInput2.name);
	inputValid=false;
	if (myInput1.checked||myInput2.checked||myInput3.checked)
		inputValid=true;
	setError(inputValid,myError);
}
function typesOfFunction(myInput1,myInput2,myInput3,myInput4,myInput5,myError) { //check input match
	myError=initError(myError,myInput2.name);
	inputValid=false;
	if (myInput1.checked||myInput2.checked||myInput3.checked||myInput4.checked||(trim(myInput5.value)!= ''))
		inputValid=true;
	setError(inputValid,myError);
}
  
//Form Validation..............

function initError(myError,myFieldName) { //set error message if not passed
	if (typeof(myError)=='undefined'||trim(myError)=='')
		myError='Error in '+myFieldName+' field input';
	return myError;
}
function initOptions(myOptions) { //set options array if not passed
	if (typeof(myOptions)=='undefined'||trim(myOptions)=='')
		myOptions='required';
	return myOptions;
}

function checkText(myInput,myError,myOptions) { //check text boxes
	myError=initError(myError,myInput.name);
	myOptions=initOptions(myOptions);
	myValue=trim(myInput.value);
	optionsArray=myOptions.split(',') //convert options string to an array
	inputValid=true;
	for (i=0;inputValid&&i<optionsArray.length;i++) { //loop over validation options
		thisOptionArray=optionsArray[i].split('=');
		if (thisOptionArray[0]=='required') //required
			inputValid=(myValue!='');
		else if (thisOptionArray[0]=='alphanumeric') //alphanumeric (can be blank)
			inputValid=(/^\w*$/.test(myValue));
		else if (thisOptionArray[0]=='alpha') //alpha (can be blank)
			inputValid=(/^[a-zA-Z]*$/.test(myValue));
		else if (thisOptionArray[0]=='username') //username (can be blank)
			inputValid=(/^[\w@\.\-]*$/.test(myValue));
		else if (thisOptionArray[0]=='float') //numeric (can be blank)
			inputValid=(!isNaN(myValue));
		else if (thisOptionArray[0]=='posInteger') //positive integer (can be blank)
			inputValid=(/^\d*$/.test(myValue));
		else if (thisOptionArray[0]=='integer') //any integer (can be blank)
			inputValid=(/^-?\d*$/.test(myValue));
		else if (thisOptionArray[0]=='length') //fixed-length
			inputValid=(myValue.length==thisOptionArray[1]);
		else if (thisOptionArray[0]=='RGB') //rgb value (can be blank)
			inputValid=(/^[a-fA-F0-9]{6}$|^$/.test(myValue));
		else if (thisOptionArray[0]=='email') //email address (can be blank)
			inputValid=(/^(([\w\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+){0,1}$/.test(myValue));
		else if (thisOptionArray[0]=='windowsfile') //Windows folder name (can be blank)
			inputValid=(!/[\\\/:*?"<>|]/.test(myValue));
		else if (thisOptionArray[0]=='mobile') //mobile phone in format 0123456789 (can be blank)
			inputValid=(/^[0-9]{10}$|^$/.test(myValue));
		else if (thisOptionArray[0]=='austPhone') //Australian land phone in format 00-12345678 (can be blank)
			inputValid=(/^[0-9]{2}\-[0-9]{8}$|^$/.test(myValue));
		else if (thisOptionArray[0]=='alphaWithSpaces') //alpha with spaces (can be blank)
			inputValid=(/^[a-zA-Z ]*$/.test(myValue));
		else if (thisOptionArray[0]=='alphaNumericWithSpaces') //alphanumeric with spaces (can be blank)
			inputValid=(/^[a-zA-Z0-9 ]*$/.test(myValue));
		else if (thisOptionArray[0]=='numericWithSpaces') //numeric with spaces (can be blank)
			inputValid=(/^[0-9 ]*$/.test(myValue));
	}
	setError(inputValid,myError);
}

function checkRadioOrCheckbox(myInput,myError) { //check radio buttons
	myError=initError(myError,myInput.name);
	inputValid=false;
	if (typeof(myInput.length)=='undefined'&&myInput.checked)
		inputValid=true;
	else {
		for (i=0;i<myInput.length;i++) {
			if (myInput[i].checked) {
				inputValid=true;
				break;
			}
		}
	}
	setError(inputValid,myError);
}

function checkSelect(myInput,myError) { //check single and multiple select lists
	myError=initError(myError,myInput.name);
	inputValid=(myInput.selectedIndex!=-1&&myInput.options[myInput.selectedIndex].value!='-1')
	setError(inputValid,myError);
}

function checkDate(myDD,myMM,myYY,myError,myOptions) { //check date selects (can be blank)
	if (typeof(myError)=='undefined'||trim(myError)=='')
		myError='Invalid date';
	if (typeof(myOptions)=='undefined')
		myOptions='';
	thisDD=myDD.options[myDD.selectedIndex].value;
	thisMM=myMM.options[myMM.selectedIndex].value;
	thisYY=myYY.options[myYY.selectedIndex].value;
	inputValid=true;
	if ((thisDD==''||thisMM==''||thisYY=='')&&myOptions=='required')
		inputValid=false;
	else if ((thisDD!=''&&thisMM!=''&&thisYY!='')) {
		daysInMonth=[31,(thisYY%4)?28:29,31,30,31,30,31,31,30,31,30,31];
		inputValid=(thisDD<=daysInMonth[thisMM-1])
	}
	setError(inputValid,myError);
}

function checkMatch(myInput1,myInput2,myError) { //check that two strings match
	myError=initError(myError,myInput2.name);
	inputValid=(trim(myInput1.value)==trim(myInput2.value));
	setError(inputValid,myError);
}

function trim(myString) {
	return myString.replace(/^\s*/,'').replace(/\s*$/,'');
}

function setError(inputValid,myError) {
	if (!inputValid)
		err_msg+='   - '+myError+'\n';
}

function validationResults() {
	if (err_msg!='') {
		err_msg='Please correct the following and try again:\n'+err_msg;
		alert(err_msg);
		return false;
	}
	return true;
}


