﻿// File for Handling Validation of Registration Page
//Writtem By Aman Bhullar[ShubhRishtey.com]

var strRowBackColor = '#FFCEB7';
var strWhiteColor = '#FFFFFF';
var one_day=1000*60*60*24;
var one_month=1000*60*60*24*30;
var one_year=1000*60*60*24*30*12;
	
function ValidatePage()
{    
	var strError = "";
	var blnResult = true;
	document.getElementById('rwError').style.backgroundColor = strWhiteColor;
	document.getElementById('lblMessages').innerHTML = "";
	
	if(isEmpty(document.getElementById('txtName')))
    {
        document.getElementById('rwName').style.backgroundColor = strRowBackColor;
        strError = strError + "<li>Please type a Name</li>";
    }
    else
    {
        document.getElementById('rwName').style.backgroundColor = strWhiteColor;
    }
	
	if(document.getElementById('cmbDOBDay').value  == 'D' || document.getElementById('cmbDOBMonth').value == 'M' || document.getElementById('cmbDOBYear').value == 'Y')
	{
	    document.getElementById('rwDOB').style.backgroundColor = strRowBackColor;
	    strError = strError + "<li>Please select Date of Birth</li>";
	}
	else
	{
	    document.getElementById('rwDOB').style.backgroundColor = strWhiteColor;
	}
	
    if(isRadioEmpty('Gender'))
    {
        document.getElementById('rwGender').style.backgroundColor = strRowBackColor;
        strError = strError + "<li>Please select a gender</li>";
    }
    else
    {
        document.getElementById('rwGender').style.backgroundColor = strWhiteColor;
    }
	
	if(isRadioEmpty('MaryStatus'))
	{
	    document.getElementById('rwMarryS').style.backgroundColor = strRowBackColor;
	    strError = strError + "<li>Please select the Marital Status</li>";
	}
	else
	{
	    document.getElementById('rwMarryS').style.backgroundColor = strWhiteColor;
	}
	
	if(isDropDownEmpty('cmbReligion','0'))
	{
	    document.getElementById('rwReligion').style.backgroundColor = strRowBackColor;
	    strError = strError + "<li>Please select the Religion</li>";
	}
	else
	{
	    document.getElementById('rwReligion').style.backgroundColor = strWhiteColor;
	}
	
	if(isDropDownEmpty('cmbHeight','0'))
    {
        document.getElementById('rwHeight').style.backgroundColor = strRowBackColor;
        strError = strError + "<li>Please select the Height</li>"; 
    }
    else
    {
        document.getElementById('rwHeight').style.backgroundColor = strWhiteColor;
    }
	
	if(isDropDownEmpty('cmbCountryResidence','0'))
	{
	    document.getElementById('rwRStatus').style.backgroundColor = strRowBackColor;
	    strError = strError + "<li>Please select the Residing Status</li>";
	}
	else
	{
	    document.getElementById('rwRStatus').style.backgroundColor = strWhiteColor;
	}
	
	if(isEmpty(document.getElementById('txtEMail')))
	{
	    document.getElementById('rwEMail').style.backgroundColor = strRowBackColor;
	    strError = strError + "<li>Please type an Email Address</li>";
	}
	else
	{
	    if(!ValidateEmail(document.getElementById('txtEMail').value))
	    {
	        document.getElementById('rwEMail').style.backgroundColor = strRowBackColor;
	        strError = strError + "<li>Please type an valid Email Address</li>";
	    }
	    else
	    {
	        var blnInnerResult = true;
	        for ( var Idx = 0; Idx < document.getElementById('txtEMail').value; Idx++ )
	        {
		        if ( document.getElementById('txtEMail').value.charAt(Idx) == '	'
			        || document.getElementById('txtEMail').value.charAt(Idx) == ' '
			        || document.getElementById('txtEMail').value.charAt(Idx) == ','
			        || document.getElementById('txtEMail').value.charAt(Idx) == '/'
			        || document.getElementById('txtEMail').value.charAt(Idx) == '\\'
			        || document.getElementById('txtEMail').value.charAt(Idx) == ';' )
	                {
		                //alert( "Blanks or other invalid characters are not allowed in the E-mail ID.\nPlease enter only one E-mail ID." );
		                blnInnerResult = false;
	                }
	        }
	        if(blnInnerResult == false)
	        {
	            document.getElementById('rwEMail').style.backgroundColor = strRowBackColor;
	            strError = strError + "<li>Blanks or other invalid characters are not allowed in the E-mail ID.</li>";
	        }
	        else
	        {
	            document.getElementById('rwEMail').style.backgroundColor = strWhiteColor;
	        }
	    }
	}
	
	if(isEmpty(document.getElementById('txtPassword')))
	{
	    document.getElementById('rwPass').style.backgroundColor = strRowBackColor;
	    strError = strError + "<li>Please type a Password</li>";
	}
	else
	{
	    if(document.getElementById('txtPassword').value.length < 4)
	    {
	        document.getElementById('rwPass').style.backgroundColor = strRowBackColor;
	        strError = strError + "<li>Password must contain minimum 4 Characters</li>";
	    }
	    else
	    {
	        document.getElementById('rwPass').style.backgroundColor = strWhiteColor;
	    }
	}
	
	if(isEmpty(document.getElementById('txtCPassword')))
	{
	    document.getElementById('rwCPass').style.backgroundColor = strRowBackColor;
	    strError = strError + "<li>Please Confirm Password</li>";
	}
	else
	{
	    if(document.getElementById('txtPassword').value != document.getElementById('txtCPassword').value)
	    {
	        document.getElementById('rwPass').style.backgroundColor = strRowBackColor;
	        document.getElementById('rwCPass').style.backgroundColor = strRowBackColor;
	        strError = strError + "<li>Both the Password do not match. Please Re-Enter Passwords</li>";
	        document.getElementById('txtPassword').value = "";
	        document.getElementById('txtCPassword').value = "";
	    }
	    else
	    {
	        document.getElementById('rwPass').style.backgroundColor = strWhiteColor;
	        document.getElementById('rwCPass').style.backgroundColor = strWhiteColor;
	    }
	}
	
	if(isDropDownEmpty('cmbEduCategory',0))
	{
	    document.getElementById('rwEduCat').style.backgroundColor = strRowBackColor;
	    strError = strError + "<li>Please select the Educational Category</li>";
	}
	else
	{
	    document.getElementById('rwEduCat').style.backgroundColor = strWhiteColor;
	}
	
	if(isDropDownEmpty('cmbEmployedIn',0))
	{
	    document.getElementById('rwEmpIn').style.backgroundColor = strRowBackColor;
	    strError = strError + "<li>Please select the Employment Status</li>";
	}
	else
	{
	    document.getElementById('rwEmpIn').style.backgroundColor = strWhiteColor;
	}
	
	
	var strDes = new String(document.frmRegistration.elements["Description"].value);
	strDes = strDes.replace(/\s+$/,"");
	//alert(strDes.length);
	if(strDes.length == 0)
    {
        document.getElementById('rwMyself').style.backgroundColor = strRowBackColor;
        strError = strError + "<li>Please type About Yourself.</li>"; 
    }
	else
	{
		if(strDes.length < 100)
        {
            document.getElementById('rwMyself').style.backgroundColor = strRowBackColor;
            strError = strError + "<li>Please type Minimum 100 Characters</li>"; 
        } 
        else
        {
            document.getElementById('rwMyself').style.backgroundColor = strWhiteColor;
        }
	 }
	
	if(isDropDownEmpty('cmbPostedBy',0))
	{
	    document.getElementById('rwPostBy').style.backgroundColor = strRowBackColor;
	    strError = strError + "<li>Please Select Profile Posted By</li>";
	}
	else
	{
	    document.getElementById('rwPostBy').style.backgroundColor = strWhiteColor;
	}
	
	if(isRadioEmpty('chkAgree'))
	{
	    document.getElementById('rwAgree').style.backgroundColor = strRowBackColor;
	    strError = strError + "<li>Please read and confirm the acceptance to the Terms and Conditions</li>";
	}
	else
	{
	    document.getElementById('rwAgree').style.backgroundColor = strWhiteColor;		    
	}
	
	if(strError == "")
	{
	    var calyear = displayage(document.getElementById('cmbDOBYear').value,document.getElementById('cmbDOBMonth').value,document.getElementById('cmbDOBDay').value, 'years', 0, 'rounddown')
	    var intAge = parseInt(calyear);
	    if(getCheckedVal('Gender') == "0")
	    {
	        if(intAge < 21)
	        {
	            document.getElementById('rwDOB').style.backgroundColor = strRowBackColor;
	            strError = strError + "<li>You should be 21 Years old to Register</li>";
	        }
	        else
	        {
	            document.getElementById('rwDOB').style.backgroundColor = strWhiteColor;
	        }
	    }
	    else
	    {
	        if(intAge < 18)
	        {
	            document.getElementById('rwDOB').style.backgroundColor = strRowBackColor;
	            strError = strError + "<li>You should be 18 Years old to Register</li>";
	        }
	        else
	        {
	            document.getElementById('rwDOB').style.backgroundColor = strWhiteColor;
	        }
	    }
	}
	
	if(strError != "")
	{
	    blnResult = false;
	    document.getElementById('rwError').style.backgroundColor = strRowBackColor;
	    strError = "<table border='0' cellpadding='0' cellspacing='0'><tr><td height='25px' class='Panelmedblack'>&nbsp;&nbsp;&nbsp;Please correct the following errors. Fields with errors are highlighted</td></tr>" + 
	                "<tr><td><ul type='disc' class='medblack'>" + strError + "</ul></td></tr></table>";
	    document.getElementById('lblMessages').innerHTML = strError;
	    document.getElementById('txtName').focus();
	    window.scrollTo(0,0);
	}
	
    return blnResult;
}//END OF FUNCTION ValidatePage
    
function displayage(yr, mon, day, unit, decimal, round){
today=new Date()
var pastdate=new Date(yr, mon-1, day)

var countunit=unit
var decimals=decimal
var rounding=round

finalunit=(countunit=="days")? one_day : (countunit=="months")? one_month : one_year
decimals=(decimals<=0)? 1 : decimals*10

if (unit!="years"){
if (rounding=="rounddown")
//document.write(Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals+" "+countunit)
alert (Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals+' '+countunit)
else
//document.write(Math.ceil((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals+" "+countunit)
alert (Math.ceil((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals+' '+countunit)
}
else
{
yearspast=today.getFullYear()-yr-1
tail=(today.getMonth()>mon-1 || today.getMonth()==mon-1 && today.getDate()>=day)? 1 : 0
pastdate.setFullYear(today.getFullYear())
pastdate2=new Date(today.getFullYear()-1, mon-1, day)
tail=(tail==1)? tail+Math.floor((today.getTime()-pastdate.getTime())/(finalunit)*decimals)/decimals : Math.floor((today.getTime()-pastdate2.getTime())/(finalunit)*decimals)/decimals
//document.write(yearspast+tail+" "+countunit)
//alert (yearspast+tail+' '+countunit)
var calyear=yearspast+tail;
}
return calyear;
}//End of Function displayage
	
//This function Return the Checked value from List of Radio.Check Boxes
function getCheckedVal(strObjName)
{
    var strVal;
    for(var iCount=0;iCount<document.getElementsByName(strObjName).length;iCount++)
    {
        if(document.getElementsByName(strObjName).item(iCount).checked != null)
        {
            if(document.getElementsByName(strObjName).item(iCount).checked) strVal = document.getElementsByName(strObjName).item(iCount).value;
        }
    }//End For
    return strVal;
}//END OF FUNCTION
	

//Function For Validation of Email Address	
function ValidateEmail( Email )
{
	var atCharPresent = false;
	var dotPresent = false;

	for ( var Idx = 0; Idx < Email.length; Idx++ )
	{
		if ( Email.charAt ( Idx ) == '@' )
			atCharPresent = true;
		if ( Email.charAt ( Idx ) == '.' )
			dotPresent = true;
	}

	if ( !atCharPresent || !dotPresent )
		return false;

	return true;
}
    
function isDropDownEmpty(strObjName, strEmptyVal)
{
    var blnResult = true;
    if(document.getElementById(strObjName).value == strEmptyVal)
    {
        blnResult = true;
    }
    else
    {
        blnResult = false;
    }
    return blnResult;
}//End of Function isDropDownEmpty
    
function isRadioEmpty(strObjName)
{
    var blnResult = true;
    for(var iCount=0;iCount<document.getElementsByName(strObjName).length;iCount++)
    {
        if(document.getElementsByName(strObjName).item(iCount).checked != null)
        {
            if(document.getElementsByName(strObjName).item(iCount).checked) blnResult = false;
        }
    }//End For
    return blnResult;
}//END OF FUNCTION
    
function isEmpty(obj)
{
    var blnResult;
    var strString = new String(document.getElementById(obj.id).value);
    strString = strString.replace(/\s+$/,"");
    document.getElementById(obj.id).value = strString;
    if(strString.length == 0)
    {
        blnResult = true;
    }
    else
    {
        blnResult = false;
    }
	return blnResult;
}//END OF FUNCTION isEmpty

//VALIDATION CODE FOR THE BASIC DETAILS FORM

