function isemail(str) { return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(str)) }

function hideerror() {
	if (displayTimer != null) {
		clearTimeout(displayTimer);
		displayTimer = null;
	}
	$('#bigerror').hide();		
}

function padme(obj) {
	while(obj.value.length < 12) {
		obj.value = obj.value + ' ';		
	}
}

function validateform() {
	var readygo = true;
	if(!isemail($('#emailaddress').val())) {
		$('#textfield1 span').attr('class','textfieldInvalidShown');readygo=false;
	} else {
		$('#textfield1 span').attr('class','textfieldInvalid');
	}
	if(!isemail($('#emailconfirm').val())) {
		$('#textfield2 span').attr('class','textfieldInvalidShown');readygo=false;
	} else if ($('#emailconfirm').val() != $('#emailaddress').val()) {
		$('#textfield2 span').attr('class','textfieldInvalidShown');readygo=false;
	} else {
		$('#textfield2 span').attr('class','textfieldInvalid');
	}
		
	var tac = jQuery.trim($('#accesscode').val());
	if(tac.length < 8) {
		$('#textfield3 span').attr('class','textfieldInvalidShown');readygo=false;
	} else {
		$('#textfield3 span').attr('class','textfieldInvalid');
	}
	return readygo;
}

function checkcode() {
	if(validateform()){
		var param1 = $("#wpform input").serialize();
		var params = param1 + '&method=ProcessCode&returnformat=json&queryformat=column';
		$('#bigerror').hide();
		$('#btnSend').hide();
		$('#waiting').show();
		$.ajax({ type: 'post', url: 'assets/ajax/wallpaper.cfc', data: params, dataType: 'json', success: processresult, failure:processresult });
	}
}

function processresult(j) {
	
	if(!(j.DATA)) {
		theErrorCode = 99;
	} else {
		theErrorCode = j.DATA.ERRORCODE[0];
	}
	
	var errorString = '';
	switch(theErrorCode) {
		case 0:
			break;
		case 1:
			errorString = 'The email addresses you entered do not match.';
			break;
		case 2:
			errorString = 'The email address you entered is not valid.';
			break;
		case 3:
			errorString = 'The wallpaper access code you entered is invalid.';
			break;
		case 4:
			errorString = 'The platform you requested is not available right now; please try again in a few hours.';
			break;
		case 5:
			errorString = 'The wallpaper code you entered appears to be invalid. (E5)';
			break;
		case 6:
			errorString = 'Wierd - that skin design is not actually available for the device. Please re-check the code.';
			break;
		case 7:
			errorString = 'The wallpaper you requested is not available right now;  please try again in a few hours. (E7)';
			break;
		case 8:
			errorString = 'An error occurred while emailing the wallpaper; please try again later. (E8)';
			break;
		case 9:
			eval(j.DATA.CALLBACK[0]);
			break;
	}
	
	if (theErrorCode != "0") {
		$('#bigerror').html(errorString);
		$('#bigerror').show();
		displayTimer = setTimeout("hideerror()", 15000);
		$('#btnSend').show();
		$('#waiting').hide();
		$('#accesscode').val('');
	} else if (theErrorCode == "9") {
		//game domain
	} else {
		displayTimer = null;
		hideerror();
		$('#instructions').html('<p style="color:#F7A614">Your &quot;' + j.DATA.DESIGNNAME[0] + '&quot; wallpaper for the ' + j.DATA.PLATFORMNAME[0] + ' has been sent to you and should arrive shortly.</p><p style="color:#F7A614">If your wallpaper is not delivered within the next 15 minutes, you may want to check to see if it is mistakenly being held in your spam or junk email folders.</p>');	
		$('#btnSend').show();
		$('#waiting').hide();
		$('#accesscode').val('');
	}

}


