// JavaScript Document

function ajax_submit ()
{
	jQuery("#waiting").html("<img src='js/waiting.gif' width=20><br>Please wait...");
	jQuery("#waiting").show();

	var mal = document.catalogue.mal.value;
	var title = document.catalogue.title.value;
	var firstname = document.catalogue.firstname.value;
	var name = document.catalogue.name.value;
	var country = document.catalogue.country.value;
	var language = getSelectedRadioValue(document.catalogue.language);
	var fifi = "CFOC_subscribe";


	var status = getSelectedRadioValue(document.catalogue.status);
	var alreadyCust = getSelectedRadioValue(document.catalogue.alreadyCust);

	var company = document.catalogue.company.value;
	var customer = document.catalogue.customer.value;
	var street = document.catalogue.street.value;
	var city = document.catalogue.city.value;
	var state = document.catalogue.state.value;
	var zip = document.catalogue.zip.value;
	var vat = document.catalogue.ein.value;
	var phone = document.catalogue.tel.value;
	var ur = document.catalogue.ur.value;
	var info = document.catalogue.info.value;
	var norobots = document.catalogue.norobots.value;
	var newsletter_savilerow = document.catalogue.newsletter_savilerow.value;
	var newsletter_bespoken = document.catalogue.newsletter_bespoken.value;


	var message = "";
	
	if (norobots=='')
		{
			document.catalogue.norobots.style.backgroundColor = "#999999";
	         	message += "Please fill in the anti-spam code.\n";			
			}	

	if (mal=='')
		{
			document.catalogue.mal.style.backgroundColor = "#999999";
	         	message += "Please fill in your e m a i l address.\n";			
			}	
			
	if (mal!='')
		{
		indexAroba = document.catalogue.mal.value.indexOf('@');
      	indexPoint = document.catalogue.mal.value.indexOf('.');
      	if ((indexAroba < 0) || (indexPoint < 0))      
			{
			document.catalogue.mal.style.backgroundColor = "#999999";
         	message += "Please check the email address, it doesn't look correct.\n";			
			}
		}	
		
	if (firstname=='')
		{
			document.catalogue.firstname.style.backgroundColor = "#999999";
	         	message += "Please fill in your first name.\n";			
			}
	if (name=='')
		{
			document.catalogue.name.style.backgroundColor = "#999999";
	         	message += "Please fill in your last name.\n";			
			}
	if (country=='--')
		{
	         	message += "Please choose your country.\n";			
			}
			
	if (status=='bu' && alreadyCust=='n')
		{
		if (company=='')
			{
			document.catalogue.company.style.backgroundColor = "#999999";
	         	message += "Please fill in your company.\n";			
			}
		if (street=='')
			{
			document.catalogue.street.style.backgroundColor = "#999999";
	         	message += "Please fill in your street.\n";			
			}
		if (city=='')
			{
			document.catalogue.city.style.backgroundColor = "#999999";
	         	message += "Please fill in your city.\n";			
			}
		if (zip =='')
			{
			document.catalogue.zip.style.backgroundColor = "#999999";
	         	message += "Please fill in your einnbr.\n";			
			}
/*		if (vat=='')
			{
			document.catalogue.ein.style.backgroundColor = "#999999";
	         	message += "Please fill in your tax number.\n";			
			} */
		if (phone=='')
			{
			document.catalogue.tel.style.backgroundColor = "#999999";
	         	message += "Please fill in your phone number.\n";			
			}
		}


	if (status=='bu' && alreadyCust=='y')
		{
		if (company=='')
			{
			document.catalogue.company.style.backgroundColor = "#999999";
	         	message += "Please fill in your company.\n";			
			}
		if (customer=='')
			{
			document.catalogue.customer.style.backgroundColor = "#999999";
	         	message += "Please fill in your Scabal Customer Number.\nOr fill in \"unknown\" if you are not sure.";			
			}
		}


	if (message != '')
		{
			jQuery("#waiting").html(message+'<p><b>[close]</b></p>');
			jQuery("#waiting").click( function() { jQuery("#waiting").hide(); } );
		}
	else
		{
		// on envoie la requete ajax
		jQuery.ajax(
			{
   			type: "POST",
   			url: "include" + "/" + fifi + ".php",
   			data: "title="+(title)+"&firstname="+(firstname)+"&name="+(name)+"&mail="+(mal)+"&country="+country+"&language="+language+"&info="+(info)+"&ur="+(ur)+"&phescaone="+(phone)+"&vat="+(vat)+"&zip="+(zip)+"&state="+(state)+"&city="+(city)+"&street="+(street)+"&customer="+(customer)+"&company="+(company)+"&alreadyCust="+(alreadyCust)+"&status="+escape(status)+"&norobots="+escape(norobots)+"&newsletter_savilerow="+escape(newsletter_savilerow)+"&newsletter_bespoken="+escape(newsletter_bespoken),
			success: function(msg){
								
				if (msg.indexOf('Sorry, ') != -1)
					{
					
					jQuery("#waiting").html(msg+'<p><b>[close]</b></p>');
					jQuery("#waiting").click( function() { jQuery("#waiting").hide(); } );
					}
				else
					{
					
					jQuery("#form").html(msg);
 					jQuery("#waiting").hide();
					}
 				}
			});
		}
	
	return false;
}


function getSelectedRadioValue (radiobutton)
{
	var returnValue = "";
	if (radiobutton.length == 1)
	{
		returnValue = radiobutton.value;
	} 
	else 
	{
		for (i=0;i<radiobutton.length;i++)
		{
			if (radiobutton[i].checked==true) 
			{
			returnValue=radiobutton[i].value;
			}
		}
	}
	return returnValue;
}



function adaptForm (status)
{
	
if (status == 'status_bu')	
	{
	jQuery(".bu").show();
	jQuery(".co").hide();
	jQuery(".new").show();
	jQuery(".old").hide();
	}
else if (status == 'status_new')	
	{
	jQuery(".old").hide();
	jQuery(".new").show();
	}
else if (status == 'status_old')	
	{
	jQuery(".old").show();
	jQuery(".new").hide();
	}
else
	{
	jQuery(".co").show();
	jQuery(".bu").hide();
	jQuery(".new").hide();
	}
}
