jQuery(document).ready(function(){

	var $html = jQuery('#billing-postcode-container').html();
	$html = '<div id="billing-postcode-container">'+$html+'</div>';
	var $i_html = '<input type="hidden" name="address_postcode" id="address_postcode" value="Ireland" />';

	
	var $dhtml = jQuery('#delivery-postcode-container').html();
	$dhtml = '<div id="delivery-postcode-container">'+$dhtml+'</div>';
	var $di_html = '<input type="hidden" name="address_postcode_del" id="address_postcode_del" value="Ireland" />';

	
function controlBillingPostcode($value){
		jQuery('#billing-postcode-container').remove();
	if($value!='Ireland'){
		if(jQuery('#address_postcode')) jQuery('#address_postcode').remove();
	jQuery('#country').after($html);
	} else {
	jQuery('#country').after($i_html);	
	}
}


function controlDeliveryPostcode($value){
		if(jQuery('#delivery-postcode-container')) jQuery('#delivery-postcode-container').remove();
	if($value !='Ireland'){
		if(jQuery('#address_postcode_del')) jQuery('#address_postcode_del').remove();
	jQuery('#delivery-country').after($dhtml);
	} else {
	jQuery('#delivery-country').after($di_html);	
	}
	jQuery('#address_country_del').val($value);
}



var $mtm_fields = jQuery('input.text','form.item');
var $values = Array();
$mtm_fields.each(function(){

	$values.push(jQuery(this).val())
	var $index = jQuery($mtm_fields).index(jQuery(this));
	
jQuery(this).click(function(){
	if(jQuery(this).val()==$values[$index]){
		jQuery(this).val('')
	jQuery(this).css({'color':'#000'})
		
		}
})
jQuery(this).blur(function(){
	if(jQuery(this).val()==''){ 
		jQuery(this).val($values[$index])
		jQuery(this).css({'color':'#999'})
		}
	

})
})

jQuery('a','form.item').click(function(){
	var $not_complete=0;
	var $too_big=0;
	var $not_numeric = 0;
$mtm_fields.each(function(){
		var $index = jQuery($mtm_fields).index(jQuery(this));
	if(jQuery(this).val()==$values[$index] || jQuery(this).val()=='') $not_complete++;
	if(isNaN(jQuery(this).val())) $not_numeric++;
	if(!isNaN(jQuery(this).val()) && jQuery(this).val() > 900) $too_big++;
})
if($not_complete){
	alert('Please complete required width and height in millimetres');
	$not_numeric=0;
	$too_big=0
}
if($not_numeric){
	alert('Please use only numeric values for sizes');
	$not_complete=0;
	$too_big=0
}
if($too_big){
	alert('Width or height cannot exceed 900mm in size');
	$not_complete=0;
	$not_numeric=0;
}
if(!$not_complete && !$not_numeric && !$too_big) jQuery('form.item').submit();
	return false;
})



controlBillingPostcode(jQuery('#address_country').val());
controlDeliveryPostcode(jQuery('#address_country_del').val());

jQuery('#address_country').change(function(){
   controlBillingPostcode(jQuery(this).val());
})


jQuery('#address_country_del').change(function(){
 controlDeliveryPostcode(jQuery(this).val());
})


jQuery('#address_delivery').click(function(){

	var $fields = Array('country','postcode','company','address1','address2','address3','towncity','county');
	
	if(jQuery(this).is(':checked')){

		for(var $i=0;$i<$fields.length;$i++){
			var $val = jQuery('#address_'+$fields[$i]).val();
		
		jQuery('#address_'+$fields[$i]+'_del').val($val);
	
		}
	} else {
			for(var $i=0;$i<$fields.length;$i++){
			jQuery('#address_'+$fields[$i]+'_del').val('');
		}
		 
	}
	controlDeliveryPostcode(jQuery('#address_country').val());
	jQuery('#address_postcode_del').val(jQuery('#address_postcode').val())
})







})
