/*****************************************************************************
 *  File Name     : quickRegistration_validation.js                          *
 *  Author        : T.Manimaran  					                         *
 *  Owner         : ICM Solutions                                            *
 *  Creation Date : 19-April-08									             *
 *  Last Modified : S.Kalanidhi	   	                                         *
 *  Modified Date :	18-Aug-11												 *
 *  Usuage        : This javascript class is mainly responsible for 		 *
 * 					validation customer	inputs for quick Registration        *
 *****************************************************************************/

function validate(){   
	var salutation	=	document.getElementById("salutation");
	var fname		=	document.getElementById("firstName");
	var pass		=	document.getElementById("password");
	var con_Pass	=	document.getElementById("confirmPassword");
	var dateOfBirth	=	document.getElementById("dateOfBirth");
	var p_Mail		=	document.getElementById("primaryEmail");
	var day_Code	=	document.getElementById("dayCode");
	var day_Number	=	document.getElementById("dayNumber");
	var mobile		=	document.getElementById("mobileNo");
	var referred	=	document.getElementById("referred");
	//var imgVerify	=	document.getElementById("image_verify");
	var conditions	= 	document.getElementById("termsConditions").checked;
	var referredBy	= 	document.getElementById("referred").value;
	var friendsEmail =  document.getElementById("friendsEmail"); 
	//var stateCode	 =	document.getElementById("stateCode");
	//var otherState	 =	document.getElementById("otherState");

	if(salutation.value=="select"){
	 	alert('Select your salutation');
		salutation.focus();
		return false;
	}
	if(trim(fname.value).length == 0){
	 	alert('Enter your First name');
		fname.focus();
		fname.select();
		return false;
	}
	if(isValidName(trim(fname.value))==false){
		alert('Name should be in alphabets');
		fname.focus();
		fname.select();
		return false;
	}else if(trim(fname.value).length > 50){
		alert('Name cannot be greater than 50 characters');
		fname.focus();
		fname.select();
		return false;
	}

     if(trim(p_Mail.value).length == 0 || trim(p_Mail.value).length > 100){
		alert('Enter your valid E-mail ID');
		p_Mail.focus();
		p_Mail.select();
		return false;
	 }
	
	if(isEmail(trim(p_Mail.value))==false){
		alert('Enter a Valid Email ID');
		p_Mail.focus();
		p_Mail.select();
		return false;
	}
	
	/* This method is to check Password field without blanks, with less than 6 characters
	 * and greater than 16 characters.  */
	if(pass.value.trim().length == 0){
		alert("Enter Password");
		pass.focus();
		pass.select();
		return false;
	}else if(pass.value.length < 6 ){
		alert("Password cannot be less than 6 Characters");
		pass.focus();
		pass.select();
		return false;
	}else if(pass.value.length > 16 ){
		alert("Password cannot be greater than 16 Characters");
		pass.focus();
		pass.select();
		return false;
	}
	/*else if(isAlphaNumeric(pass.value)== false ){
		alert("Enter mix of '6-16' alphabets and numbers for Password");
		pass.focus();
		pass.select();
		return false;
	}
	else if(isInteger(pass.value)){
		alert("Enter mix of '6-16' alphabets and numbers for Password");
		pass.focus();
		pass.select();
		return false;
	}else if(isAlphabetic(pass.value)== true){
		alert("Enter mix of '6-16' alphabets and numbers for Password");
		pass.focus();
		pass.select();
		return false;
	}*/
	/*else if(isLogin(pass.value)==false){
		alert("Enter mix of '6-16' alphabets and numbers for Password");
		pass.focus();
		pass.select();
		return false;
	} */
	/* This method is to check Confirm Password field without blanks, and it should match
	 * Password field.  */	
	if(con_Pass.value.trim().length == 0){
		alert("Enter Confirm Password");
		con_Pass.focus();
		con_Pass.select();
		return false;
	}else if(pass.value !=con_Pass.value){
		alert("Your Confirm Password does not matches with the Password Field");
		con_Pass.focus();
		con_Pass.select();
		return false;
	}
	
	if((trim(day_Code.value).length== 0) && (trim(day_Number.value).length== 0)&& (trim(mobile.value).length== 0) ){
		alert("Telephone Number or Mobile Number is Mandatory");
		mobile.focus();
		mobile.select();
		return false;	
	}
	if((trim(day_Code.value).length > 0) || (trim(day_Number.value).length > 0)){
		if(isInteger(trim(day_Code.value)) == false){
			alert("Enter valid Telephone Area Code");
			day_Code.focus();
			day_Code.select();
			return false;
		}
		if(trim(day_Code.value).length > 5){
			alert("Enter Telephone Area Code within 5 digits");
			day_Code.focus();
			day_Code.select();
			return false;
		}
		if(isInteger(trim(day_Number.value)) == false){
			alert("Enter valid Telephone Number");
			day_Number.focus();
			day_Number.select();
			return false;
		}
		if(trim(day_Number.value).length > 10 ){
			alert("Enter Telephone Number within 10 digits");
			day_Number.focus();
			day_Number.select();
			return false;
		}
	}
	
	if((trim(mobile.value).length > 0)){
		if(isInteger(trim(mobile.value)) == false){
			alert("Enter valid Mobile Number");
			mobile.focus();
			mobile.select();
			return false;
		}
		if(trim(mobile.value).length < 10){
			alert("Your Mobile Number must contain 10 digits");
			mobile.focus();
			mobile.select();
			return false;
		}
		if(trim(mobile.value).length > 10){
			alert("Enter Mobile Number in 10 digits");
			mobile.focus();
			mobile.select();
			return false;
		}
	}

	/* This method is to check State field that any one should be selected.	 */
 	/*	if((stateCode.value=="selected")){
		alert('Select your State Name');
		stateCode.focus();
		return false;
	}

	if((stateCode.value=="50") && isAlphabetic(otherState.value)==false){
		alert('Enter valid other state');
		otherState.value="";
		otherState.focus();
		otherState.select();
		return false;
	}
	
	if(otherState.value.length > 0 ){
		if(otherState.value.length > 25){
			alert('Enter other state within 25 characters');
			otherState.value="";
			otherState.focus();
			otherState.select();
			return false;
		}
	}*/
if(document.getElementById("campaignCookieVal").value.length==0){
	if(referred.value=='selected'){
		alert("Select Source");
		referred.focus();
		return false;
	}

	if(referredBy=="Newspaper" || referredBy=="Magazine" || referredBy=="TV" || referredBy=="Radio"){
		if(referedEntered==""){
			alert("Enter "+referredBy+" Name");
			return false;
		}
	}else if(referredBy=="Friend"){
			
		if(referedEntered==""){
			alert("Enter Friend's Email Id");
			return false;
		}
		
		if(isEmail(trim(friendsEmail.value))==false){
			alert('Enter a Valid Email ID');
			friendsEmail.focus();
			friendsEmail.select();
			return false;
		}

	}else if(referredBy=="InternetKiosk"){
		if(referedEntered==""){
			alert("Enter Email Id");
			return false;
		}
		if(isEmail(trim(friendsEmail.value))==false){
			alert('Enter a Valid Email ID');
			friendsEmail.focus();
			friendsEmail.select();
			return false;
		}

	}else if(referredBy=="Others"){
		if(referedEntered==""){
			alert("Enter Others field");
			return false;
		}
	}
}

	/* This method is to check Word Verification field, should not be blank and 
	 * should contain data as seen in image. */
	/* if(!isEmpty(imgVerify.value)==false){
		alert("Enter the image verification text as you see above ");
		imgVerify.focus();
		imgVerify.select();
		return false;
	} */
	
    /* This method is to verify Terms & Conditions field, should be checked as 
	 * acceptance. */
	if(conditions==false){
		alert("You must agree to the Terms and Conditions \n to submit your details");
		return false;
	}
	
	document.getElementById("submitbutton").disabled=true;
}


//******Regular expression starts here****//
//VALIDATION FOR CHECK NAMES
var reAlphabetic = /^[a-zA-Z]+$/
var nameExpression = /^[a-zA-Z''. ]+$/
var defaultEmptyOK = false

function isEmpty(s){ 
	return ((s == null) || (s.length == 0))
}

function isAlphabetic(s){  
	var i;
    if (isEmpty(s)) 
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);
    else {
       return reAlphabetic.test(s)
    }
}

function isValidName(s){ 
	var i;
    if (isEmpty(s)) 
       if (isValidName.arguments.length == 1) return defaultEmptyOK;
       else return (isValidName.arguments[1] == true);
    else {
       return nameExpression.test(s)
    }
}



//VALIDATION FOR LOGIN ID
var alphaNum= /^[a-zA-Z][a-zA-Z0-9]*$/
var defaultEmptyOK = false

function isEmpty(s){ 
	return ((s == null) || (s.length == 0))
}

function isLogin(s){  
	var i;
    if (isEmpty(s)) 
       if (isLogin.arguments.length == 1) return defaultEmptyOK;
       else return (isLogin.arguments[1] == true);
    else {
       alphaNum.test(s)
		return alphaNum.test(s) 
    }
}

//VALIDATION FOR NUMBER
var reInteger = /^\d+$/
function isInteger(s){
	var i;
    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    return reInteger.test(s)
}

//VALIDATION FOR CHECK DATE NOT LESS THAN CURRECT DATE
function isLessThenCurrentDate(dateStr){
	var mydate=new Date(commonServerDate.getFullYear(), commonServerDate.getMonth(), commonServerDate.getDate());
	var a1=new Array();
	a1=dateStr.split("/");
	var userdate=new Date(a1[2],a1[1]-1,a1[0]);
	var one_day=1000*60*60*24
	var noOfDays=Math.ceil((mydate.getTime()-userdate.getTime())/(one_day));

	if(noOfDays<=0){ 
		return false;
	}
	return true;
}

/** DHTML date validation script for dd/mm/yyyy. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/) */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isIntegers(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;
}

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 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 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 testDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=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("Enter your Date of Birth (dd/mm/yyyy)")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isIntegers(stripCharsInBag(dtStr, dtCh))==false){
		alert("Enter a valid date")
		return false
	}
	return true
}


//VALIDATION FOR ALPHANUMERIC
function isAlphaNumeric(val){
	if (val.match(/^[a-zA-Z0-9]+$/)){
		return true;
	}else{
		return false;
	} 
}

//Trim function 
String.prototype.trim = function() {
// Strip leading and trailing white-space
return this.replace(/^\s*|\s*$/g, "");
}
