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 max input of characters for password
function textCounter(field, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, field.value.length - 1)	
	//remove the last character entered because it's the maxlimit + 1 character
	alert(form.jsalert1.value + maxlimit + form.jsalert2.value);
	return true;
	}
}



//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;
}
//added 040909 begin
/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
var mdy=0;
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 (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		mdy='m'
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		mdy='d'
		return false
	}
	if (strYear.length != 4 || year==0){
		alert("Please enter a valid 4 digit year")
		mdy='y'
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}
//added 040909 end




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;    
			}
		}
	}

	// PHONEPREFIX
	if(form.phoneprefix != null) {
		form.phoneprefix.value = Trim(form.phoneprefix.value);
		str = form.phoneprefix.value;
		if(str == '') {
			alert("You must supply a Country Code for your phone number.");
			form.phoneprefix.focus();
			return false;
		}
		if(!IsNumeric(str)) {
			alert("You must supply a numeric value in the Country Code for your phone number.");
			form.phoneprefix.focus();
			return false;    
		}
	}

	// 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;
		}
	}	

    // birth year
	if(form.yyyy != null) {
		form.yyyy.value = Trim(form.yyyy.value);
		str = form.yyyy.value;
		if(str == '') {
			
			alert('Please enter birthday year.');
			form.yyyy.focus();
			return false;
		}
		if(!isAlphanumeric(str, '0123456789')) {

            alert('Please enter numeric value in birth year.');
			form.yyyy.focus();

			return false;    
		}
		if(str.length!=4) {

            alert('Please enter birth year in 4 digit.');
			form.yyyy.focus();

			return false;    
		}
		var now = new Date();
		if( ((eval(str)+21) > now.getFullYear() ) ) {

            alert('You have to be at least 21 years to register.');
			form.yyyy.focus();

			return false;    
		}
		if( ((eval(str)+125) < now.getFullYear() ) ) {

            alert('Please enter your correct year of birth.');
			form.yyyy.focus();

			return false;    
		}
		
	}

//birthdate validation
	if(form.yyyy != null) {
	    var dt1=form.birthmonth.value;
	    var dt2=form.birthday.value;
	    var dt3=form.yyyy.value;
	    var dt=dt1 + "/" + dt2 + "/" + dt3;
	    if (isDate(dt)==false){
	        if (mdy=='m') form.birthmonth.focus();
	        if (mdy=='d') form.birthday.focus();
	        if (mdy=='y') form.yyyy.focus();
		    return false
	    }
              var dateToCheck=new Date ( dt3, dt1-1, dt2 )
              var eighteenYearsAgo = new Date();

              eighteenYearsAgo.setYear(eighteenYearsAgo.getFullYear() - 21);
              eighteenYearsAgo.setHours(0);
              eighteenYearsAgo.setMinutes(0);
              eighteenYearsAgo.setSeconds(1);

              if (dateToCheck.getTime() > eighteenYearsAgo.getTime())
              {
                alert("You must be at least 21 years of age.");
                form.yyyy.focus();
                //objField.select();
                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')) {
		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();
}