﻿function ValidateEmail()
{
    var valid = true;
    var email = document.getElementById("ctl00_ContentPlaceHolder1_txtEmail");
    email.value = email.value.trim();
    var emailDiv = document.getElementById("emailDiv");
    emailDiv.style.display = "none";
    if(email.value == '')
    {
        emailDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">Email Address is required.</span>";
        emailDiv.style.display = "block";
        email.focus();
        valid = false;                
    }
    else
    { 
        if(checkEmailSyntax(email.value) == false)
        {
            emailDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">Invalid Email address.</span>";
            emailDiv.style.display = "block";
            email.focus();
            valid = false; 
        }
    }
    return valid;
}

function ValidateLoginEmail() {
    var valid = true;
    var email = document.getElementById("ctl00_ContentPlaceHolder1_loginControl_UserName");
    email.value = email.value.trim();
    var emailDiv = document.getElementById("emailDiv");
    emailDiv.style.display = "none";
    if (email.value == '') {
        emailDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">Email Address is required.</span>";
        emailDiv.style.display = "block";
        email.focus();
        valid = false;
    }
    else {
        if (checkEmailSyntax(email.value) == false) {
            emailDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">Invalid Email address.</span>";
            emailDiv.style.display = "block";
            email.focus();
            valid = false;
        }
    }
    return valid;
}

function ValidatePassword()
{
    var valid = true;
    var password = document.getElementById("ctl00_ContentPlaceHolder1_txtPassword");
    var passwordDiv = document.getElementById("passwordDiv");      
    passwordDiv.style.display = "none";    
    if(password.value == '')
    {
        passwordDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">Password is required.</span>";
        passwordDiv.style.display = "block";
        password.focus();
        valid = false;
    }      
    else
    {    
        if(password.value.length < 6)
        {
            passwordDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">Password must have atleast 6 characters.</span>";
            passwordDiv.style.display = "block";
            password.focus();
            valid = false;
        }        
    }
    return valid;
}
function ValidateConfirmPassword()
{
    var valid = true;
    var password = document.getElementById("ctl00_ContentPlaceHolder1_txtPassword");
    var confirmPassword = document.getElementById("ctl00_ContentPlaceHolder1_txtConfirmPassword");
    var confirmPasswordDiv = document.getElementById("confirmPasswordDiv"); 
    confirmPasswordDiv.style.display = "none";
    if(confirmPassword.value == '')
    { 
        confirmPasswordDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">Confirm Password is required.</span>";
        confirmPasswordDiv.style.display = "block";
        confirmPassword.focus();
        valid = false;
    }
    else
    {                
        if(confirmPassword.value != password.value)
        {
            confirmPasswordDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">Confirm Password does not match.</span>";
            confirmPasswordDiv.style.display = "block";
            confirmPassword.focus();
            valid = false;
        }
    }
    return valid;
}
function ValidateSecurityQuestion()
{
    var valid = true;
    var securityQue = document.getElementById("ctl00_ContentPlaceHolder1_ddlSecurityQuestion");
    var securityQuestionDiv = document.getElementById("securityQuestionDiv");
    securityQuestionDiv.style.display = 'none';    
    if(securityQue.value == 'Security Question')                   
    {             
       securityQuestionDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">Security Question is required</span>";
       securityQuestionDiv.style.display = 'block';
       securityQue.focus();
       valid = false;
    }
    return valid;
}
function ValidateSecurityAnswer()
{
    var valid = true;
    var securityAns = document.getElementById("ctl00_ContentPlaceHolder1_txtSecurityAnswer");
    var securityAnswerDiv = document.getElementById("securityAnswerDiv");
    securityAnswerDiv.style.display = 'none';
    securityAns.value = securityAns.value.trim();
    if(securityAns.value == '')
    {             
        securityAnswerDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">Security Answer is required.</span>";
        securityAnswerDiv.style.display = 'block';
        securityAns.focus();
        valid = false; 
    }
    return valid;
}

function ValidateDisplayName()
{
    var valid = true;
    var txtdisplayname = document.getElementById("ctl00_ContentPlaceHolder1_txtdisplayname");
    var displayNameDiv = document.getElementById("ctl00_ContentPlaceHolder1_displayNameDiv");
    displayNameDiv.style.display = "none"; 
    txtdisplayname.value = txtdisplayname.value.trim();
    if(txtdisplayname.value.trim() == '')
    {            
        displayNameDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">User name is required.</span>";
        displayNameDiv.style.display = "block";
        txtdisplayname.focus();
        valid = false; 
    }
    else
    {
        if(txtdisplayname.value.length < 6)
        {
            displayNameDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\"> User name must have at least 6 characters.</span>";
            displayNameDiv.style.display = "block";
            displayNameDiv.focus();
            valid = false; 
        }
        else
        {
            if(txtdisplayname.value.indexOf(' ') > 0)
            {            
                displayNameDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">Spaces are not allowed in User name.</span>";
                displayNameDiv.style.display = "block";
                txtdisplayname.focus();
                valid = false; 
            }
            else
            {
                remeoveSpecialCharaters('ctl00_ContentPlaceHolder1_txtdisplayname');
            }
        }
    }
    return valid;
}



function ValidateFirstName()
{
    var valid = true;
    var txtFirstName = document.getElementById("ctl00_ContentPlaceHolder1_txtFirstName");
    var firstNameDiv = document.getElementById("firstNameDiv");
    txtFirstName.value = txtFirstName.value.trim();
    firstNameDiv.style.display = "none"; 
    if(txtFirstName.value == '')
    {            
        firstNameDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">First name is required.</span>";
        firstNameDiv.style.display = "block";
        txtFirstName.focus();        
        valid = false;
    }
    return valid;
}

function ValidateLastName()
{
    var valid = true;
    var txtLastName = document.getElementById("ctl00_ContentPlaceHolder1_txtLastName");
    var lastNameDiv = document.getElementById("lastNameDiv");
    txtLastName.value = txtLastName.value.trim();
    lastNameDiv.style.display = "none"; 
    if(txtLastName.value.trim() == '')
    {            
        lastNameDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">Last name is required.</span>";
        lastNameDiv.style.display = "block";
        txtLastName.focus();
        valid = false;
    }
    return valid;
}
function ValidateState()
{
    var valid = true;
    var ctry = document.getElementById("ctl00_ContentPlaceHolder1_ddlcountries");
    var divtxtState = document.getElementById("stateDiv");
    divtxtState.style.display = "none";
    if(ctry.value != "1" && ctry.value != "5")
    {
        var txtState = document.getElementById("ctl00_ContentPlaceHolder1_txtState");        
        if(txtState.value.trim() == '')
        {
            divtxtState.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">Please enter state.</span>";
            divtxtState.style.display = "block";
            txtState.focus();
            valid = false;
        }
    }    
    return valid;
}
function ValidateCity()
{
    var valid = true;
    var txtCity = document.getElementById("ctl00_ContentPlaceHolder1_txtCity");
    var cityDiv = document.getElementById("cityDiv");
    txtCity.value = txtCity.value.trim();
    cityDiv.style.display = "none";
    if(txtCity.value.trim() == '')
    {
        cityDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">City name is required.</span>";
        cityDiv.style.display = "block";
        txtCity.focus();        
        valid = false;
    }
    return valid;
}
function ValidateZip()
{
    var valid = true;
    var pin = document.getElementById("ctl00_ContentPlaceHolder1_txtPin");
    var ctry = document.getElementById("ctl00_ContentPlaceHolder1_ddlcountries");
    var pinDiv = document.getElementById("pinDiv");
	pinDiv.style.display = "none";
    pin.value = pin.value.trim();
    if(ctry.value == "1" || ctry.value == "5")
    {
        if(pin.value == '')
        {
            pinDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">Zip code is required.</span>";
            pinDiv.style.display = "block";
            pin.focus();
            valid = false;
        }  
        else
        {
            if(isNaN(pin.value) && ctry.value == "1")
            {
                pinDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">Zip code must be numeric for United States.</span>";
                pinDiv.style.display = "block";
                pin.focus();
                valid = false;
            }
         } 
    }
    return valid;
}
function ValidateDOB()
{
    var valid = true;
    var date = document.getElementById("ctl00_ContentPlaceHolder1_ddlDate");
    var month = document.getElementById("ctl00_ContentPlaceHolder1_ddlMonth");
    var year = document.getElementById("ctl00_ContentPlaceHolder1_ddlYear");
    var divDOB = document.getElementById("divDOB");
    divDOB.style.display = "none";        
    if(date.value =='Day' || month.value == 0 || year.value == 'Year')
    {
        divDOB.innerHTML = "<img src='/images/error_x.jpg' hspace='3' align='left'><span style=\"color:Red;\">Date of Birth is required.</span>";
        divDOB.style.display = 'block';
        date.focus();
        valid = false;
    }
    else
    {
        if(date.value > 31)
        {
            divDOB.innerHTML = "<img src='/images/error_x.jpg' hspace='3' align='left'><span style=\"color:Red;\">Date of Birth is required.</span>";
            divDOB.style.display = 'block';
            date.focus();
            valid = false;
        }
        else
        {
            switch (month.value)
            {
                case "2":
                        if((year.value % 4) == 0)
                        {
                            if(date.value > 29)
                            {
                                divDOB.innerHTML = "<img src='/images/error_x.jpg' hspace='3' align='left'><span style=\"color:Red;\">Please enter valid date.</span>";
                                divDOB.style.display = 'block';
                                date.focus();
                                valid = false;
                            } 
                        }
                        else
                        {
                            if(date.value > 28)
                            {
                                divDOB.innerHTML = "<img src='/images/error_x.jpg' hspace='3' align='left'><span style=\"color:Red;\">Please enter valid date.</span>";
                                divDOB.style.display = 'block';
                                date.focus();
                                valid = false;
                            }  
                        }
                        break;
                 
                 case "4": 
                 case "6":
                 case "9":
                 case "11":
                        if(date.value > 30)
                        {
                            divDOB.innerHTML = "<img src='/images/error_x.jpg' hspace='3' align='left'><span style=\"color:Red;\">Please enter valid date.</span>";
                            divDOB.style.display = 'block';
                            date.focus();
                            valid = false;
                        }   
                        break;
            }
            if(valid == true)
            {
                var today = new Date();
                var DOB = new Date(year.value + '/' + month.value + '/' + date.value);
                if(DOB.format("yyyy/M/dd") > today.format("yyyy/M/dd"))
                {
                    divDOB.innerHTML = "<img src='/images/error_x.jpg' hspace='3' align='left'><span style=\"color:Red;\">Date of birth can not be greater than today.</span>";
                    divDOB.style.display = 'block';
                    date.focus();
                    valid = false;
                }
            }
        }
        
    }
    return valid;
}
function ValidatePractitioner()
{
    var valid = true;
    var divPractitioner = document.getElementById('ctl00_ContentPlaceHolder1_divPractitioner');
    
    if(divPractitioner.style.display == 'block')
    {    
        var yearsOfPractise=document.getElementById("ctl00_ContentPlaceHolder1_txtYearsInPractice");
        var divYearsOfPractise = document.getElementById("ctl00_ContentPlaceHolder1_divYearsInPractice");
        divYearsOfPractise.style.display = "none";
        yearsOfPractise.value = yearsOfPractise.value.trim();
        if(yearsOfPractise.value == '')
        {
            divYearsOfPractise.style.display = "block";
            divYearsOfPractise.innerHTML = "<img src='/images/error_x.jpg' hspace='3' align='left'><span style=\"color:Red;\">Years of practice required.</span>";
            yearsOfPractise.focus();
            valid = false;
        }        
        else if(isNaN(yearsOfPractise.value))
        {
            divYearsOfPractise.style.display = "block";
            divYearsOfPractise.innerHTML = "<img src='/images/error_x.jpg' hspace='3' align='left'><span style=\"color:Red;\">Years of practice must be a numeric value.</span>";
            yearsOfPractise.focus();
            valid = false;
        }
        else if(yearsOfPractise.value < 0)
        {
            divYearsOfPractise.style.display = "block";
            divYearsOfPractise.innerHTML = "<img src='/images/error_x.jpg' hspace='3' align='left'><span style=\"color:Red;\">Years of practice must be a positive value.</span>";
            yearsOfPractise.focus();
            valid = false;
        }          
    }
    return valid;
}
function ValidateCheckAgreement()
{
    var valid;
    var chkAggreement = document.getElementById("ctl00_ContentPlaceHolder1_chkAgreement");
    var divAggreement = document.getElementById("divCheckAggreement"); 
    divAggreement.style.display = "none"; 
    if(!chkAggreement.checked)
    {
        divAggreement.style.display = "block";
        divAggreement.innerHTML = "<img src='/images/error_x.jpg' hspace='3' align='left'><span style=\"color:Red;\">You must accept the terms of use agreement.</span>";
        chkAggreement.focus();
        return false;
    }    
    return valid;
}

function ValidateJoinInputs() 
{
    var inputValid = new Array();
    inputValid[0] = ValidateCheckAgreement();
    inputValid[1] = ValidatePractitioner();
    inputValid[2] = ValidateDOB();
    inputValid[3] = ValidateZip();
    inputValid[4] = ValidateCity();
    inputValid[5] = ValidateState();
    inputValid[6] = ValidateLastName();
    inputValid[7] = ValidateFirstName();
    inputValid[8] = ValidateDisplayName();
    inputValid[9] = ValidateSecurityAnswer();
    inputValid[10] = ValidateSecurityQuestion();
    inputValid[11] = ValidateConfirmPassword();
    inputValid[12] = ValidatePassword();
    inputValid[13] = ValidateEmail();    
    var i = 0;
    for(i=0;i<inputValid.length;i++)
    {
        if(inputValid[i] == false)
        {
            return false;
        }
    }
    return true;
}
 
 function checkEmailSyntax(str)
{
    var at="@";
    var dot=".";
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot = str.indexOf(dot);
    var reg = /^([A-Za-z0-9_\-\.\+])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/

    if (str.indexOf(at)==-1){
        return false;
    }

    if (reg.test(str) == false) {
        return false;
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       return false;
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        return false;
    }

     if (str.indexOf(at,(lat+1))!=-1)
     {
        return false;
     }

     if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        return false;
     }

     if (str.indexOf(dot,(lat+2))==-1){
        return false;
     }

     if (str.indexOf(" ")!=-1){
        return false;
     }
     return true ;                             
}

function ShowPractitioner()
{
    var rblHealth = document.getElementById("ctl00_ContentPlaceHolder1_rblHealth"); 
    var divPractitioner = document.getElementById('ctl00_ContentPlaceHolder1_divPractitioner');
    for (var i=0; i<rblHealth.rows.length; i++) 
    {  
        for (var j=0; j<rblHealth.rows[i].cells.length; j++) 
        {
            var list1 = rblHealth.rows[i].cells[j].childNodes[0]; 
            if(list1.checked) 
            {
                if(list1.value == 'Y')
                {
                    divPractitioner.style.display = 'block';
                }
                else
                {
                    var divYearsOfPractise = document.getElementById("ctl00_ContentPlaceHolder1_divYearsInPractice");
                    divYearsOfPractise.style.display = "none";
                    divPractitioner.style.display = 'none';                    
                }
            }
        }
     }
}

function remeoveSpecialCharaters()
{
        var txtdisplayname = document.getElementById("ctl00_ContentPlaceHolder1_txtdisplayname");
        var displayNameDiv = document.getElementById("ctl00_ContentPlaceHolder1_displayNameDiv");
        if(txtdisplayname.value.trim() == '')
        {
            displayNameDiv.innerHTML ="<img src='/images/error_x.jpg' hspace='3' align='left'><span style=\"color:red;\">User name is required.</span>";;
            displayNameDiv.style.display = "block";
            txtdisplayname.focus();
            return false;
        }
        else
        {            
            var oldLength= txtdisplayname.value.length;    
            txtdisplayname.value =  txtdisplayname.value.replace(/[^a-zA-Z0-9_]+/g,'');
            if (oldLength != txtdisplayname.value.length)
            {      
                alert('Special charaters have been removed from Username except underscore.');              
            } 
            return true;   
        } 
}

function ShowOther()
{
    var trOther = document.getElementById("TRother");
    if(trOther.style.display == "none")
    {
        trOther.style.display = "block";
        var txtOther = document.getElementById("ctl00_ContentPlaceHolder1_txtother");
        txtOther.focus();
        return;
    }
    trOther.style.display = "none"
}

function ValidationForChangePassword()
{
    var result = ValidateCurrentPassword();
    if(result == true)
    {
        result = ValidateChangedPassword();
        if(result == true)
        {
            return ValidateChangedConfirmPassword();
        }
    }
    return result;
}
function ValidateChangedPassword()
{
    var valid = true;
    var passwordDiv = document.getElementById("ctl00_ContentPlaceHolder1_msgDiv");
    var password = document.getElementById("ctl00_ContentPlaceHolder1_lblNew");
    passwordDiv.style.display = "none"; 
    if(password.value == '')
    {   
        passwordDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">New Password is required.</span>";
        passwordDiv.style.display = "block";
        password.focus();
        valid = false;
    }      
    else
    {    
        if(password.value.length < 6)
        {
            passwordDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">New Password must have atleast 6 characters.</span>";
            passwordDiv.style.display = "block";
            password.focus();
            valid = false;
        }        
    }
    return valid;
}
function ValidateChangedConfirmPassword()
{
    var valid = true;
    var passwordDiv = document.getElementById("ctl00_ContentPlaceHolder1_msgDiv");
    var ConfirmPassword = document.getElementById("ctl00_ContentPlaceHolder1_lblConfirm");
    var Newpassword = document.getElementById("ctl00_ContentPlaceHolder1_lblNew");
    passwordDiv.style.display = "none"; 
    if(ConfirmPassword.value == '')
    {   
        passwordDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">Confirm Password is required.</span>";
        passwordDiv.style.display = "block";
        ConfirmPassword.focus();
        valid = false;
    }      
    else
    {    
        if(ConfirmPassword.value != Newpassword.value)
        {
            passwordDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">Confirm Password must match with new password.</span>";
            passwordDiv.style.display = "block";
            ConfirmPassword.focus();
            valid = false;
        }        
    }
    return valid;
}
function ValidateCurrentPassword()
{
    var valid = true;
    var passwordDiv = document.getElementById("ctl00_ContentPlaceHolder1_msgDiv");
    var ConfirmPassword = document.getElementById("ctl00_ContentPlaceHolder1_lblCurrent");
    passwordDiv.style.display = "none"; 
    if(ConfirmPassword.value == '')
    {   
        passwordDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">Current Password is required.</span>";
        passwordDiv.style.display = "block";
        ConfirmPassword.focus();
        valid = false;
    } 
    return valid;
}

function btnDisable()
{
    document.getElementById('ctl00_ContentPlaceHolder1_btnSubmit').disabled = true;
}

/**** linh ****/
function ValidateUserName() {
    var valid = true;
    var txtdisplayname = document.getElementById("ctl00_ContentPlaceHolder1_Username");
    var displayNameDiv = document.getElementById("ctl00_ContentPlaceHolder1_displayNameDiv");
    displayNameDiv.style.display = "none";
    txtdisplayname.value = txtdisplayname.value.trim();
    
    if (txtdisplayname.value.trim() == '') {
        displayNameDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">User Name is required.</span>";
        displayNameDiv.style.display = "block";
        txtdisplayname.focus();
        valid = false;
    }
    else {
        if (txtdisplayname.value.length < 6) {
            displayNameDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\"> User Name must have at least 6 characters.</span>";
            displayNameDiv.style.display = "block";
            displayNameDiv.focus();
            valid = false;
        }
        else {
            if (txtdisplayname.value.indexOf(' ') > 0) {
                displayNameDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">Spaces are not allowed in User Name.</span>";
                displayNameDiv.style.display = "block";
                txtdisplayname.focus();
                valid = false;
            }
            else {
                var oldLength = txtdisplayname.value.length;
                txtdisplayname.value = txtdisplayname.value.replace(/[^a-zA-Z0-9_]+/g, '');

                if (oldLength != txtdisplayname.value.length) {
                    displayNameDiv.innerHTML = "<img src='/images/error_x.jpg'  hspace='3' align='left'><span style=\"color:Red;\">Special charaters are not allowed in User Name except underscore.</span>";
                    displayNameDiv.style.display = "block";
                    txtdisplayname.focus();
                    valid = false;
                }
                else {
                    valid = true;
                }
            }
        }
    }
    return valid;
}