/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function validate(){
    
    var data = '';
    data = document.getElementById('alias_name').value;
    alert ("data - "+data);
    var iChars = "!#$%^&*()+=-[]\\\';,/{}|\":<>?~ ";
    for (var i = 0; i < data.length; i++){
        //alert ("herer - "+iChars.indexOf(data.charAt(i)));
        if (iChars.indexOf(data.charAt(i)) != -1){
          return false;
        }else{
            alert("alias contains special character");
        }
    }
    return false;
}

// function to validate
function Validate(){
    var DOB           = '';
    var error_message = '';
    var screenname    = '';
    var Email         = '';
    var terms         = '';
    var check_email   = '';
    var check_al      = '';

    DOB           = document.getElementById('age').value;
    screenname    = document.getElementById('Name').value;
    Email         = document.getElementById('Email').value;
    error_message = document.getElementById('error_message');
    terms = document.getElementById('Terms').checked;
    
    if (DOB == '') {
        error_message.innerHTML = 'Please Enter Your Age';
        document.getElementById('age').style.backgroundColor="RED";
        document.getElementById('age').style.color="WHITE";
        document.getElementById('age').focus();
        return false;
    }else if (screenname == '') {
        error_message.innerHTML = 'Please Enter Your Screen Name';
        document.getElementById('Name').style.backgroundColor="RED";
        document.getElementById('Name').style.color="WHITE";
        document.getElementById('Name').focus();
        return false;
    }else if (Email == '') {
        error_message.innerHTML = 'Please Email Address';
        document.getElementById('Email').style.backgroundColor="RED";
        document.getElementById('Email').style.color="WHITE";
        document.getElementById('Email').focus();
        return false;
    }else if(terms == false){
         error_message.innerHTML = 'Please accept the Terms & Conditions';
         document.getElementById('Terms').style.backgroundColor="RED";
         document.getElementById('Terms').style.color="WHITE";
         return false;
    }else{
        //alert("all ok");
    }

    if (screenname.indexOf(' ') != -1){
        alert("Please Remove White Spcaes From Your Screen Name");
        error_message.innerHTML = 'Please Remove White Spcaes From Your Screen Name';
        document.getElementById('Name').style.backgroundColor="RED";
        document.getElementById('Name').style.color="WHITE";
        document.getElementById('Name').focus();
        return false;
    }

    if (Email != '') {
        cemail = echeck(Email);
        if(cemail == false){
            error_message.innerHTML = 'Please Re-Enter From Email Address';
            document.getElementById('Email').style.backgroundColor="RED";
            document.getElementById('Email').style.color="WHITE";
            document.getElementById('Email').focus();
            return false;
        }else{
            check_email = 'T';
        }
    }
    if (screenname != '') {
        var checkalias = '';
        checkalias = ValidateSpecialCharcs(screenname);
        if(checkalias == false){
            error_message.innerHTML = 'Please Remove Special Characters From Your Screen Name';
            document.getElementById('Name').style.backgroundColor="RED";
            document.getElementById('Name').style.color="WHITE";
            document.getElementById('Name').focus();
            return false;
        }else{
            check_al = 'T';
        }
    }

    

//    if(screenname.indexOf(' ') == -1){
//        error_message.innerHTML = 'Please Remove White Spcaes From Your Screen Name';
//        document.getElementById('Name').style.backgroundColor="RED";
//        document.getElementById('Name').style.color="WHITE";
//        document.getElementById('Name').focus();
//        return false;
//    }else{
//            check_al = 'T';
//    }

    if((check_al == 'T') && (check_email == 'T')){
        //return true;

        document.getElementById('age').style.backgroundColor="WHITE";
        document.getElementById('age').style.color="BLACK";

        document.getElementById('Email').style.backgroundColor="WHITE";
        document.getElementById('Email').style.color="BLACK";
        
        document.getElementById('Name').style.backgroundColor="WHITE";
        document.getElementById('Name').style.color="BLACK";
        error_message.innerHTML = '';
        //alert("allllllllllll ok");

        return true;
    }else{
        return false;
    }
}

// function to validate
function HelpValidate(){
    var error_message = '';
    var Email         = '';
    var check_email   = '';

    Email         = document.getElementById('Email').value;
    error_message = document.getElementById('error_message');
    
    if (Email == '') {
        error_message.innerHTML = 'Please Email Address';
        document.getElementById('Email').style.backgroundColor="RED";
        document.getElementById('Email').style.color="WHITE";
        document.getElementById('Email').focus();
        return false;
    }else{
        //alert("all ok");
    }
    
    if (Email != '') {
        cemail = echeck(Email);
        if(cemail == false){
            error_message.innerHTML = 'Please Re-Enter Email Address';
            document.getElementById('Email').style.backgroundColor="RED";
            document.getElementById('Email').style.color="WHITE";
            document.getElementById('Email').focus();
            return false;
        }else{
            check_email = 'T';
        }
    }

    if(check_email == 'T'){
        //return true;
        document.getElementById('Email').style.backgroundColor="WHITE";
        document.getElementById('Email').style.color="BLACK";
        error_message.innerHTML = '';
        //alert("allllllllllll ok");
        return true;
    }else{
        return false;
    }
}


// function to check for specials characters
function ValidateSpecialCharcs(data){
    //var data = '';
    //data = document.getElementById(id).value;
    var iChars = "!#£¦$%^&*()/+=[]\\\';,/{}|\":<>?~`";
    for (var i = 0; i < data.length; i++){
        if (iChars.indexOf(data.charAt(i)) != -1){
          return false;
        }
    }
}

function echeck(str) {

    var at="@";
    var dot=".";
    var www = "www.";
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot=str.indexOf(dot);
    var http = 'http://';

    if (str.indexOf(at)==-1){
        alert("Invalid E-mail ID. Please enter a Valid Email Address");
        return false
    }else if (str.indexOf(www)!=-1){
        alert("Invalid E-mail ID. Please enter a Valid Email Address");
        return false
    }else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
        alert("Invalid E-mail ID. Please enter a Valid Email Address");
        return false
    }else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        alert("Invalid E-mail ID. Please enter a Valid Email Address");
        return false
    }else if (str.indexOf(at,(lat+1))!=-1){
        alert("Invalid E-mail ID. Please enter a Valid Email Address");
        return false
    }else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        alert("Invalid E-mail ID. Please enter a Valid Email Address");
        return false
    }else if (str.indexOf(dot,(lat+2))==-1){
        alert("Invalid E-mail ID. Please enter a Valid Email Address");
        return false
    }else if (str.indexOf(" ")!=-1){
        alert("Invalid E-mail ID. Please enter a Valid Email Address");
        return false
    }else if( (str.length - (str.lastIndexOf(dot) + 1)) < 2 )  {
        alert("Invalid E-mail ID. Please enter a Valid Email Address");
        return false
    }else if(str.length > 1){

        var data      = '';
        var spcemail  = '';
        var spaddress = '';
        
        data      = str.split('@');
        spcemail  = ValidateSpecialCharcs(data[0]);
        spaddress = ValidateSpecialCharcs(data[1]);
        

        if(spcemail == false){
            alert("Invalid E-mail ID. Please Remove Special Character Re-Enter a Valid Email Address");
            return false;
        }

        if(spaddress == false){
            alert("Invalid E-mail ID. Please Remove Special Character Re-Enter a Valid Email Address");
            return false;
        }
    }
    else{
        return true
    }
}