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( _proxy_jslib_handle(this_form.form_contact_name, 'value', '', 0, 0)==''){
		msg+="-Please enter a contact name for this order\n";
	}
	if(!valid_email( _proxy_jslib_handle(this_form.form_contact_email, 'value', '', 0, 0))){
		msg+="-Please enter a contact email address\n";
	}
	if( _proxy_jslib_handle(this_form.form_contact_phone, 'value', '', 0, 0)==''){
		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  _proxy_jslib_handle(ccErrors, (ccErrorNo), 0, 0);
	};
	return ''; // Not very neat, but...
}



//the checkout validation
function checkout_validate(stage,this_form) {
	var msg='';

	if(stage=='card'){
		if( _proxy_jslib_handle(this_form.form_card_type, 'value', '', 0, 0)==''){
			msg+="-Please select a card type\n";
		}
		else if( _proxy_jslib_handle(this_form.form_card_name, 'value', '', 0, 0)==''){
			msg+="-Please enter a valid card name\n";
		}
		if( _proxy_jslib_handle(this_form.form_card_number, 'value', '', 0, 0)==''){
			msg+="-Please enter a valid card number\n";
		}
			
		if( _proxy_jslib_handle(this_form.form_card_cvv, 'value', '', 0, 0)==''){
			msg+="-Please enter a valid card CVV number\n";
		}
		//if the start date and the end date are empty
		if( _proxy_jslib_handle(this_form.form_card_start_date, 'value', '', 0, 0)=='' &&  _proxy_jslib_handle(this_form.form_card_expiry_date, 'value', '', 0, 0)==''){
			msg+="-Please enter a valid start date and / or expiry date\n";
		}
		//if there is a start date check it
		if( _proxy_jslib_handle(this_form.form_card_start_date, 'value', '', 0, 0)!='' && !valid_card_date( _proxy_jslib_handle(this_form.form_card_start_date, 'value', '', 0, 0))){
			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 =  _proxy_jslib_handle(this_form.form_card_start_date, 'value', '', 0, 0);
			
			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( _proxy_jslib_handle(this_form.form_card_expiry_date, 'value', '', 0, 0))){
			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 =  _proxy_jslib_handle(this_form.form_card_expiry_date, 'value', '', 0, 0);
			
			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';
			}
		}


		if(msg!='') {		
			alert(msg);
		}
		else{
			var credit_card_validation = testCreditCard( _proxy_jslib_handle(this_form.form_card_number, 'value', '', 0, 0),  _proxy_jslib_handle(this_form.form_card_type, 'value', '', 0, 0));
			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 yor details are correct, please press OK.");
			}
			if (confirmation) {			
				basket_form_switch('payment');
			}
		}
	}
	else if(stage=='payment'){
		if( _proxy_jslib_handle(this_form.form_payment_address, 'value', '', 0, 0)==''){
			msg+="-Please enter a valid payment address\n";
		}
		if( _proxy_jslib_handle(this_form.form_payment_town_city, 'value', '', 0, 0)==''){
			msg+="-Please enter a valid payment town / city\n";
		}
		if( _proxy_jslib_handle(this_form.form_payment_county_state, 'value', '', 0, 0)==''){
			msg+="-Please enter a valid county / state\n";
		}
		if( _proxy_jslib_handle(this_form.form_payment_country, 'value', '', 0, 0)==''){
			msg+="-Please enter a valid country\n";
		}
		if( _proxy_jslib_handle(this_form.form_payment_postcode, 'value', '', 0, 0)==''){
			msg+="-Please enter a valid form_payment_postcode\n";
		}

		if(msg!=''){
			alert(msg);
		}
		else{
			basket_form_switch('delivery');
		}
	}
	else if(stage=='delivery'){
		if( _proxy_jslib_handle(this_form.form_delivery_address, 'value', '', 0, 0)==''){
			msg+="-Please enter a valid delivery address\n";
		}
		if( _proxy_jslib_handle(this_form.form_delivery_town_city, 'value', '', 0, 0)==''){
			msg+="-Please enter a valid delivery town / city\n";
		}
		if( _proxy_jslib_handle(this_form.form_delivery_county_state, 'value', '', 0, 0)==''){
			msg+="-Please enter a valid county / state\n";
		}
		if( _proxy_jslib_handle(this_form.form_delivery_country, 'value', '', 0, 0)==''){
			msg+="-Please enter a valid country\n";
		}
		if( _proxy_jslib_handle(this_form.form_delivery_postcode, 'value', '', 0, 0)==''){
			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 ( _proxy_jslib_handle(document, 'getElementById', '', 0, 0)){
		// this is the way the standards work
		var style2 =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(which_div).style;
		style2.display = style2.display? "":"block";
	}
	else if (document.all){
		// this is the way old msie versions work
		var style2 =  _proxy_jslib_handle(document.all, (which_div), 0, 0).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 ( _proxy_jslib_handle(document, 'getElementById', '', 0, 0)){
		// this is the way the standards work
		var style2 =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(which_div).style;
		style2.display = "block";
	}
	else if (document.all){
		// this is the way old msie versions work
		var style2 =  _proxy_jslib_handle(document.all, (which_div), 0, 0).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 ( _proxy_jslib_handle(document, 'getElementById', '', 0, 0)){
		// this is the way the standards work
		var style2 =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)(which_div).style;
		style2.display = "";
	}
	else if (document.all){
		// this is the way old msie versions work
		var style2 =  _proxy_jslib_handle(document.all, (which_div), 0, 0).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= _proxy_jslib_handle( _proxy_jslib_handle(this_form.contact_type.options, (this_form.contact_type.selectedIndex), 0, 0), 'value', '', 0, 0);
	}

	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) {
	 _proxy_jslib_assign('', this_form.form_delivery_address, 'value', '=', ( _proxy_jslib_handle(this_form.form_payment_address, 'value', '', 0, 0)));
	 _proxy_jslib_assign('', this_form.form_delivery_town_city, 'value', '=', ( _proxy_jslib_handle(this_form.form_payment_town_city, 'value', '', 0, 0)));
	 _proxy_jslib_assign('', this_form.form_delivery_county_state, 'value', '=', ( _proxy_jslib_handle(this_form.form_payment_county_state, 'value', '', 0, 0)));
	 _proxy_jslib_assign('', this_form.form_delivery_country, 'value', '=', ( _proxy_jslib_handle(this_form.form_payment_country, 'value', '', 0, 0)));
	 _proxy_jslib_assign('', this_form.form_delivery_postcode, 'value', '=', ( _proxy_jslib_handle(this_form.form_payment_postcode, 'value', '', 0, 0)));
}

function product_popup(open_url,image_width,image_height) {
	//alert(image_width+'x'+image_height);
	newwindow= _proxy_jslib_handle(window, 'open', '', 1, 0)(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;
}
 ;
_proxy_jslib_flush_write_buffers() ;