var alertTxt = "";
var alertArray = new Array();

$(document).ready(function() {
	$("#Login").keyup(function() { 
		var search = $("#Login").val(); 
		$.ajax({
			type: "POST", 
			url: "/ajax/signup.asp", 
			data: "illegalchars=" + search, 
			success: function(message) {
				if(message=="ILLEGALCARS"){
					updateSpan("Login",message);
				} else {
					$("#LoginSpan").empty();
				}
			}
		});
	});
	$("#Password1").keyup(function() { 
		var search = $("#Password1").val(); 
		$.ajax({
			type: "POST", 
			url: "/ajax/signup.asp", 
			data: "illegalchars=" + search, 
			success: function(message) {
				if(message=="ILLEGALCARS"){
					updateSpan("Password1",message);
				} else {
					$("#Password1Span").empty();
				}
			}
		});
	});
	alertTxt = $("#spanAlertTexts").text();
	alertArray = alertTxt.split("|");
});

function IsEmail(email){
	var isEmail = false;	
	if (email.replace(/([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})/gi,'') == ""){
		isEmail = true;
	}
	return isEmail;
}

function changeCountry() {
	document.getElementById("selectCountry").innerHTML = countrySelectBox;
	$("#country").hide();
}

function checkForm(f) {
	f.SecurityCode.value = f.SecurityCode.value.toUpperCase();
	var LoginMinLength = 6;
	if (f.Login.value.length < LoginMinLength) {
		alert(fixText(alertArray[0])); //login too short
		return false;
	}
	
	if (f.Login.value.length > 30) {
		alert(fixText(alertArray[1])); //login too long
		return false;
	}

	if ((f.Password1.value.length<6) || (f.Password2.value.length<6)) {
		alert(fixText(alertArray[2])); //password too short
		return false;
	}

	if (f.Password1.value != f.Password2.value) {
		alert(fixText(alertArray[3])); //passwords not the same
		return false;
	}

	if (!IsEmail(f.Email.value)) {
		alert(fixText(alertArray[4])); //email error
		return false;
	}
	
	if (f.CountryCode.value.length < 1) {
		alert(fixText(alertArray[5])); //no country code selected
		return false;
	}

	if (f.PrefTeam.value.length < 1 || 1==1) {
		alert(fixText(alertArray[6])); //no team
		return false;
	}

	if (f.chkOnly.checked == false) {
		alert(fixText(alertArray[7])); //one account not checked
		return false;
	}
	
	if (f.chkTerms.checked == false) {
		alert(fixText(alertArray[8])); //not agreed to terms
		return false;
	}
	
	if (f.SecurityCode.value.length < 5) {
		alert(fixText(alertArray[9])); //no security code
		return false;
	}

	return true;
}


function updateSpan(field,errCode) {
	var ErrorText = "";
	switch(errCode) {
		case 'SHORTLOGIN': 
			ErrorText = errorTexts['shortLogin'];
			break;
		case 'LONGLOGIN': 
			ErrorText = errorTexts['longLogin'];
			break;
		case 'LOGINEXISTS': 
			ErrorText = errorTexts['loginExists'];
			break;
		case 'ILLEGALCARS': 
			ErrorText = errorTexts['illegalChars'];
			break;
		case 'SHORTPASS': 
			ErrorText = errorTexts['shortPass'];
			break;
		case 'BADPASS': 
			ErrorText = errorTexts['badPass'];
			break;
		case 'NOPASSMATCH': 
			ErrorText = errorTexts['noPassMatch'];
			break;
		case 'INVALIDEMAIL': 
			ErrorText = errorTexts['invalidEmail'];
			break;
		case 'ILLEGALCARSEMAIL': 
			ErrorText = errorTexts['illegalCharsEmail'];
			break;
		case 'EMAILEXISTS': 
			ErrorText = errorTexts['emailExists'];
			break;
		default:
			ErrorText = errCode;
			break;
	}
	if (ErrorText=='') {
		ErrorText = "<img src='/images/checked-green.gif' alt='V' title='V' />";
	}
	document.getElementById(field + 'Span').innerHTML = "<font size='1' color='red'>" + ErrorText + "</font>"
}


function checkLogin(field) {
	var CheckValue = $("#"+field).val();
	if (CheckValue != '') {
		if (field == 'Login') {
			$.post("/ajax/signup.asp", { login: CheckValue },function(data){
				updateSpan(field,data);
			});
		} else if (field == 'Password1') {
			$.post("/ajax/signup.asp", { password: CheckValue },function(data){
				updateSpan(field,data);
			});
		} else if (field == 'Password2') {
			$.post("/ajax/signup.asp", { password1: $('#Password1').val(), password2: CheckValue },function(data){
				updateSpan(field,data);
			});
		} else if (field == 'Email') {
			$.post("/ajax/signup.asp", { email: CheckValue },function(data){
				updateSpan(field,data);
			});
		}
	}
}