// JavaScript Document

function changedate()
{
if (document.returns1.rday.value.length  == 2 && document.returns1.rmonth.value.length  == 2 && document.returns1.ryear.value.length  == 4){
	var returnByDate=new Date();
	returnByDate.setFullYear(document.returns1.ryear.value,document.returns1.rmonth.value-1,document.returns1.rday.value);
	returnByDate.setDate(returnByDate.getDate()+10)
	var month = returnByDate.getMonth()+1;
	document.returns1.dateby.value = returnByDate.getDate() + "/" + month + "/" + returnByDate.getFullYear();
	//info.innerHTML = '&nbsp;';
	elem = document.getElementById("info");
 	elem.innerHTML = '&nbsp;';
	document.returns1.submit.disabled=false;
	
	var today = new Date();
	if (returnByDate<today){
	  document.returns1.submit.disabled=true;
	  //info.innerHTML = 'Your order can no longer be returned as you have had it for more than seven days.';
	  elem.innerHTML = 'Sorry! Your order can no longer be returned as you have had it for more than seven working days.';
	}
}
else{
	document.returns1.submit.disabled=false
	document.returns1.dateby.value = "";
	//info.innerHTML = '&nbsp;';
	elem = document.getElementById("info");
 	elem.innerHTML = '&nbsp;';	
}
} 

function validate_required(field,alerttxt)
{
	with (field)
	{
		if (value==null||value=="")
		  {alert(alerttxt);return false;}
		else {return true;}
	}
}

function validate_form(thisform)
{
	with (thisform)
	{
		if (validate_required(firstname,"Please enter your first name")==false)
		  {firstname.focus();return false;}
		  
		if (validate_required(lastname,"Please enter your last name")==false)				
		  {lastname.focus();return false;}
		  
		if (validate_required(postcode,"Please enter your postcode")==false)
		  {postcode.focus();return false;}
		  
		if (validate_required(polordernum,"Please enter the first 4 digits of your order number")==false)
		  {polordernum.focus();return false;}		  		  
		  
		if (validate_required(oday,"Invalid day. Please enter the date of your order. Using the format DD/MM/YYYY")==false)
		  {oday.focus();return false;}
		  
		if (oday.value.length != 2)
		  {alert("Invalid day. Please enter the date of your order. Using the format DD/MM/YYYY");oday.focus();return false;}
		  
		if (oday.value > 31)
		  {alert("Invalid day. Please enter a valid day on which you made your order.");oday.focus();return false;}		  
		  
		if (validate_required(omonth,"Invalid month. Please enter the date of your order. Using the format DD/MM/YYYY")==false)
		  {omonth.focus();return false;}
		  
		if (omonth.value.length != 2)
		  {alert("Invalid month. Please enter the date of your order. Using the format DD/MM/YYYY");oday.focus();return false;}
		  
		if (omonth.value > 12)
		  {alert("Invalid month. Please enter a valid month on which you made your order.");oday.focus();return false;}		  
		  
		if (validate_required(oyear,"Invalid year. Please enter the date of your order. Using the format DD/MM/YYYY")==false)
		  {oyear.focus();return false;}
		  
		if (oyear.value.length != 4)
		  {alert("Invalid year. Please enter the date of your order. Using the format DD/MM/YYYY");oday.focus();return false;}		    		  
		    
		if (validate_required(rday,"Invalid day. Please enter the date you received your order. Using the format DD/MM/YYYY")==false)
		  {oday.focus();return false;}
		  
		if (rday.value.length != 2)
		  {alert("Invalid day. Please enter the date you received your order. Using the format DD/MM/YYYY");oday.focus();return false;}
		  
		if (rday.value > 31)
		  {alert("Invalid day. Please enter a valid day on which you received your order.");oday.focus();return false;}		  
		  
		if (validate_required(rmonth,"Invalid month. Please enter the date you received your order. Using the format DD/MM/YYYY")==false)
		  {rmonth.focus();return false;}
		  
		if (rmonth.value.length != 2)
		  {alert("Invalid month. Please enter the date you received your order. Using the format DD/MM/YYYY");oday.focus();return false;}
		  
		if (rmonth.value > 12)
		  {alert("Invalid month. Please enter a valid month on which you received your order.");oday.focus();return false;}		  
		  
		if (validate_required(ryear,"Invalid year. Please enter the date you received your order. Using the format DD/MM/YYYY")==false)
		  {oyear.focus();return false;}
		  
		if (ryear.value.length != 4)
		  {alert("Invalid year. Please enter the date you received your order. Using the format DD/MM/YYYY");oday.focus();return false;}

		var orderedDate=new Date();
		orderedDate.setFullYear(oyear.value,omonth.value-1,document.returns1.oday.value);		  
		  
		var receivedDate=new Date();
		receivedDate.setFullYear(ryear.value,rmonth.value-1,document.returns1.rday.value);
		
		var today = new Date();
		
		if (orderedDate > today)
		  {alert("Invalid order date. The order date is in the future. Please re-enter");oday.focus();return false;}
		  
		if (receivedDate > today)
		  {alert("Invalid order received date. The order received date is in the future. Please re-enter");rday.focus();return false;}
		  
		if (receivedDate < orderedDate)
		  {alert("The order received date is before the ordered date. Please re-enter");oday.focus();return false;}		  		  		
						  		  		  		  		  		  		  
	}
}

function validate_radio(field)
{
	with (field)
	{
		var radio_choice = false;
		for (counter = 0; counter < field.length; counter++)
		{
			if (field[counter].checked)
			radio_choice = true; 
		}
		
		if (radio_choice==false)
		  {return false;}
		else {return true;}		
	}
}

function validate_products(prod,qty,reason,alerttxt)
{
		if (prod.value==null||prod.value=="")
		  {return true;}
		else
		  {		  
		  	if(qty.value==null||qty.value=="" || validate_radio(reason)==false){
				alert(alerttxt);		  
		  		return false;
			}
			else {return true;}
		  }
}

function validate_form_step2(thisform)
{
	with (thisform)
	{
		if (validate_required(prod1,"Please enter at least one product. No product code entered.")==false)
		  {prod1.focus();return false;}
		  
		if (validate_required(qty1,"Please enter at least one product. No product quantity entered.")==false)				
		  {qty1.focus();return false;}
		  
		if (validate_radio(reason1)==false)				
		  {alert("Please enter at least one product. No reason entered.");return false;}  

		if (validate_products(prod2,qty2,reason2,"You have entered a product code for product 2, but the rest of the data is incomplete.")==false)				
		  {prod2.focus();return false;}
		  
		if (validate_products(prod3,qty3,reason3,"You have entered a product code for product 3, but the rest of the data is incomplete.")==false)				
		  {prod3.focus();return false;}
		  
		if (validate_products(prod4,qty4,reason4,"You have entered a product code for product 4, but the rest of the data is incomplete.")==false)				
		  {prod4.focus();return false;}
		  
		if (validate_products(prod5,qty5,reason5,"You have entered a product code for product 5, but the rest of the data is incomplete.")==false)				
		  {prod5.focus();return false;}
		  
		if (validate_products(prod6,qty6,reason6,"You have entered a product code for product 6, but the rest of the data is incomplete.")==false)				
		  {prod6.focus();return false;}		  
		  
		if (validate_products(prod7,qty7,reason7,"You have entered a product code for product 7, but the rest of the data is incomplete.")==false)				
		  {prod7.focus();return false;}		  
		  
		if (validate_products(prod8,qty8,reason8,"You have entered a product code for product 8, but the rest of the data is incomplete.")==false)				
		  {prod8.focus();return false;}		
		  
		if (validate_products(prod9,qty9,reason9,"You have entered a product code for product 9, but the rest of the data is incomplete.")==false)				
		  {prod9.focus();return false;}
		  
		if (validate_products(prod10,qty10,reason10,"You have entered a product code for product 10, but the rest of the data is incomplete.")==false)				
		  {prod10.focus();return false;}		  		    		  		  		  		  
	}
}	
