//JJ's Ajax form enhancements. Feb-2009
var EMAIL_VALIDATION_STATE = 0;

function checkRegiForm()
{	
	var errormessage = document.getElementById('errormessage').firstChild;
	
	//check username is not empty
	var username = document.getElementById('username');
	if (username.value.length < 1){
		$('#username_error').text('Please enter a valid username.');
		$('#username_error').css("display", 'block');
		username.parentNode.className = 'highlight';
		username.focus();
		return false;	
	}
	
	//check if username contains non-alphanumeric characters
	//"!", "&", "`","~","#","%","$","^","*","(",")","+","=","[","{","]","}","|",";",":",">",",","?","'","<","\\","/","\"",
	var alaphaNumericCheck = new RegExp("[^a-zA-Z0-9_@.]");
	var alphaNumCheckResult = alaphaNumericCheck.exec(username.value);
	if (alphaNumCheckResult != null){
		$('#username_error').text('Allowed characters are numbers, letters, underscore, @, and .');
		$('#username_error').css("display", 'block');
		username.parentNode.className = 'highlight';
		username.focus();
		return false;
	}
	
	//check if username contains words that are blocked
    var badNames = new Array();
    badNames = ["fuck","close","auction","cancel","prick","bidz","admin","listing","access","stop","bitch",
       "vagina","penis","bastard","bloody","jesus","arse","balls","bollocks","cock","crap","cunt","fart","knob",
       "prick","pussy","shit","tits","twat","nutter","retard","schizo","spastic","battyboy","dyke","faggot","poof",
       "queer","blaad","bumbu","chichi","hoockie","punani","bastard","bitch","dickhead","slag","slut","wanker",
       "whore","bonk","bugger","poke","shag","chink","kyke","kike","nigger","paki","papist","pikey","yid","guest_account"];
    for (var i = 0;i<badNames.length;i++){
    	if (username.value.indexOf(badNames[i]) > -1){
			$('#username_error').text('Your username contains a word or expression that is not allowed as username on our website. Please use an alternate username.');
			$('#username_error').css("display", 'block');
			username.parentNode.className = 'highlight';
			username.focus();
    		return false;
		}
    }
	
	$('#username_error').text('');
	$('#username_error').css("display", 'none');
	username.parentNode.className = 'valid';
    
	//check password fields are not empty and are the same
	var password = document.getElementById('password');
	var passwordConfirm = document.getElementById('passwordConfirm');
	if (password.value.length < 1){		
		$('#password_error').text('Please enter a valid password.');
		$('#password_error').css("display", 'block');
		password.parentNode.className = 'highlight';
		password.focus();
		return false;
	}
	
	//check if password was entered but confirm password was not.
	if (password.value.length > 0 && passwordConfirm.length < 1){
		$('#passwordConfirm_error').text('You must confirm your password.');
		$('#passwordConfirm_error').css("display", 'block');
		passwordConfirm.parentNode.className = 'highlight';
		passwordConfirm.focus();
		return false;
	}
	
	//check that password and confirmPassword are the same
	if (password.value != passwordConfirm.value){
		$('#passwordConfirm_error').text('Your passwords do not match.');
		$('#passwordConfirm_error').css("display", 'block');
		passwordConfirm.parentNode.className = 'highlight';
		passwordConfirm.focus();
		return false;
	}
	
	$('#password_error').text('');
	$('#password_error').css("display", 'none');
	password.parentNode.className = 'valid';
	$('#passwordConfirm_error').text('');
	$('#passwordConfirm_error').css("display", 'none');
	passwordConfirm.parentNode.className = 'valid';
	
	//check email is not empty
	var email = document.getElementById('email');
	if (email.value.length < 1 ){
		$('#email_error').text('Please enter a valid e-mail address.');
		$('#email_error').css("display", 'block');
		email.parentNode.className = 'highlight';
		email.focus();
		return false;
	}
	
	//check email is in correct format
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if (reg.test(email.value) == false){
		$('#email_error').text('Please enter a valid e-mail address.');
		$('#email_error').css("display", 'block');
		email.parentNode.className = 'highlight';
		email.focus();
		return false;
	}
	
	$('#email_error').find("text:first").text('');
	$('#email_error').css("display", 'none');
	password.parentNode.className = 'valid';
	
	//check captcha code is not empty
	var captchaCode = document.getElementById('enteredCaptchaCode');
	if (captchaCode.value.length < 1){		
		$('#captcha_error').text('Please enter the code shown on the picture below.');
		$('#captcha_error').css("display", 'block');
		captchaCode.parentNode.className = 'highlight';
		captchaCode.focus();
		return false;
	}
	
	//check terms and conditions is checked
	var agreed = document.getElementById('termsConditionsAgreeCheckbox');
	if (agreed.checked == false){
		errormessage.nodeValue = 'You must read and agree to the site terms and conditions before registering.';
		agreed.focus();
		return false;
	}
}//END: checkRegiForm

function processCheckUsernameAvailabilityResponse(data, textStatus, XMLHttpRequest) {
	var error = $('#username_error');		
	var usernameElement = $('#username');
	if( error && usernameElement )
	{
		if( data != "success")
		{
			error.text(data);
			error.css("display", 'block');
			usernameElement.parent().attr('class', 'highlight');
		}
		else
		{
			error.text("");
			error.css("display", 'none');
			usernameElement.parent().attr('class', 'valid');
		}
	}
}

function processCheckUsernameAvailabilityError(XMLHttpRequest, textStatus, errorThrown) {
	var error = $('#username_error');
	var usernameElement = $('#username');
	if( error && usernameElement )
	{
		error.text("");
		error.css("display", 'none');
		usernameElement.parent().attr('class', 'valid');
	}
	
}
function checkUsernameAvailability(element,tid,sid)
{
	if (element.val().length > 0){
		var randomnumber=Math.floor(Math.random()*1001)
		var final_url = "\/bzJApp\/VerifyUserNameAction.action?tid="+tid+"&sid="+sid+"&username="+element.val().toLowerCase();+"&pageload="+randomnumber;
		
		jQuery.ajax({
		  url: final_url,
		  async: true,
		  success: processCheckUsernameAvailabilityResponse,
		  error : processCheckUsernameAvailabilityError
		 });
	}
	else
	{
		var error = $('#username_error');
		error.text("");
		error.css("display", 'none');
		element.parent().attr('class', 'valid');
	}
}

//Form fields onBlur functions

function validateUsername(element,checkUserNameAvailability,tid,sid){
	var error = $('#username_error');
	
	if (element.val().length < 1){
		error.text('');
		error.css("display", 'none');
		element.parent().attr('class', '');
		return false;
	}
	
	var alaphaNumericCheck = new RegExp("[^a-zA-Z0-9_@.]");
	var alphaNumCheckResult = alaphaNumericCheck.exec(element.val());
	if (alphaNumCheckResult != null){
		error.text('Allowed characters are numbers, letters, underscore, @, and .');
		error.css("display", 'block');
		element.parent().attr('class', 'highlight');
		return false;
	}
	
	var badNames = new Array();
    badNames = ["fuck","close","auction","cancel","prick","bidz","admin","listing","access","stop","bitch",
       "vagina","penis","bastard","bloody","jesus","arse","balls","bollocks","cock","crap","cunt","fart","knob",
       "prick","pussy","shit","tits","twat","nutter","retard","schizo","spastic","battyboy","dyke","faggot","poof",
       "queer","blaad","bumbu","chichi","hoockie","punani","bastard","bitch","dickhead","slag","slut","wanker",
       "whore","bonk","bugger","poke","shag","chink","kyke","kike","nigger","paki","papist","pikey","yid","guest_account"];
    for (var i = 0;i<badNames.length;i++){
    	if (element.val().indexOf(badNames[i])>-1){
    		error.text('Your username contains a word that is not allowed as username on our website. Please use an alternate username.');
			error.css("display", 'block');
			element.parent().attr('class', 'highlight');
    		return false;
		}
    }
	
	
	if( checkUserNameAvailability == "Y" )
	{
		checkUsernameAvailability(element,tid,sid);
	}
	else
	{
		error.text('');
		error.css("display", 'none');
		element.parent().attr('class', 'valid');
	}
	return true;
}

function validatePassword(element){
	var error = document.getElementById('password_error');
	
	if (element.val().length < 1 || $('#passwordConfirm').val().length < 1){
		error.firstChild.nodeValue = '';
		error.style.display = 'none';
		element.parent().attr('class', '');
		$('#passwordConfirm_error').text('');
		$('#passwordConfirm_error').css("display", 'none');
		$('#passwordConfirm').parent().attr('class', '');
		return false;
	}
	
	if ($('#passwordConfirm').val().length > 0 && element.val().length > 0 && element.val() != $('#passwordConfirm').val()){
		$('#passwordConfirm_error').text('Your passwords do not match.');
		$('#passwordConfirm_error').css("display", 'block');
		$('#passwordConfirm').parent().attr('class', 'highlight');
    	return false;
	}
	
	error.firstChild.nodeValue = '';
	error.css("display", 'none');
	element.parent().attr('class', 'valid');
	$('#passwordConfirm_error').text('');
	$('#passwordConfirm_error').css("display", 'none');
	$('#passwordConfirm').parent().attr('class', 'valid');
	return true;
}

function validateConfirmPassword(element){
	var error = document.getElementById('passwordConfirm_error');
	
	if (element.val().length < 1 || $('#password').val().length < 1){
		error.firstChild.nodeValue = '';
		error.style.display = 'none';
		element.parent().attr('class', '');
		$('#password_error').text('');
		$('#password_error').css("display", 'none');
		$('#password').parent().attr('class', '');
		return false;
	}
	
	if (element.val().length > 0 && $('#password').val().length > 0 && element.val() != $('#password').val()){
		error.firstChild.nodeValue = 'Your passwords do not match.';
		error.style.display = 'block';
		element.parent().attr('class', 'highlight');
    	return false;
	}
	
	error.firstChild.nodeValue = '';
	error.style.display = 'none';
	element.parent().attr('class', 'valid');
	$('#password_error').text('');
	$('#password_error').css("display", 'none');
	$('#password').parent().attr('class', 'valid');
	return true;
}

function ajaxValidateEmail(element)
{
	var stripped = element.val();
	element.val(stripped.replace("/\\s*/g", ""));
	var urlPart = "emailAddress=" + element.val();
	$.ajax(
	{	url: "/bzJApp/VerifyEmailAction.action?"+urlPart, 
		context: this,
		success: this.validateEmail, 
		error : this.processFailure, 
		complete : this.processComplete
	});	
}

function validateEmail (data, textStatus, XMLHttpRequest) {
	var error = document.getElementById('email_error');
	error.innerHTML = '.';
	var element = document.getElementById('email');
	
	if (element.value.length < 1){
		error.firstChild.nodeValue = '';
		error.style.display = 'none';
		element.parentNode.className = '';
		return false;
	}
	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if (reg.test(element.value) == false){
		error.firstChild.nodeValue = 'Please enter a valid e-mail address.';
		error.style.display = 'block';
		element.parentNode.className = 'highlight';
    	return false;
	}
	
	if(data == 1){
		error.innerHTML = 'Please enter a valid e-mail address. <input style="display: none;" type="hidden" name="emailValid" value="false" />';
		error.style.display = 'block';
		element.parentNode.className = 'highlight';
		return false;
	} else {
		error.innerHTML = '<input style="display: none;" type="hidden" name="emailValid" value="true" />';	
	}
	
	error.firstChild.nodeValue = '';
	error.style.display = 'none';
	element.parentNode.className = 'valid';
	return true;	
}

function processFailure(XMLHttpRequest, textStatus, errorThrown) {
	logToWindow("verification - failed. " + textStatus);
	if (errorThrown != null)
	{
		processException(XMLHttpRequest, errorThrown);
	}
}//END: processFailure

function processComplete(XMLHttpRequest, textStatus) {
	logToWindow("verification - complete.");
}//END: processComplete

function processException(Req, Ex1) {
	logToWindow("verification - execption " + Ex1);
}//END: processException

function validateCaptcha(element){
	var error = document.getElementById('captcha_error');
	
	if (element.val().length < 1){
		error.firstChild.nodeValue = '';
		error.style.display = 'none';
		element.attr('class', '');
		return false;
	}
	
	//error.text('');
	//error.style.display = 'none';
	//element.parentNode.className = 'valid';
	return true;
}

//Checkbox functions

function showSubscribeConfirm(){
	if(document.getElementById('newsletterCheckbox').checked){
		document.getElementById('subscribe_confirm').style.display = 'block';
	} else {
		document.getElementById('subscribe_confirm').style.display = 'none';
	}
}

function showAgreement() {
	if(document.getElementById('bidz_agreement')){
		document.getElementById('bidz_agreement').style.display = '';
	}
}
