/***************************************************************************************/
// General functions
/***************************************************************************************/
/////////////////////////////////////////////////////////////////////////////////////////
// Checks whether a string is a valid email address.
/////////////////////////////////////////////////////////////////////////////////////////

function LeapYear(year) 
{
    if ((year/4)   != Math.floor(year/4))   return false;
    if ((year/100) != Math.floor(year/100)) return true;
    if ((year/400) != Math.floor(year/400)) return false;
    return true;
}
function getDaysofMonth(month,year)
{
	daysofmonth   = new Array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	daysofmonthLY = new Array( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	arryindx=parseInt(month)-1;
	if (LeapYear(year))
	{
  		daysofmonth = daysofmonthLY;
	}
	return daysofmonth[arryindx];
}
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;
}
function formatCheck(dateStr) 
{
	dateSplit = dateStr.split("/");
	if(dateSplit.length != 3) 
	{
		alert("Invalid Date");
		return false;
	}
	if (isNaN(dateSplit[0])==true || isNaN(dateSplit[1])==true || isNaN(dateSplit[2])==true)
	{
		alert("Invalid Date");
		return false;
	}
		
	if(trimSpaces(dateSplit[0]).length <= 0 || parseInt(trimSpaces(dateSplit[0])) > 12 || parseInt(trimSpaces(dateSplit[0])) == 0)
	{
		alert("Invalid Month for Date");
		return false;
	}
	
	if(trimSpaces(dateSplit[1]).length <= 0)
	{
		alert("Invalid Day for Date");
		return false;
	}
	if(parseInt(dateSplit[1]) > getDaysofMonth(dateSplit[0],dateSplit[2]))
	{
		alert("Invalid Day for Month");
		return false;
	}
	if(trimSpaces(dateSplit[2]).length <= 3) 
	{
		alert("Invalid Year for Date");
		return false;
	}
	return true;
}
function CheckPhoneNumber(TheNumber) {
	
	if (TheNumber.length==1){
	if (TheNumber.charAt(0)==0) return false;
	}
	if (TheNumber.length<7) return false;
	var valid = 1
	var GoodChars = "0123456789-+/ ,"
	var i = 0
	for (i =0; i <= TheNumber.length -1; i++) {
	 if (GoodChars.indexOf(TheNumber.charAt(i)) == -1) {
	  valid = 0
	 } // End if statement
	} // End for loop
	return valid
}// End of Check
function CheckPinCode(TheNumber) {
	
	if (TheNumber.length==1){
	if (TheNumber.charAt(0)==0) return false;
	}
	if (TheNumber.length<6) return false;
	var valid = 1
	var GoodChars = "0123456789"
	var i = 0
	for (i =0; i <= TheNumber.length -1; i++) {
	 if (GoodChars.indexOf(TheNumber.charAt(i)) == -1) {
	  valid = 0
	 } // End if statement
	} // End for loop
	return valid
}// End of Ch
function isDigit (c) {
  return ( (c >= '0') && (c <=  '9') )
}
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;
}
function frmSubmit(){
		
	if(trimSpaces(document.form1.str_Username.value)==""){
		alert("Enter the user name");
		document.form1.str_Username.value="";
		document.form1.str_Username.focus();
		return false;
	}
	
	if(trimSpaces(document.form1.str_Password.value)==""){
		alert("Enter the password");
		document.form1.str_Password.value="";
		document.form1.str_Password.focus();
		return false;
	}else{
		if(document.form1.str_Password.value!=document.form1.str_Password1.value){
			alert("Password mismatch");
			document.form1.str_Password.focus();
			return false;
		}
	}
	if(trimSpaces(document.form1.str_FirstName.value)==""){
		alert("Enter the name");
		document.form1.str_FirstName.value="";
		document.form1.str_FirstName.focus();
		return false;
	}
	if(trimSpaces(document.form1.str_fatherName.value)==""){
		alert("Enter the Father Name");
		document.form1.str_fatherName.value="";
		document.form1.str_fatherName.focus();
		return false;
	}
	if(trimSpaces(document.form1.str_Adr1.value)==""){
		alert("Enter the address");
		document.form1.str_Adr1.value="";
		document.form1.str_Adr1.focus();
		return false;
	}
	
	if(trimSpaces(document.form1.str_district.value)==""){
		alert("Enter the District");
		document.form1.str_district.value="";
		document.form1.str_district.focus();
		return false;
	}
	if(trimSpaces(document.form1.str_Zip.value)==""){
		alert("Enter a valid pin code");
		document.form1.str_Zip.value="";
		document.form1.str_Zip.focus();
		return false;
	}else{
		if(CheckPinCode(document.form1.str_Zip.value)==0){
			alert("Enter a valid  pin code");
			document.form1.str_Zip.value="";
			document.form1.str_Zip.focus();
			return false;
		}
	}
	if(trimSpaces(document.form1.str_Phone.value)==""){
		alert("Enter the phone number");
		document.form1.str_Phone.value="";
		document.form1.str_Phone.focus();
		return false;
	}else{
		if(CheckPhoneNumber(document.form1.str_Phone.value)==0){
			alert("Enter a valid  phone number");
			document.form1.str_Phone.value="";
			document.form1.str_Phone.focus();
			return false;
		}
	}
	if(trimSpaces(document.form1.str_Email.value)==""){
		alert("Enter the email id");
		document.form1.str_Email.focus();
		return false;
	}else{
		if(checkEmail(document.form1.str_Email.value)==false){
			document.form1.str_Email.focus();
			return false;
		}
	}
	if(document.form1.checkbox.checked==false){
		alert( " Please check the terms and condition " );
		document.form1.checkbox.focus();
		return false;
		}
	
}

function  frmSubmit1(){
	if(trimSpaces(document.quizform.str_Username.value)==""){
		alert("Enter the user name");
		document.quizform.str_Username.focus();
		return false;
	}
	if(trimSpaces(document.quizform.str_Password.value)==""){
		alert("Enter the  password");
		document.quizform.str_Password.value="";
		document.quizform.str_Password.focus();
		return false;
	}	
}


