/* File Name : RegValidate.js Author : Kapil Gupta Date : 17/01/2001 Revisions : None Purpose : JavaScript Validation code for Registration Module */ 



function fnRegValidate() 
{ 
	// FOR USER NAME
	
	if (!(fnCheckLength("Name",document.getElementById("txtFName"),1,40))) return (false); 
	
	// FOR LAST NAME 
	if (!(fnCheckLength("Last Name",document.getElementById("txtLName"),1,40))) return (false); 
	
	// FOR EMAIL 
	if (!(fnCheckLength("Email",document.getElementById("txtEmail"),1,40))) return (false); 
	
	// VALID EMAIL FORMAT 
	if (!(fnIsValidEmail(document.getElementById("txtEmail")))) return (false); 
	
	// FOR ADDRESS 
	if(!(fnCheckLength("Address",document.getElementById("txtAddress"),1,100))) return (false); 
	
	// FOR ATLEAST ONE PHONE NO
	
	if(document.getElementById("txtDayPhone").value=="" && document.getElementById("txtMobile").value=="" && document.getElementById("txtEvePhone").value=="")
	{
		alert("Please enter atleast one Phone Number! Please retry");
		document.getElementById("txtDayPhone").focus();
		return(false);
	}
	// FOR CITY 
	if (!(fnCheckLength("City",document.getElementById("txtCity"),1,40))) return (false); 
	
	//FOR DATE (DOB)
	
	
	if(document.getElementById("cmbDay").value>0 || document.getEmelentById("cmbMonth").value>0 || document.getElementById("cmbYear").value>0)
		{
			var dt1 = document.getElementById("cmbMonth").value + "/" + document.getElementById("cmbDay").value + "/" + document.getElementById("cmbYear").value;
			if(ValidateDate(dt1)==false)
			{ 
				return(false);
			}
		}
	


	// FOR LOGIN NAME 
	if(!document.getElementById("radPlan1").checked)
	{
		if (document.getElementById("txtLogin").value.length>8)
		{
			alert("Sign In Id cannot be more than 12 characters! Please retry.");
			document.getElementById("txtLogin").focus();
		 	return (false); 
		}
		else if (document.getElementById("txtLogin").value.length<1)
		{
			
			alert("Invalid Length of Sign In Id! Please retry.");
			document.getElementById("txtLogin").focus();
		 	return (false); 
		}
	}
	else
	{
		if (!(fnCheckLength("Sign In Id",document.getElementById("txtLogin"),1,12))) return (false); 
	}

	// to Restrict manu as prefix
	if(document.getElementById("txtLogin").value.length>3 && document.getElementById("txtLogin").value.substring(0,4).toLowerCase()=="manu")
        	{
	     		alert('Sign In Id cannot start with manu');
			document.getElementById("txtLogin").focus();
			return(false);
        	}


	// CHECKING FOR NOSPECIAL CHARS 
	if (!(fnIsValidAlpha("Sign In Id",document.getElementById("txtLogin")))) return (false); 
	
	// CHECKING FOR SPACES IN BETWEEN 
	if (!(fnCheckWhiteSpaces("Sign In Id",document.getElementById("txtLogin")))) return (false); 

	// FOR PASSWORD1 
	if (!(fnCheckLength("Password",document.getElementById("txtPassword"),6,12))) return (false); 
	
	// PASSWORD CANNOT BE SAME AS THE SIGN IN NAME 
	if(document.getElementById("txtLogin").value.toUpperCase() == document.getElementById("txtPassword").value.toUpperCase()) 
	{ 
		alert("Password cannot be same as Sign In Id"); 
		document.getElementById("txtPassword").focus(); 
		return (false); 
	} 

	//CHECKING FOR NO SPECIAL CHARS 
	if(!(fnIsValidAlpha("Password",document.getElementById("txtPassword")))) return (false); 
	
	//CHECKING FOR SPACES IN BETWEEN 
	if(!(fnCheckWhiteSpaces("Password",document.getElementById("txtPassword")))) return (false); 
	
	//alert("Password1 Checked"); 
	// CHECKING FOR PASSWORD1 MATHCING WITH PASSWORD2 
	if (document.getElementById("txtPassword").value != document.getElementById("txtConfirmPassword").value) 
	{ 
		alert("The passwords do not match! Please retry.."); 
		document.getElementById("txtPassword").value = ""; 
		document.getElementById("txtConfirmPassword").value = ""; 
		document.getElementById("txtPassword").focus(); 
		return (false); 
	} 
	
	// FOR OTHER PROFESSION 
	//alert(document.frmReg.cmbProfession.selectedIndex); 
	if(document.getElementById("cmbProfession").value == 18) 
	{ 
		if(document.getElementById("txtProfession").value == "") 
		{ 
			alert("Please specify the other profession");
			document.getElementById("txtProfession").focus(); 
			return (false); } 
		} 
		if(!document.getElementById("chkAgreed").checked) 
		{ 
			alert("Please go through the terms and conditions"); 
			document.getElementById("chkAgreed").focus(); 
			return (false); 
		} 
	
		//alert(document.frmReg.cmbPlan.value); 
		//FOR SCHEME CHOSEN 
		if(document.getElementById("cmbPlan").value=='') 
		{ 
			alert("Please select the Subscription Scheme!"); 
			//document.frmReg.cmdPlan.focus(); 
			return (false); 
		} 
		
		//alert("Validations over");
		return (true); 
	} 



var dtCh= '/';
var minYear=1900;
var maxYear=2100;

function ValidateDate(dt)
{
	if (isDate(dt)==false)
	{
		return false;
	}
    return true;
}

function isDate(dtStr)
{	
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) 
		strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) 
		strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) 
	{
		if (strYr.charAt(0)=="0" && strYr.length>1) 
			strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	
	
	if (pos1==-1 || pos2==-1)
	{
		alert("The date format should be : mm/dd/yyyy");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day");
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date");
		return false;
	}
return true;

}
function DaysArray(n) 
{
	for (var i = 1; i <= n; i++) 
	{
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30;}
		if (i==2) {this[i] = 29;}
	} 
	return this;
}

function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}


function stripCharsInBag(s, bag)
{
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function isInteger(s)
{
	var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

