/*	________________________________________________________________________________________________________
_|
_|
|
| CREATE BY : Umang Savaliya
| CREATE DATE: 7 Sep, 02:00 AM
|
| -----------------------------------------------------------------------------------------------------------
| LAST MODIFIED DATE:  			|| MODIFIED REASION:
| (1) 2008-11-17				|| (1) Milin M.
| -----------------------------------------------------------------------------------------------------------
|
| DESCRIPTION:
| 		(1) Javascript file, function for validation.
|
|			Copyright (c)  2006-2007-2008 by pinstorm. All Rights Reserved.
|________________________________________________________________________________________________________
*/


/*
*
*	@GLOBAL variable(containg reg. exp.) for email validation
*
**/

var regMail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/


/*-------------------------------------------------------------------------------------------
Validation FUNCTION
--------------------------------------------------------------------------------------------*/

/*
*	#Function PARAMETER
*
*	@fome - form object Ex. document.fomeName;
*
**/

function Validation(fome)
{
    var txtName = TrimString($('txtName').value);
    var txtEmail = TrimString($('txtEmail').value);
//    var txtCountry = TrimString($('cobCountry').value);
//    var txtCity = TrimString($('txtCity').value);
    //var txtCountrycode = TrimString($('txtCountrycode').value);
    var txtAreacode = TrimString($('txtAreacode').value);
    var txtPhone = TrimString($('txtPhone').value);
    var txtMobile = TrimString($('txtMobile').value);
//    var cobProduct = TrimString($('cobProduct').value);
    var txtLoan = TrimString($('txtLoan').value);
    var txtSalary = TrimString($('txtSalary').value);
    //var slctPrefTime = TrimString($('slctPrefTime').value);
    //	var cobCurrency = TrimString($('cobCurrency').value);
    //	var cobCurrency2 = TrimString($('cobCurrency2').value);

    var errMsg = '';
    var foc = 1;

    if(txtName == '')
    {
        errMsg += 'Please enter your Name.\n';
        if(foc==1)
        {
            $('txtName').focus();
            foc++;
        }
    }
    else if(containsspecial(txtName)==true)
    {
        errMsg +="No special characters please in Name field.\n";
        if(foc==1)
        {
            $('txtName').focus();
            foc++;
        }
    }
    else if(containsdigit(txtName)==true)
    {
        errMsg +="No digits please in Name field.\n";
        if(foc==1)
        {
            $('txtName').select();
            foc++;
        }
    }

    if(txtEmail == '')
    {
        errMsg += 'Please enter your E-mail.\n';
        if(foc==1)
        {
            $('txtEmail').focus();
            foc++;
        }
    }
    else if(regMail.test(txtEmail)==false)
    {
        errMsg +="Please fill in a valid E-mail ID.\n";
        if(foc==1)
        {
            $('txtEmail').focus();
            foc++;
        }
    }
/*
    if(txtCountry == '')
    {
        errMsg += 'Please enter your country.\n';
        if(foc==1)
        {
            $('cobCountry').focus();
            foc++;
        }
    }

    if(txtCity == '')
    {
        errMsg += 'Please enter your city.\n';
        if(foc==1)
        {
            $('txtCity').focus();
            foc++;
        }
    }
*/
    /*if(txtAreacode == '' && txtPhone == '' && txtMobile == '')
    {
        errMsg += 'Please enter LandLine or Mobile No.\n';
        if(foc==1)
        {
            $('txtAreacode').focus();
            foc++;
        }
    }*/

    if(txtAreacode != '' && txtPhone == '')
    {
        errMsg += 'Please enter all values in LandLine .\n';
        if(foc==1)
        {
            $('txtAreacode').focus();
            foc++;
        }
    }

    if(txtAreacode == '' && txtPhone != '')
    {
        errMsg += 'Please enter all values in LandLine .\n';
        if(foc==1)
        {
            $('txtAreacode').focus();
            foc++;
        }
    }

    if(txtPhone != '' && txtPhone.length<7)
    {
        errMsg += 'Landline Number should contain atleast 7 digits.\n';
        if(foc==1)
        {
            $('txtPhone').focus();
            foc++;
        }
    }

    if('' == txtMobile) {
        errMsg += 'Please enter your Mobile No.\n';
        if(foc==1)
        {
            $('txtMobile').focus();
            foc++;
        }
    }
    else if (txtMobile != '' && txtMobile.length>0) { //added on 4-dec-2008, check for first digit should be 0
        var sFirstDigit = txtMobile.substr(0,2);//first 2 digits starting with '07' 29oct09

        if ('07' != sFirstDigit) {
            errMsg += 'Mobile Number should start from 07.\n';
            if (foc==1)
            {
                $('txtMobile').focus();
                foc++;
            }
        }
    }
    else if (txtMobile != '' && txtMobile.length<11) { //added on 4-dec-2008, check for 11 digits number
        errMsg += 'Mobile Number should be 11 digits.\n';
        if (foc==1)
        {
            $('txtMobile').focus();
            foc++;
        }
    }

    /*		if(txtAreacode != '' && txtPhone == '')
    {
    errMsg += 'Please enter all values in LandLine .\n';
    if(foc==1)
    {
    $('txtPhone').focus();
    foc++;
    }
    }


    if(txtAreacode == '' && txtPhone != '')
    {
    errMsg += 'Please enter all values in LandLine .\n';
    if(foc==1)
    {
    $('txtAreacode').focus();
    foc++;
    }
    }

    */

    if(txtAreacode != '' || txtPhone != '' || txtMobile != '')
    {

        if(containsalph(txtAreacode)==true)
        {
            errMsg +="Please use only numerics in the Area code field.\n";
            if(foc==1)
            {
                $('txtAreacode').focus();
                foc++;
            }
        }

        if(containsalph(txtPhone)==true)
        {
            errMsg +="Please use only numerics in the Phone field.\n";
            if(foc==1)
            {
                $('txtPhone').focus();
                foc++;
            }
        }

        if(containsalph(txtMobile)==true)
        {
            errMsg +="Please use only numerics in the Mobile field.\n";
            if(foc==1)
            {
                $('txtMobile').focus();
                foc++;
            }
        }

    }

/*
    if(cobProduct == '')
    {
        errMsg += 'Please select product.\n';
        if(foc==1)
        {
            $('cobProduct').focus();
            foc++;
        }
    }
*/
    if(txtLoan == '')
    {
        errMsg += 'Please enter Loan Amount.\n';
        if(foc==1)
        {
            $('txtLoan').focus();
            foc++;
        }
    }
    else if(containsalph(txtLoan)==true)
    {
        errMsg +="Please use only numerics in the Amount field.\n";
        if(foc==1)
        {
            $('txtLoan').focus();
            foc++;
        }
    }
    else if(containsspecial(txtLoan)==true)
    {
        errMsg +="No special characters please in Amount field.\n";
        if(foc==1)
        {
            $('txtLoan').focus();
            foc++;
        }
    }

/*
    if(txtSalary=='0')
    {
        errMsg +="Please no 0 value in the Salary field.\n";
        if(foc==1)
        {
            $('txtSalary').focus();
            foc++;
        }
    }
    else if(containsalph(txtSalary)==true)
    {
        errMsg +="Please use only numerics in the Salary field.\n";
        if(foc==1)
        {
            $('txtSalary').focus();
            foc++;
        }
    }
    else if(containsspecial(txtSalary)==true)
    {
        errMsg +="No special characters please in Salary field.\n";
        if(foc==1)
        {
            $('txtSalary').focus();
            foc++;
        }
    }

    if(cobCurrency2 == '')
    {
    if(txtSalary != '')
    {
    errMsg += 'Please enter your currency for Salary.\n';
    if(foc==1)
    {
    $('cobCurrency2').focus();
    foc++;
    }
    }
    }*/


    if(errMsg != '')
    {
        alert(errMsg);
        return false;
    }
    else
    {
        document.frmDhfl.submit();
        return true;
    }

}


/*-------------------------------------------------------------------------------------------
End Validation FUNCTION
--------------------------------------------------------------------------------------------*/


/*-------------------------------------------------------------------------------------------
tabValidation FUNCTION
--------------------------------------------------------------------------------------------*/

/*
*	#Function PARAMETER
*
*	@val - value of the field
*	@mDiv - Main Field Div
*	@eDiv - field error Div
*	$i - field tracking
**/

function tabValidation(val,eDiv,i)
{

    //	var mainDiv = $(mDiv);
    var errDiv = $(eDiv);
    var errMsg = '';
    //	mainDiv.className = 'error';

    if(i==1)	// Name
    {
        if(val == '')
        {
            errMsg = 'Please enter your name.';
        }
        else if(containsspecial(val)==true)
        {
            errMsg ="No special characters please in Name field.\n";
        }
        else if(containsdigit(val)==true)
        {
            errMsg ="No digits please in Name field.\n";
        }
    }
    else if(i==2)	// E-mail validation.
    {
        if(val == '')
        {
            errMsg = 'Please enter your email.';
        }
        else if(regMail.test(val)==false)
        {
            errMsg ='Please fill in a valid email ID.';
        }
    }
    /*else if(i==3)	// Country
    {
    if(val == '')
    {
    errMsg = 'Please enter your country.';
    }
    }
    else if(i==4)	// City validation
    {
        if(val == '')
        {
            errMsg = 'Please enter your city.';
        }
    }*/
    else if(i==5)	// Telephone validation for country code
    {
        if(val == '')
        {
            errMsg = 'Please enter Country code.';
        }
        else if(containsalph(val)==true)
        {
            errMsg ='Please use only numerics in the Country code field.';
        }

    }
    else if(i==6)	// Telephone validation for Area code
    {

        if(containsalph(val)==true)
        {
            errMsg ='Please use only numerics in the Area code field.';
        }

    }
    else if(i==7)	// Telephone validation for Phone
    {
        if(containsalph(val)==true)
        {
            errMsg ='Please use only numerics in the phone field.';
        }

    }
    else if(i==8)	// Mobile validation
    {
        //var param = val;
        var tel_len = val.length;

        if('' == val) {
            errMsg = 'Please enter your Mobile No.';
        }
        else if(containsalph(val)==true)
        {
            errMsg ='Please use only numerics in the Mobile field.';
        }
        else if (val != '' && tel_len>0) { //added on 29-oct-2009, check for first digit should be 0
            var sFirstDigit = val.substr(0,2);//first 2 digits starting with '07' 29oct09

            if ('07' != sFirstDigit) {
                errMsg = 'Mobile Number should start from 07.';
            }
            else if (val != '' && tel_len<11) { //added on 4-dec-2008, check for 11 digits number
                errMsg = 'Mobile Number should be 11 digits.';
            }
        }


    }
    /*else if(i==9)	// Product validation
    {
        if(val == '')
        {
            errMsg = 'Please enter Product.';
        }
    }*/
    else if(i==10)	// Loan validation
    {
        if(val == '')
        {
            errMsg = 'Please enter Loan Amount.';
        }
        else if(containsalph(val)==true)
        {
            errMsg ='Please use only numeric value in the Amount field.';
        }
    }
    /*else if(i==11)	// Salary validation
    {
        if(val == '0')
        {
            errMsg = 'Please enter numeric value in the Salary field .';
        }
        else if(containsalph(val)==true)
        {
            errMsg ='Please enter numeric value in the Salary field.';
        }
    }*/
    else if(i==12)	// Loan currency validation
    {
        if(val == '')
        {
            errMsg = 'Please enter currency for loan.';
        }

    }
    /*	else if(i==13)	// Salary currency validation
    {
    if($('txtSalary').value != '')
    {
    if(val == '')
    {
    errMsg = 'Please enter currency for salary.';
    }
    }
    }*/

    if(errMsg == '')
    {
        //mainDiv.className = 'fc1';
        errDiv.style.display = 'none';
    }
    else
    {

        //mainDiv.className = 'fcNew';
        errDiv.style.display = 'block';
        errDiv.innerHTML = errMsg;
    }
}

/*-------------------------------------------------------------------------------------------
End tabValidation FUNCTION
--------------------------------------------------------------------------------------------*/


function TrimString(str)	// Remove the space form string
{
    var st=new String(str)
    stpos=0;
    strln=st.length;
    while(stpos<strln)
    {
        if(st.substr(stpos,1)!=' ')
        {
            break;
        }
        stpos=stpos+1;
    }

    edpos=strln-1;
    while(edpos>=stpos)
    {
        if(st.substr(edpos,1)!=' ')
        {
            break;
        }
        edpos=edpos-1;
    }
    var retst=st.substring(stpos,edpos+1);
    return retst;
}

function containsalph(param)
{
    mystrLen = param.length;
    for(i=0;i<mystrLen;i++)
    {
        if((param.charAt(i)<"0")||(param.charAt(i)>"9"))
        {
            return true;
        }
    }
    return false;
}


function containsspecial(param)
{
    mystrLen = param.length;

    for(i=0;i<mystrLen;i++)
    {
        if((param.charAt(i)=="~") || (param.charAt(i)=="!") || (param.charAt(i)=="@") || (param.charAt(i)=="#") || (param.charAt(i)=="$") || (param.charAt(i)=="%") || (param.charAt(i)=="^") || (param.charAt(i)=="&") || (param.charAt(i)=="*") || (param.charAt(i)=="(") || (param.charAt(i)==")") || (param.charAt(i)=="?") || (param.charAt(i)==">") || (param.charAt(i)=="<") || (param.charAt(i)==",") || (param.charAt(i)==".") || (param.charAt(i)==":") || (param.charAt(i)==";") || (param.charAt(i)=="\"") || (param.charAt(i)=="'") || (param.charAt(i)=="\\") || (param.charAt(i)=="|") || (param.charAt(i)=="{") || (param.charAt(i)=="}") || (param.charAt(i)=="[") || (param.charAt(i)=="]") || (param.charAt(i)=="+") || (param.charAt(i)=="=") || (param.charAt(i)=="-") || (param.charAt(i)=="_"))
        {
            return true;
        }
    }
    return false;
}


function containsdigit(param)
{
    mystrLen = param.length;
    for(i=0;i<mystrLen;i++)
    {
        if((param.charAt(i)=="0") || (param.charAt(i)=="1") || (param.charAt(i)=="2") || (param.charAt(i)=="3") || (param.charAt(i)=="4") || (param.charAt(i)=="5") || (param.charAt(i)=="6") || (param.charAt(i)=="7") || (param.charAt(i)=="8") || (param.charAt(i)=="9") || (param.charAt(i)=="/"))
        {
            return true;
        }
    }
    return false;
}


/*
*	#Function PARAMETER
*
*	@str - field id name
*
*
**/

function $(str)
{
    return document.getElementById(str); // return object of field
}


function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1){ return btn[cnt].value;}else{ return null; } //return btn[cnt].value;
}

