var bFocused=false;

function ChangeFocus(focused)
{
	bFocused=focused;
}

function SubmitContact()
{
	if(VerifyContact())
		document.form_contact.submit();
}

function VerifyContact()
{
	if(bFocused==false)
	{
		return false;
	}
	if(document.form_contact.namecl.value=="")
	{
		alert("Please insert your name");
		document.form_contact.namecl.focus();
		return false;
	}
	if(document.form_contact.email.value=="")
	{
		alert("Please insert your e-mail");
		document.form_contact.email.focus();
		return false;
	}
	if(document.form_contact.phone.value=="")
	{
		alert("Please insert your phone");
		document.form_contact.phone.focus();
		return false;
	}
	if(document.form_contact.noblinds.value=="")
	{
		alert("Please insert your blinds number");
		document.form_contact.noblinds.focus();
		return false;
	}
	if(document.form_contact.comments_text.value=="")
	{
		alert("Please insert your message");
		document.form_contact.comments_text.focus();
		return false;
	}
	
	return true;
}

function SelectAction(idx){
	switch(idx){
		case 0: $('no_order').style.display = 'none';
				$('with_order').style.display = 'none';
				break;
		case 1: $('no_order').style.display = 'block';
				$('with_order').style.display = 'none';
				break;
		case 2: $('no_order').style.display = 'none';
				$('with_order').style.display = 'block';

				// if logged in as trade customer auto populate fields
				new Ajax.Request('json_orders.php', {
					  method:'post',
					  parameters:{'get_trade_customer':true},
					  onCreate:function(){
						$('pls_wait').style.display = 'block';
					  },
					  onComplete: function(transport){
						  $('pls_wait').style.display = 'none';
						  if ( transport.responseText ) {
							  responseJson = transport.responseText.evalJSON();
							  $('surname').value = responseJson['surname'];
							  $('postal').value = responseJson['postcode'];
							  $('already_order').length = 0;
							  if(responseJson['orders'].length > 0){
								  this.order = $('already_order');
								  responseJson['orders'].each(function(el, index){
									  this.order.options[this.order.options.length] = new Option(el.id +' placed on '+el.order_date, el.ordercode);
									});
									$('hbutton').style.display = 'block';
							  }
						  }
					  }
					});				

				break;
	}
}

function VerifyContact2(){
	if ($('surname').value == ""){
		alert("Please insert the surname on the card used for payment!");
		$('surname').focus();
		return false;
	}
	if ($('postal').value == ""){
		alert("Please insert the delivery postal code!");
		$('postal').focus();
		return false;
	}
	if ($('already_order').value == ""){
		alert("Please select order!");
		$('already_order').focus();
		return false;
	}
	return true;
}

function SubmitContact2(){
	if($('already_order').value != ""){
		window.location = 'https://www.blindsuk.net/cart/redirect_conv.php?oc='+$('already_order').value;
		return;
	}
	if(VerifyContact2()){
		window.location = 'https://www.blindsuk.net/cart/redirect_conv.php?name='+$('surname').value+'&postal='+$('postal').value;
	}
}

function OptionLoader(){
	SelectAction($('opt').selectedIndex);
}
