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 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;
	}

}

//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;
	}
}

function ValidateForm(form) {

	// ALIAS
	if(form.alias != null) {
		// required; must be 3 to 15 characters in length...  
		str = form.alias.value
		if(str == '' || str.length < 3) {
			alert(form.jsalert22.value);
			form.alias.focus();
			return false;
		}
		// must contain alphanumeric characters only...
		if(!isAlphanumeric(str, alphanumchars + '~`!@#$*^(),.?')) {
			alert(form.jsalert23.value);
			form.alias.focus();
			form.alias.select();
			return false;
		}
	}
	

	var str1 = str2 = '';
	// FIRST NAME
	if(form.first != null) {
		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) {
		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) {
		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) {
		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) {
			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;    
			}
		}
	}
	
 	
	// PHONE
	if(form.phone != null) {  
		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;    
		}
	}



	// E-MAIL
	if(form.email != null) {
		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) {
		str1 = form.email.value;
		str2 = form.email2.value;
		if (!(str1 == str2)) {
			alert(form.jsalert19.value);
			form.email2.value="";
			form.email2.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;
		}
	}

	// PASSWORD
	if(form.password != null) {
		str = form.password.value;
		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){
		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;
		}
	}

	// REFERRAL
	if(form.buddy != null) {
		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 good, 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 doStateChange(form) {
    if(form != null && form.state != null && form.country !=null) {
        // if a state is selected, then pre-select United States for the country
        if(form.state.selectedIndex >= 2 && form.state.selectedIndex <= 52)
            form.country.selectedIndex = 1;
        // if a province/territory is selected, then pre-select Canada for the country
        if(form.state.selectedIndex >= 53 && form.state.selectedIndex <= 65)
            form.country.selectedIndex = 2;
    }
}

function doStateChange(form) {
    if(form != null && form.state != null && form.country !=null) {
		// if other is selected, then pre-select -Select Country- for the country
        if(form.state.selectedIndex == 1)
			form.country.selectedIndex = 0;
        // if a state is selected, then pre-select United States for the country
		if(form.state.selectedIndex >= 2 && form.state.selectedIndex <= 52)
            form.country.selectedIndex = 1;
        // if a province/territory is selected, then pre-select Canada for the country
        if(form.state.selectedIndex >= 53 && form.state.selectedIndex <= 68)
            form.country.selectedIndex = 2;
    }
}

function doCountryChange(form) {
    if(form != null && form.state != null && form.country !=null) {
        var selectedCountryCode = form.country.options[form.country.selectedIndex].value.toUpperCase();

        // if selected country isn't Canada or the US, then pre-select 'Outside the US or Canada'
        if(selectedCountryCode != 'US' && selectedCountryCode != 'CA')
            form.state.selectedIndex = 1;  // 1 = 'Outside the US or Canada'

        // if the country selection is United States, jump to the first state in the list
        if(selectedCountryCode == 'US')
            form.state.selectedIndex = 2;  // 2 = 'Alabama'

        // if the country selection is Canada, jump to the first province in the list
        if(form.country.options[form.country.selectedIndex].value == 'CA')
            form.state.selectedIndex = 53;  // 53 = 'Alberta'
    }
}