/*
	Website scripts
	(c) Kerve Creative
*/

function openWindow(address,winurl,winwidth,winheight) {
	var new_window = window.open(address,winurl,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=' + winwidth + ',height=' + winheight);
}

function popup_centre(popupwidth,popupheight) {
	/* When calling this function supply the width and height of the popup */
	var horizontal = ((screen.availWidth / 2) - (popupwidth / 2));
	var vertical = ((screen.availHeight / 2) - (popupheight / 2));
	window.moveTo(horizontal,vertical);
}

//Signup form validation script
function validateSignup(formData, jqForm, options) {
	var errormessage = 'Please ensure that you have completed the following:\n';
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var form = jqForm[0];
	
	if(
		(form.email.value != '') &&
		(form.email.value != 'Your email') &&
		(filter.test(form.email.value))
	) {
		//document.signup_form.submit();
	} else {
		if((form.email.value == '') || (form.email.value == 'Your email')) {
			errormessage += "  > Your email address\n";
		} else {
			if((filter.test(form.email.value)) == false) {
				errormessage += "  > Your email address must be a valid\n";
			}
		}
		
		alert(errormessage);
		return false;
	}
}

//Contact form validation script
function validateContact(formData, jqForm, options) {
	var errormessage = 'Please ensure that you have completed the following:\n';
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var form = jqForm[0];
	
	if(
		(form.contact_message.value != '') &&
		(form.contact_name.value != '') &&
		(form.contact_email.value != '') &&
		(filter.test(form.contact_email.value))
	) {
		//document.signup_form.submit();
	} else {
		if(form.contact_name.value == '') {
			errormessage += "  > Your name\n";
		}
		if(form.contact_email.value == '') {
			errormessage += "  > Your email address\n";
		} else {
			if((filter.test(form.contact_email.value)) == false) {
				errormessage += "  > Your email address must be a valid\n";
			}
		}
		if(form.contact_message.value == '') {
			errormessage += "  > Your message\n";
		}
		
		alert(errormessage);
		return false;
	}
}

// prepare the form when the DOM is ready 
$(document).ready(function() { 
    //Signup Form
	var options1 = {
		beforeSubmit:	validateSignup,
        success:		function(responseText,statusText) { 
			alert(responseText); 
    	},
		resetForm:		true
    };
    $('#signup').ajaxForm(options1);
	
	//Contact Form
	var options2 = {
		beforeSubmit:	validateContact,
        success:		function(responseText,statusText) { 
			alert(responseText); 
    	},
		resetForm:		true
    };
    $('#contact_form').ajaxForm(options2);
});

function gotoRange(location) {
	if(location != "") {
		self.location = '/?category=' + location;
	}
}
