var wrong_path='<img height="15" width="16" alt="correct" src="http://blog.buyplaywin.com/wp-content/signup/i-wrong.gif"/>';
var correct_path='<img height="15" width="16" alt="correct" src="http://blog.buyplaywin.com/wp-content/signup/i-correct.gif"/>';

  
  ////////////        script for signup Page

var is_valid_signup=true;
var is_valid_username_form=false;
var is_valid_email_form=false;
var is_valid_password_form=false;
//var is_email_confirmed=false;
//var is_password_confirmed=false;

var password_notice='must be at least 6 characters';

var is_valid_email=false;
 function validate_email(elm,conf){
    var str=elm.value;
    var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if(str==""){
        $("email_status").innerHTML="";
    }
	else{
	    if (str.match(emailRegEx)){
       	    $("email_status").innerHTML=correct_path+'&nbsp;';
	           email_confirmation(conf);
       	    is_valid_email=true;
    		}
	    else{
 	         $("email_status").innerHTML=wrong_path+'&nbsp;';
	         is_valid_email=false;
	    }	
 		
	}
}

var is_valid_email_conf=false;
function email_confirmation(elm){
    if (elm.value!=''){
        if($('user_email').value==elm.value) {
            $("email_conf_status").innerHTML=correct_path;
            is_valid_email_conf=true;
        }
        else{
            $("email_conf_status").innerHTML=wrong_path+' must match email';    
            is_valid_email_conf=false;
        }   
    }
    else{
        $("email_conf_status").innerHTML='';
        is_valid_email_conf=false;
    }
}

var is_valid_username=false;
function validate_username(elm){
 //"use only letters, numbers, and .-_@ please."
    var str=elm.value;
    var usernameRegEx =/^[a-zA-Z0-9_\.@-]{3,50}$/;
    if(str==""){
        $("username_status").innerHTML="must be at least 3 characters";
    }
    else{
        if (str.match(usernameRegEx)){
            $("username_status").innerHTML=correct_path;
            is_valid_username=true;
            }
        else{
               if(str.length<=3){
                $("username_status").innerHTML='must be at least 3 characters';
                is_valid_username=false;
                }
                else{
                    $("username_status").innerHTML=wrong_path+' invalid characters included';
                    is_valid_username=false;
                }
        }
    }
   
    }
var is_valid_password=false;
function hide_password_notice(elm,conf){
    var str=elm.value;
    if(str.length>5){
        $("pass_note").innerHTML=correct_path;
        is_valid_password=true;
        if($('user_password_confirmation').value!=""){
            match_password(conf.value);
        }
    }
    else{
        $("pass_note").innerHTML=password_notice;
        is_valid_password=false;
        if($('user_password_confirmation').value!=""){
            match_password(conf.value);
        }
    }
}

var is_valid_password_conf=false;
function match_password(elm){
    if(elm!=""){
        if(elm!=$('user_password').value){
            $("pass_conf_status").innerHTML=wrong_path+' must match password';
            is_valid_password_conf=false;
        }
        else{
            $("pass_conf_status").innerHTML=correct_path;
            is_valid_password_conf=true;
        }
    }
    else{
        $("pass_conf_status").innerHTML='';
            is_valid_password_conf=false;
    }
}

function signup_form_validate()
{
 
    if(is_valid_username && is_valid_email && is_valid_email_conf && is_valid_password && is_valid_password_conf) {
        if($('user_login').value=="" || $('user_email').value=="" || $('signup_conf_mail').value=="" || $('user_password').value=="" ||  $('user_password_confirmation').value==""){
            alert("All fields required...");
            return false;
        }
        else if($('user_email').value!=$('signup_conf_mail').value){
            alert("Email does't match...");
            return false;
        }
        else if($('user_password').value!=$('user_password_confirmation').value){
            alert("Password does't match...");
            return false;
        }
        else if($('birth_day').selectedIndex==0 ||$('birth_month').selectedIndex==0 || $('birth_year').selectedIndex==0 ){
            alert("Please select Date of Birth");
            return false;
        }
        else if(!isDate($('birth_month')[$('birth_month').selectedIndex].value+'/'+$('birth_day')[$('birth_day').selectedIndex].value+'/'+$('birth_year')[$('birth_year').selectedIndex].value)){
            return false;
        }
        else if($('accept_terms').checked!=true){
            alert("Please accept 'terms and conditions'");
            return false;
        }
        else{
            return true;
        }
        
    }    
    else{
        return false;
    }
}

  
  ////////////     End Of script for signup Page
  

var dtCh= "/";
var minYear=1800;
var maxYear=2200;

function isInteger(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 isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=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("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function ValidateForm(){

	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){

		return false
	}
    return true
 }
