function is_numeric(str){
   var ValidChars="0123456789";
   var IsNumber=true;
   var Char;

	for(i=0;i<str.length && IsNumber==true;i++){
		Char = str.charAt(i);

		if(ValidChars.indexOf(Char)==-1){
			IsNumber = false;
		}
	}

	return IsNumber;
}

function valid_email(str){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	/* var filter=/^.+@.+\..{2,3}$/; */
	if(filter.test(str)){
		return true;
	}

	return false;
}

function valid_card_date(str){
	if(str.length=='4' && is_numeric(str)){
		return true;
	}

	return false;
}

//the checkout confirm validation
function checkout_confirm_validate(this_form){
	var msg='';

	if(this_form.form_contact_name.value==''){
		msg+="-Please enter a contact name for this order\n";
	}
	if(!valid_email(this_form.form_contact_email.value)){
		msg+="-Please enter a contact email address\n";
	}
	if(this_form.form_contact_phone.value==''){
		msg+="-Please enter a contact phone number\n";
	}

	if(msg!=''){
		alert(msg);
	}
	else{
		this_form.submit();
	}
}

function testCreditCard (card_number, card_type) {
	// Taken from http://www.braemoor.co.uk/software/creditcard.shtml
	// Uncomment this line to suspend temporarily:
	// return '';
  if (!checkCreditCard (card_number, card_type)) {
		return ccErrors[ccErrorNo];
	};
	return ''; // Not very neat, but...
}

//the checkout validation
function checkout_validate(stage,this_form,downloads) {

	// If we set downloads to true, we're validating the downloads checkout page

	if(this_form.payment_method.value=='paypal' && downloads) {
		return confirm('Once you have paid via PayPal, you will be sent an email containing a link to your download.\n\nIf you don\'t receive this email, please check your junk mail folder!\n\nYour link will also be displayed on this page when you return from the PayPal website.');
		// No need to validate
	}

	var msg='';

	if(stage=='card'){
		if(this_form.form_card_type.value==''){
			msg+="-Please select a card type\n";
		}
		else if(this_form.form_card_name.value==''){
			msg+="-Please enter a valid card name\n";
		}
		if(this_form.form_card_number.value==''){
			msg+="-Please enter a valid card number\n";
		}

		if(this_form.form_card_cvv.value==''){
			msg+="-Please enter a valid card CVV number\n";
		}
		//if the start date and the end date are empty
		if(this_form.form_card_start_date.value=='' && this_form.form_card_expiry_date.value==''){
			msg+="-Please enter a valid start date and / or expiry date\n";
		}
		//if there is a start date check it
		if(this_form.form_card_start_date.value!='' && !valid_card_date(this_form.form_card_start_date.value)){
			msg+="-Please enter the start date in the correct format MMYY (Only numbers, no other characters)\n";
		}
		else {
			// Check that the start date is in the past:
			var today = new Date();
			var start_date = this_form.form_card_start_date.value;

			var start_date_month = start_date.substr(0,2);
			var start_date_year = start_date.substr(2,2);

			if (start_date_year > 80) {
				// We'll assume that any date higher than 80 refers to the 1900s; this will break near the end of the 21st century ;-)
				start_date_year = parseInt(start_date_year,10) + 1900;
			}
			else {
				start_date_year = parseInt(start_date_year,10) + 2000;
			}

			if (start_date_year > today.getFullYear() || (start_date_year == today.getFullYear() && start_date_month > (today.getMonth() + 1))) {

				msg += 'The start date cannot be in the future';
			}
		}

		//check the expiry date
		if(!valid_card_date(this_form.form_card_expiry_date.value)){
			msg+="-Please enter the expiry date in the correct format MMYY (Only numbers, no other characters)\n";
		}
			else {
			// Check that the expiry date is in the future:
			var today = new Date();
			var expiry_date = this_form.form_card_expiry_date.value;

			var expiry_date_month = expiry_date.substr(0,2);
			var expiry_date_year = expiry_date.substr(2,2);

			if (expiry_date_year > 80) {
				// We'll assume that any date higher than 80 refers to the 1900s; this will break near the end of the 21st century ;-)
				expiry_date_year = parseInt(expiry_date_year,10) + 1900;
			}
			else {
				expiry_date_year = parseInt(expiry_date_year,10) + 2000;
			}

			if (expiry_date_year < today.getFullYear() || (expiry_date_year == today.getFullYear() && expiry_date_month < (today.getMonth() + 1))) {

				msg += 'The expiry date cannot be in the past';
			}
		}

		// Check the email address, if necessary:
		if (downloads) {
			if(!valid_email(this_form.form_contact_email.value)){
				msg+="-Please enter a contact email address\n";
			}
		}

		if(msg!='') {
			alert(msg);
			return false;
		}
		else{
			var credit_card_validation = testCreditCard(this_form.form_card_number.value, this_form.form_card_type.value);
			var confirmation = true;
			if (credit_card_validation) {
				confirmation = confirm("We may not be able to process that card because it appears that: \n\n"+credit_card_validation+"\n\nPlease press Cancel to check your details. In particular, please check that the card type (e.g. Visa) matches your card.\n\nIf you are sure your details are correct, please press OK.");
			}
			if (confirmation) {
				if (downloads) {
					return true;
				}
				else {					
					basket_form_switch('payment');
				}
			}
			else {
				return false;
			}
		}
	}
	else if(stage=='payment'){
		if(this_form.form_payment_address.value==''){
			msg+="-Please enter a valid payment address\n";
		}
		if(this_form.form_payment_town_city.value==''){
			msg+="-Please enter a valid payment town / city\n";
		}
		if(this_form.form_payment_county_state.value==''){
			msg+="-Please enter a valid county / state\n";
		}
		if(this_form.form_payment_country.value==''){
			msg+="-Please enter a valid country\n";
		}
		if(this_form.form_payment_postcode.value==''){
			msg+="-Please enter a valid form_payment_postcode\n";
		}

		if(msg!=''){
			alert(msg);
		}
		else{
			basket_form_switch('delivery');
		}
	}
	else if(stage=='delivery'){
		if(this_form.form_delivery_name.value==''){
			msg+="-Please enter the recipient's name\n";
		}
		if(this_form.form_delivery_address.value==''){
			msg+="-Please enter a valid delivery address\n";
		}
		if(this_form.form_delivery_town_city.value==''){
			msg+="-Please enter a valid delivery town / city\n";
		}
		if(this_form.form_delivery_county_state.value==''){
			msg+="-Please enter a valid county / state\n";
		}
		if(this_form.form_delivery_country.value==''){
			msg+="-Please enter a valid country\n";
		}
		if(this_form.form_delivery_postcode.value==''){
			msg+="-Please enter a valid postcode\n";
		}

		if(msg!=''){
			alert(msg);
		}
		else{
			this_form.submit();
		}
	}

}

//this toggles the div
function toggle_div(which_div){
	//alert(which_div);
	if (document.getElementById){
		// this is the way the standards work
		var style2 = document.getElementById(which_div).style;
		style2.display = style2.display? "":"block";
	}
	else if (document.all){
		// this is the way old msie versions work
		var style2 = document.all[which_div].style;
		style2.display = style2.display? "":"block";
	}
	else if (document.layers){
		// this is the way nn4 works
		var style2 = document.layers[which_div].style;
		style2.display = style2.display? "":"block";
	}
}

function toggle_div_on(which_div){
	//alert(which_div);
	if (document.getElementById){
		// this is the way the standards work
		var style2 = document.getElementById(which_div).style;
		style2.display = "block";
	}
	else if (document.all){
		// this is the way old msie versions work
		var style2 = document.all[which_div].style;
		style2.display = "block";
	}
	else if (document.layers){
		// this is the way nn4 works
		var style2 = document.layers[which_div].style;
		style2.display = "block";
	}
}

function toggle_div_off(which_div){
	//alert(which_div);
	if (document.getElementById){
		// this is the way the standards work
		var style2 = document.getElementById(which_div).style;
		style2.display = "";
	}
	else if (document.all){
		// this is the way old msie versions work
		var style2 = document.all[which_div].style;
		style2.display = "";
	}
	else if (document.layers){
		// this is the way nn4 works
		var style2 = document.layers[which_div].style;
		style2.display = "";
	}
}

function contact_form_switch(this_form,force){
	var viewable_layer;

	if(force!='undefined' && force!=''){
		viewable_layer=force;
	}
	else{
		viewable_layer=this_form.contact_type.options[this_form.contact_type.selectedIndex].value;
	}

	if(viewable_layer=='feedback'){
		toggle_div_on('feedback');
	}
	else{
		toggle_div_off('feedback');
	}
	if(viewable_layer=='press_and_media'){
		toggle_div_on('press_and_media');
	}
	else{
		toggle_div_off('press_and_media');
	}
	if(viewable_layer=='sales'){
		toggle_div_on('sales');
	}
	else{
		toggle_div_off('sales');
	}
	if(viewable_layer=='site_problem'){
		toggle_div_on('site_problem');
	}
	else{
		toggle_div_off('site_problem');
	}
	if(viewable_layer=='wheres_my_stuff'){
		toggle_div_on('wheres_my_stuff');
	}
	else{
		toggle_div_off('wheres_my_stuff');
	}
}

function basket_form_switch(layer){

	if(layer=='delivery'){
		toggle_div_on('delivery_address_subject');
		toggle_div_on('delivery_address');
		toggle_div_off('payment_address_subject');
		toggle_div_off('payment_address');
		toggle_div_off('card_details_subject');
		toggle_div_off('card_details');
	}
	else if(layer=='payment'){
		toggle_div_off('delivery_address_subject');
		toggle_div_off('delivery_address');
		toggle_div_on('payment_address_subject');
		toggle_div_on('payment_address');
		toggle_div_off('card_details_subject');
		toggle_div_off('card_details');
	}
	else if(layer=='card'){
		toggle_div_off('delivery_address_subject');
		toggle_div_off('delivery_address');
		toggle_div_off('payment_address_subject');
		toggle_div_off('payment_address');
		toggle_div_on('card_details_subject');
		toggle_div_on('card_details');
	}
	else{
		toggle_div_off('delivery_address_subject');
		toggle_div_off('delivery_address');
		toggle_div_off('payment_address_subject');
		toggle_div_off('payment_address');
		toggle_div_off('card_details_subject');
		toggle_div_off('card_details');
	}
}

function checkout_address_copy(this_form){
	// Name no longer needed
	// this_form.form_delivery_name.value=this_form.form_card_name.value;
	this_form.form_delivery_address.value=this_form.form_payment_address.value;
	this_form.form_delivery_town_city.value=this_form.form_payment_town_city.value;
	this_form.form_delivery_county_state.value=this_form.form_payment_county_state.value;
	this_form.form_delivery_country.value=this_form.form_payment_country.value;
	this_form.form_delivery_postcode.value=this_form.form_payment_postcode.value;
}

function product_popup(open_url,image_width,image_height){
	//alert(image_width+'x'+image_height);
	newwindow=window.open(open_url,'posingproductions','height='+image_height+',width='+image_width+',toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
	if(window.focus){
		newwindow.focus();
	}
	return false;
}



