var alphanumchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzÁáÉéÍíÓóÚúñÑÀÂÈÊàâèêôû0123456789";
var alphachars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzÁáÉéÍíÓóÚúñÑÀÂÈÊàâèêôû";

function isAlphanumeric(s, valid_chars) {
	var allValid = true;
	var ch = '';
	for (i = 0;  i < s.length;  i++) {
		ch = s.charAt(i);
		for (j = 0;  j < valid_chars.length;  j++)
			if (ch == valid_chars.charAt(j))
				break;
			if (j == valid_chars.length) {
				allValid = false;
			break;
		}
	}
	if (s.match("--")){
		allValid = false;
	}
	return allValid;
}


function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}



function isValidEmail(s){
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(s)){
		return true;
	} else {
		return false;
	}

}

function LTrim(str) {
    var s = new String(str);
    var whitespace = new String(' \t\n\r');

    // Strip all leading white-space characters
    if (whitespace.indexOf(s.charAt(0)) != -1) {

        var j=0, i = s.length;
    
        // Iterate from the left until we have no more whitespace...
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
            j++;

        // Get the substring from the first non-whitespace 
        // character to the end of the string...
        s = s.substring(j, i);
    }
    return s;
}

function RTrim(str) {
    var s = new String(str);
    var whitespace = new String(' \t\n\r');

    // Strip all trailing white-space characters
    if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {

        var i = s.length - 1;

        // Iterate from the right until we have no more whitespace...
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
            i--;

        // Get the substring from the beginning of the string to
        // where the last non-whitespace character
        s = s.substring(0, i+1);
    }
    return s;
}

function Trim(str) {
    // Strip away all leading and trailing white-space characters
    return RTrim(LTrim(str));
}

//used to control keypress event in phone1, phone2, phone3 to accept numeric only

function isNumberKey(evt)

      {

        var charCode = (evt.which) ? evt.which : event.keyCode
        
                pos1=document.getElementById('phone1').value.indexOf("0");
                    if (pos1==0)
                    {
                         alert("First digit should not be zero.");
                         document.getElementById('phone1').value='';
                         return false;
                     }


        if (charCode > 31 && (charCode < 48 || charCode > 57))
            {
                 return false;
                 
            }
                 return true;

      }



//used to control max input of characters for password
function textCounter(field, maxlimit) {
	if (field.value.length > maxlimit) {
	    //alert(field.name);
		field.value = field.value.substring(0, field.value.length - 1)	
		if (field.name=='phone1')
		{
		document.getElementById('phone2').focus();
		}
		if (field.name=='phone2')
		{
		document.getElementById('phone3').focus();
		}
		if (field.name=='phone3')
		{
		document.getElementById('gtime').focus();
		}

	return true;
	}
}

var nodigit;
nodigit=4;
function phonedigit1()
{
    var obj=document.form1.country.value;
    var divobj=document.getElementById('AU');
    if (obj=='AU')
    {
        nodigit=3;
        divobj.innerHTML="(999-999-999)" 
        divobj.style.visibility='visible';
        
    }
    else
    {
        nodigit=4;
        divobj.innerHTML="(999-999-9999)" 
        divobj.style.visibility='visible';
        
    }
    


}


function phone3blur()
{
    var divobj=document.getElementById('AU');
    divobj.style.visibility='hidden';
}


//used to check military time
function validate(oForm){
Date.prototype.toMilitaryString = function(hasLeadingZeroHour){ 
if (isNaN(this)) return "";
var h = this.getHours();
var m = this.getMinutes();
if (hasLeadingZeroHour){
if (h < 10) h = "0" + h;
}
if (m < 10) m = "0" + m;
return h + ":" + m;
}

//validate event time
var oTime = oForm.gtime;
if (oTime.value == ""){
alert(oForm.jsalert29.value);
//oTime.focus();
return false;
}
var d = new Date("1/1/2004 " + oTime.value);
var mt = d.toMilitaryString(true);
if (mt != ""){
oTime.value = mt;
//alert("Time:" + oTime.value); //debug
}
else {
alert(oForm.jsalert30.value);
//oTime.focus();
return false;
}
return true;
}





function ValidateForm(form) {
	var str1 = str2 = '';
	// FIRST NAME
	if(form.first != null) {
		form.first.value = Trim(form.first.value);
		str = form.first.value;
		if(str == '' || str.length < 1) {
			alert(form.jsalert3.value);
			form.first.focus();
			return false;
		}
		if(!isAlphanumeric(str, alphachars + '-.` \'')) {
			alert(form.jsalert4.value);
			form.first.focus();
			form.first.select();
			return false;    
		}
	}
	// LAST NAME
	if(form.last != null) {
		form.last.value = Trim(form.last.value);
		str = form.last.value;
		if(str == '' || str.length < 1) {
			alert(form.jsalert5.value);
			form.last.focus();
			return false;
		}
		if(!isAlphanumeric(str, alphachars + '-.` \'')) {
			alert(form.jsalert6.value);
			form.last.focus();
			form.last.select();
			return false;    
		}
	}
	// ADDRESS
	if(form.address != null) {
		form.address.value = Trim(form.address.value);
		str = form.address.value;
		if(str == '' || str.length < 1) {
			alert(form.jsalert7.value);
			form.address.focus();
			return false;
		}
		if(!isAlphanumeric(str, alphanumchars + '-.` \',#')) {
			alert(form.jsalert8.value);
			form.address.focus();
			form.address.select();
			return false;    
		}
	}
	// CITY
	if(form.city != null) {
		form.city.value = Trim(form.city.value);
		str = form.city.value;
		if(str == '' || str.length < 1) {
			alert(form.jsalert9.value);
			form.city.focus();
			return false;
		}
		if(!isAlphanumeric(str, alphachars + '-.` \',#')) {
			alert(form.jsalert10.value);
			form.city.focus();
			form.city.select();
			return false;    
		}
	}
	// PROVINCE/STATE
	if(form.state != null) {
		if(form.state.selectedIndex == 0) { // first item selected
			alert(form.jsalert11.value);
			form.state.focus();
			return false;
		}       
	}
	// COUNTRY
	if(form.country != null) {
		if(form.country.selectedIndex == 0) { // first item selected
			alert(form.jsalert12.value);
			form.country.focus();
			return false;
		}
	}
	// POSTAL / ZIP
	if(form.zip != null) {
		if(form.country.selectedIndex <= 2) {
			form.zip.value = Trim(form.zip.value);
			str = form.zip.value;
			if(str == '') {
				alert(form.jsalert13.value);
				form.zip.focus();
				return false;
			}
			if(!isAlphanumeric(str.toUpperCase(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 -')) {
				alert(form.jsalert14.value);
				form.zip.focus();
				form.zip.select();				
				return false;    
			}
		}
	}
	
		// PHONE1
	if(form.phone1 != null) {
		form.phone1.value = Trim(form.phone1.value);
		str = form.phone1.value;
		if(str == '') {
			alert('Please enter first 3 digit of the phone.');
			form.phone1.focus();
			return false;
		}
		if(!isAlphanumeric(str, '0123456789')) {
			alert('Please enter numeric only');
			form.phone1.focus();
			form.phone1.select();
			return false;    
		}
		pos1=str.substring (0,1);;
        if (pos1=='0')
        {
             alert("First digit should not be zero.");
             str='';
             form.phone1.focus();
             return false;
         }

		if (str.length < 3)
		{
			alert('Please enter 3 digit.');
			form.phone1.focus();
			//form.phone1.select();
			return false;    

		}
	}

		// PHONE2
	if(form.phone2 != null) {
		form.phone2.value = Trim(form.phone2.value);
		str = form.phone2.value;
		if(str == '') {
			alert('Please enter first 3 digit of the phone.');
			form.phone2.focus();
			return false;
		}
		if(!isAlphanumeric(str, '0123456789')) {
			alert('Please enter numeric only');
			form.phone2.focus();
			form.phone2.select();
			return false;    
		}
		if (str.length < 3)
		{
			alert('Please enter 3 digit.');
			form.phone2.focus();
			//form.phone1.select();
			return false;    

		}

	}

		// PHONE3
	if(form.phone3 != null) {
		form.phone3.value = Trim(form.phone3.value);
		str = form.phone3.value;
		if(str == '') {
			alert('Please enter first 3 digit of the phone.');
			form.phone3.focus();
			return false;
		}
		if(!isAlphanumeric(str, '0123456789')) {
			alert('Please enter numeric only');
			form.phone3.focus();
			form.phone3.select();
			return false;    
		}
    	if (str.length < nodigit)
		{
			alert('Please enter ' + nodigit + ' digit.');
			form.phone3.focus();
			//form.phone1.select();
			return false;    

		}


	}


	// PHONE1
//	if(form.phone1 != null) {
//		form.phone1.value = Trim(form.phone1.value);
//		str = form.phone1.value;
//		alert('hi');
//		if(str == '') {
//			alert("You must supply a Country Code for your phone number.");
//			form.phone1.focus();
//			return false;
//		}
//		if(!IsNumeric(str)) {
//			alert("You must supply a numeric value in the Country Code for your phone number.");
//			form.phone1.focus();
//			return false;    
//		}
//		if (str.length == 3)
//		{
//		    form.phone2.focus();
//		}
//			alert("You must supply a Phone1 for your phone number." + str.length);
//				
//
//	}

	// PHONE FORMAT (ONLY US)
	/*if(form.phone != null) {
		if(form.country.selectedIndex == 1) {
			form.phone.value = Trim(form.phone.value);
			str = form.phone.value;
			if(str != '') {
				if((str.match(/^[ ]*[(]{0,1}[ ]*[0-9]{3,3}[ ]*[)]{0,1}[-]{0,1}[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/)==null) && ((str.match(/^[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/)==null))) {
 				alert("Enter a valid US phone number");
				form.phone.focus();
 				return false;
				}
			}
		}
	}
	
	*/
	
	// COMBO PHONE FORMAT (ONLY US FOR UPDATE PLAYER)	
	if(form.fullphone != null) {
		if(form.country.selectedIndex == 1) {
			//form.fullphone.value = Trim(form.fullphone.value);
			str1 = form.fullphone.value.substring (0,3);
			str2 = form.fullphone.value.substring (3);

			if (str1 != ''){
				if (!(str1 == "001")){
					alert ("Start with US country code 001 and then your phone number.")
					form.fullphone.focus();					
					return false;
				}
			}

			if(str2 != '') {
				if((str2.match(/^[ ]*[(]{0,1}[ ]*[0-9]{3,3}[ ]*[)]{0,1}[-]{0,1}[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/)==null) && ((str2.match(/^[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/)==null))) {
 					alert("Please enter a valid US phone number after the Country Code.\r\nNote: The Country Code you have entered is correct.");
					form.fullphone.focus();
 					return false;
				}
			}
		}
	}
	
	
	// PHONE
	if(form.phone != null) {
		form.phone.value = Trim(form.phone.value);
		str = form.phone.value;
		if(str == '') {
			alert(form.jsalert15.value);
			form.phone.focus();
			return false;
		}
		if(!isAlphanumeric(str, '0123456789 -().#+')) {
			alert(form.jsalert16.value);
			form.phone.focus();
			form.phone.select();
			return false;    
		}
	}
	
	// COMBO PHONE (FOR PLAYER UPDATE)
	if(form.fullphone != null) {
		//form.fullphone.value = Trim(form.fullphone.value);
		str = form.fullphone.value;
		if(str == '') {
			alert(form.jsalert15.value);
			form.fullphone.focus();
			return false;
		}
		if(!isAlphanumeric(str, '0123456789 -().#+')) {
			alert(form.jsalert16.value);
			form.fullphone.focus();
			form.fullphone.select();
			return false;
		}
	}	

	// GoodTime (good time to talk)
	if(form.gtime != null) {
		if (form.gtime.selectedIndex == 0) {
			alert(form.jsalert29.value);
			form.gtime.focus();
			return false;
		}
		
	}
	
	// GoodTime (good time to talk for Player Update)
	if(form.goodtime != null) {
		if (form.goodtime.selectedIndex == 0) {
			alert(form.jsalert29.value);
			form.goodtime.focus();
			return false;
		}
		
	}

	// DATE OF BIRTH (MONTH)
	if(form.birthmonth != null) {
		if(form.birthmonth.selectedIndex == 0) { // first item selected
			alert(form.jsalert20.value);
			form.birthmonth.focus();
			return false;
		}
	}

	// DATE OF BIRTH (DAY)
	if(form.birthday != null) {
		if(form.birthday.selectedIndex == 0) { // first item selected
			alert(form.jsalert21.value);
			form.birthday.focus();
			return false;
		}
	}
	
	// DATE OF BIRTH (PLAYER UPDATE)
	if(form.birthdate != null) {
		if(form.birthdate.selectedIndex == 0) { // first item selected
			alert(form.jsalert21.value);
			form.birthdate.focus();
			return false;
		}
	}	

	// GENDER

	if(form.gender != null) {
		//alert(form.jsalert28.value);
		if(form.gender.selectedIndex == 0) { // first item selected
			alert(form.jsalert28.value);
			form.gender.focus();
			return false;
		}
	}

	// ALIAS
	if(form.alias != null) {
		// required; must be 3 to 15 characters in length...
		form.alias.value = Trim(form.alias.value);
		str = form.alias.value;
		str1 = str.toLowerCase();
		if(str == '' || str.length < 3) {
			alert(form.jsalert22.value);
			form.alias.focus();
			return false;
		}
		// must contain alphanumeric characters only...
		if(!isAlphanumeric(str, alphanumchars + '~`!@$*^(),.') || (str1.charAt(0)=='c' && str1.charAt(1)=='m')) {
			alert(form.jsalert23.value);
			form.alias.focus();
			form.alias.select();
			return false;
		}
	}
	// PASSWORD
	if(form.password != null) {
		form.password.value = Trim(form.password.value);
		str = form.password.value;
		if (form.name != 'formPrfUpd') {
			if(str == '') {
				alert(form.jsalert24.value);
				form.password.focus();
				return false;
			}
		}
		// must contain alphanumeric characters only...
		if(!isAlphanumeric(str, alphanumchars)) {
			alert(form.jsalert25.value);
			form.password.value="";
			form.password2.value="";
			form.password.select();
			form.password.focus();
			return false;
		} 
	}
	// PASSWORD2
	if(form.password2 != null){
		form.password.value = Trim(form.password.value);
		form.password2.value = Trim(form.password2.value);
		str1 = form.password.value;
		str2 = form.password2.value;
		if (!(str1==str2)){
			alert(form.jsalert26.value);
			form.password.value="";
			form.password2.value="";
			form.password.focus();
			return false;
		}
	}

	// E-MAIL
	if(form.email != null) {
		form.email.value = Trim(form.email.value);
		str = form.email.value;
		if(!isAlphanumeric(str.toLowerCase(), 'abcdefghijklmnopqrstuvwxyz0123456789@._-< >')) {
			alert(form.jsalert17.value);
			form.email.focus();
			form.email.select();
			return false;    
		}
		if(str == '' || !isValidEmail(str)) {
			alert(form.jsalert18.value);
			form.email.select();
			return false;
		}
	}
	// E-MAIL2
	if(form.email2 != null) {
		form.email.value = Trim(form.email.value);
		form.email2.value = Trim(form.email2.value);
		str1 = form.email.value;
		str2 = form.email2.value;
		if (!(str1 == str2)) {
			alert(form.jsalert19.value);
			form.email2.value="";
			form.email2.focus();
			return false;    
		}
	}
	

	// REFERRAL
	if(form.buddy != null) {
		form.buddy.value = Trim(form.buddy.value);
		str = form.buddy.value;
		// must contain alphanumeric characters only...
		if(!isAlphanumeric(str.toUpperCase(), alphanumchars)) {
			alert(form.jsalert27.value);
			form.buddy.value = "";
			form.buddy.focus();
			return false;
		}
	}	
	// All is valid, submit form
	return true;
}

// focus cursor on first field when page loads
function focusAlias(){
	document.form1.alias.focus();
	document.form1.alias.select();
}

function focusEmail(){
	document.form1.email.focus();
	document.form1.email.select();
}

function focusBuddy(){
	document.form1.buddy.select();
	document.form1.buddy.focus();
}

function focusFirst(){
	document.form1.first.focus();
}

function focusFirstStep1(){
	document.form1.alias.focus();
}