// JavaScript Document
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));
}

function isEmail(emailid) 
{
  var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;  

   if(reg.test(emailid) == false) 
   {     
      return false;
   }
   else
   {
	  return true;
   }

}
function isPhone(phone)
{

	var reg = /^[0-9-. +]+$/;

	if(reg.test(phone))
	{
		return true;
	}
	else
	{
		return false;
	}
}



function validateForm()
{
	var qty_array = Array('eye_fillet_qty','hamburgers_pack6_qty',
'porter_house_qty','veal_ettes_qty',
'scotch_fillet_qty','veal_casserole_qty',
'premium_mince_qty','rump_qty',
'tbone_qty','sausage_mince_qty',
'bbq_steak_qty','osso_bucco_qty',
'veal_cutlets_qty','diced_veal_cubes_qty',
'oyster_blade_qty','veal_sausages_qty',
'eye_round_cuts_qty','sweet_chilli_qty',
'veal_steaks_qty','herb_garlic_qty',
'veal_roast_qty','tomato_bacon_qty',
'topside_roast_qty','smoky_bacon_qty',
'marinated_qty','veal_kebabs_qty',
'marinated_honey_qty','honey_soy_twists_qty',
'garlic_roast_qty','bbq_pack_1_qty',
'plain_roast_qty','bbq_pack_2_qty',
'jerollo_roast_qty','bbq_pack_3_qty',
'veal_brisket_qty','hot_pies_qty',
'veal_schnitzel_qty','cold_pies_qty',
'red_wine_qty','hot_pasties_qty',
'veal_mignon_qty','cold_pasties_qty',
'veal_olives_qty','sausage_rolls_qty',
'stir_fry_qty','stir_fry_marinated_qty',
'veal_racks_qty','gorello_qty','flavored_sausages_qty','pickled_veal_qty','diced_veal_qty','marinated_cubes_qty','silverside_qty','soup_bones_qty','marinated_roasts_qty','marinated_schnitzel_qty','veal_pies_qty','veal_sausage_rolls_qty','veal_pasties_qty','bbq_packs_qty','veal_twists_qty','veal_kievs_qty','fillet_mignon_qty','veal_hamburgers_qty','vealettes_qty','veal_meatloaf_qty','plain_sausages_qty','char_grill_rump_qty','tails_qty');
	
	var qty_array_flag = false;
	var why1 = "";

	if(trim(document.getElementById('nameTxt').value)==''){
	
		why1+='"Name" is required <br/>';
	}
	if(isPhone(document.getElementById('phoneTxt').value)=="")
	{
		why1+='"Phone" is required <br/>';
	}
	if(isEmail(document.getElementById('emailTxt').value)=="")
	{
		why1+='"Email" is required <br/>';
	}
	if(trim(document.getElementById('cityTxt').value)==''){
	
		why1+='"City" is required <br/>';
	}
	if(trim(document.getElementById('postcodeTxt').value)==''){
	
		why1+='"Postcode" is required <br/>';
	}
	if(trim(document.getElementById('phoneTxt').value)==''){
	
		why1+='"Phone" is required <br/>';
	}
	if(trim(document.getElementById('addressTxt').value)==''){
	
		why1+='"Address" is required <br/>';
	}
	
	
	for(var j=0; j<qty_array.length; j++){
		if(trim(document.getElementById(qty_array[j]).value)!='' && trim(document.getElementById(qty_array[j]).value)!=0){		
		qty_array_flag= true
		}
		
		}
		if(qty_array_flag==false){
			why1+='"Enter quantity for atleast one product"<br/>';
		}
	//alert(why1);
	//return false;
	

			//return false;
			
	if(why1!="")
	{
		showDialog('Order Form',why1,'warning');
		return false;
	}
	else
	{
		return true;
		/*
		var name	 		= 	document.getElementById('nameTxt').value;
		
		var phone			=	document.getElementById('phoneTxt').value;
		
		var email		 	= 	document.getElementById('emailTxt').value;
		
		var city		 	= 	document.getElementById('cityTxt').value;
				
		var postcode		= 	document.getElementById('postcodeTxt').value;
		
		var address		 	= 	document.getElementById('addressTxt').value;
		
		var state		= 	document.getElementById('stateTxt').value;
		
		qty_str ='';
for(var q=0; q<qty_array.length; q++){
			if(trim(document.getElementById(qty_array[q]).value)==''){
				qty_str = qty_str + "&"+qty_array[q]+"=0";
			}
			else
			{
				qty_str = qty_str + "&"+qty_array[q]+"="+parseFloat(document.getElementById(qty_array[q]).value);
			}
			
			}
		
		
		
		var html = $.ajax({
				type	: 	"GET",
				url		: 	"email.php",
				data	:	"name="+name+"&email="+email+"&phone="+phone+"&city="+city+"&postcode="+postcode+"&address="+address+"&state="+state+qty_str,
				async	: 	false
			}).responseText;
	alert(html);
		if(html == 1)
		{
			//document.contactus.name.value = '';

			//document.contactus.email.value= '';
			
			//document.contactus.comments.value= '';
		
			showDialog('Contact Us',"Email sent successfully",'success');
			return false;

		}
		else
		{
			showDialog('Contact Us',"Email could not send",'warning');
			return false;
		}


	*/}
 
}
function onchange_qty(comp){
	var id = comp.id;
	var price_comp = id.replace('_qty','_price');
	var amt_comp = id.replace('_qty','_amt');
	var no_qty = document.getElementById(id).value;
if(parseFloat(no_qty)){
	var amt = no_qty * document.getElementById(price_comp).value
	
	if(amt !=0){
	document.getElementById(amt_comp).value = amt.toFixed(2);
	}
	else
	{
		
		document.getElementById(amt_comp).value = '';
	}
	}
	else
	{
		document.getElementById(id).value=0;
		document.getElementById(amt_comp).value = '';
	}
	var tot = getTotal();
	document.getElementById('total').innerHTML =tot.toFixed(2);
	//document.getElementById('grand_total').innerHTML =tot.toFixed(2);
	
}
function getTotal(){
	var amt_array = Array('eye_fillet_amt','hamburgers_pack6_amt',
'porter_house_amt','veal_ettes_amt',
'scotch_fillet_amt','veal_casserole_amt',
'premium_mince_amt','rump_amt',
'tbone_amt','sausage_mince_amt',
'bbq_steak_amt','osso_bucco_amt',
'veal_cutlets_amt','diced_veal_cubes_amt',
'oyster_blade_amt','veal_sausages_amt',
'eye_round_cuts_amt','sweet_chilli_amt',
'veal_steaks_amt','herb_garlic_amt',
'veal_roast_amt','tomato_bacon_amt',
'topside_roast_amt','smoky_bacon_amt',
'marinated_amt','veal_kebabs_amt',
'marinated_honey_amt','honey_soy_twists_amt',
'garlic_roast_amt','bbq_pack_1_amt',
'plain_roast_amt','bbq_pack_2_amt',
'jerollo_roast_amt','bbq_pack_3_amt',
'veal_brisket_amt','hot_pies_amt',
'veal_schnitzel_amt','cold_pies_amt',
'red_wine_amt','hot_pasties_amt',
'veal_mignon_amt','cold_pasties_amt',
'veal_olives_amt','sausage_rolls_amt',
'stir_fry_amt','stir_fry_marinated_amt','veal_racks_amt','gorello_amt','flavored_sausages_amt','pickled_veal_amt','diced_veal_amt','marinated_cubes_amt','silverside_amt','soup_bones_amt','marinated_roasts_amt','marinated_schnitzel_amt','veal_pies_amt','veal_sausage_rolls_amt','veal_pasties_amt','bbq_packs_amt','veal_twists_amt','veal_kievs_amt','fillet_mignon_amt','veal_hamburgers_amt','vealettes_amt','veal_meatloaf_amt','plain_sausages_amt','char_grill_rump_amt','tails_amt');
	
	//alert(amt_array.length);
	
	var total=0;
	for(var f=0; f < amt_array.length; f++)
	{
		if(document.getElementById(amt_array[f]).value!='' && document.getElementById(amt_array[f]).value!=0 && document.getElementById(amt_array[f]).value!='null')
		{
			total = total + parseFloat(document.getElementById(amt_array[f]).value);
		}
	}
	return total;
}
function  gotofocus()
{

	if(trim(document.getElementById('nameTxt').value)=='')
	{
		document.getElementById('nameTxt').focus();
	}
	else if(trim(document.getElementById('phoneTxt').value)=="")
	{
		document.getElementById('phoneTxt').focus();	
	}
	else if(trim(document.getElementById('emailTxt').value)=='')
	{
		document.getElementById('emailTxt').focus();
	}
	else if(trim(document.getElementById('cityTxt').value)=='')
	{
		document.getElementById('cityTxt').focus();
	}
	else if(trim(document.getElementById('postcodeTxt').value)=='')
	{
		document.getElementById('postcodeTxt').focus();
	}
	else if(trim(document.getElementById('addressTxt').value)=='')
	{
		document.getElementById('addressTxt').focus();
	}
	else if(trim(document.getElementById('stateTxt').value)=='')
	{
		document.getElementById('stateTxt').focus();
	}
}
	
// GET REGION LIST //
	
function getRegionList(countryId)
{
	
	var html = $.ajax({
				type	: 	"GET",
				url		: 	"/media/server-scripts/getRegionList.php",
				data	:	"countryId="+countryId,
				async	: 	false
			}).responseText;

	
	if(html != '')
	{
		var result	=	html.split("@@##@@");
		
		if(result[0] != '')
		{
			document.getElementById('region_show_div').innerHTML = result[0];
		}
		else
		{
			document.getElementById('region_show_div').innerHTML = result[0];
		}
		
		if(result[1] != '')
		{
			document.getElementById('distributors_show_div').innerHTML = result[1];
		}
		else
		{
			document.getElementById('distributors_show_div').innerHTML = '';
		}
	}
}

// GET DISTRIBUTORS LIST //

function getDistributorsList(regionId)
{

	var html = $.ajax({
				type	: 	"GET",
				url		: 	"/media/server-scripts/getDistributorsList.php",
				data	:	"regionId="+regionId,
				async	: 	false
			}).responseText;
	
	if(html != '')
	{
		document.getElementById('distributors_show_div').innerHTML = html;
	}
	else
	{
		document.getElementById('distributors_show_div').innerHTML = '';
	}
}





function closeWindow(){
document.getElementById("newdiv").style.display = 'none';
}