// verify form fields in newsletter.htm

function isInteger(strV,fromV,toV)
//check for valid numeric strings with values from "fromV" to "toV"	
{
   if (strV.length == 0) return false;
   n = parseInt(strV); 
   if (fromV) if (n <fromV) return false;
   if (toV) if (n>toV) return false;   
   return true;
}

function enableIf(check, field)
{
   if (check.checked)
      field.disabled = true;
   else
      field.disabled = false;	  
}

function validateEmail(email)
{
// a very simple email validation checking. 
// you can add more complex email checking if it helps 
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}

function verifyNewForm(f) 
{ // f is the form
	var valid = true;
	var f_count = 8; // form's verifiable input fields
	// initialize fields
	for (i=0;i<f_count;i++) {
	   f.elements[i].style.background="#EFE4CB"; // default background	   
	   f.elements[i].value = f.elements[i].value.replace(/^\xA0+|\xA0+$/g,""); // remove non-breaking spaces (160 or A0 in hex)
	   f.elements[i].value = f.elements[i].value.replace(/^\s+|\s+$/g,""); // trim spaces (lead-trail)
	}
	// required	
    if (f.firstname.value.length<2) {
		f.firstname.style.background="yellow";
		valid = false;
	}		
    if (f.lastname.value.length<2) {
		f.lastname.style.background="yellow";
		valid = false;
	}
	if (f.country.selectedIndex==0)	{
	    f.country.style.background="yellow";
		valid = false;
	}
    if (f.postal_code.value.length<2) {
		f.postal_code.style.background="yellow";
		valid = false;
	}		
    if ((f.email.value.length<6)||(!validateEmail(f.email.value))) {
		f.email.style.background="yellow";
		valid = false;
	}
	// optional	
    if (f.customer_id.value.length!=0) if (!isInteger(f.customer_id.value,0,0)){
		f.customer_id.style.background="yellow";
		valid = false;
	}
    if (f.year_of_birth.value.length!=0) if (!isInteger(f.year_of_birth.value,1900,2010)) {
		f.year_of_birth.style.background="yellow";
		valid = false;
	}
		
	if (!valid)
		alert("Some required fields are missing or contain incorrect values.\nPlease fill in the fields marked with the yellow color and try again.");
	return valid;
}

function verifyDeleteForm(f) 
{ // f is the form
	var valid = true;
	var f_count = 2; // form's verifiable input fields

	// initialize fields
	for (i=0;i<f_count;i++) {
	   f.elements[i].style.background="#EFE4CB"; // default background	   
	   f.elements[i].value = f.elements[i].value.replace(/^\xA0+|\xA0+$/g,""); // remove non-breaking spaces (160 or A0 in hex)
	   f.elements[i].value = f.elements[i].value.replace(/^\s+|\s+$/g,""); // trim spaces (lead-trail)
	}
	// required	
    if (f.subscription_id.value.length!=8) {
		f.subscription_id.style.background="yellow";
		valid = false;
	}		
   	if ((f.email.value.length<6)||(!validateEmail(f.email.value))) {
			f.email.style.background="yellow";
			valid = false;
	}
		
	if (!valid)
		alert("Some required fields are missing or contain incorrect values.\nPlease fill in the fields marked with the yellow color and try again.");
	return valid;
}

function verifyUpdateForm(f) 
{ // f is the form
	var valid = true;
	var f_count = 5; // form's verifiable input fields
	var e_index= new Array(5); 
	e_index[0] = 0; e_index[1] =1; e_index[2] = 2; e_index[3] = 4; e_index[4] = 6;
	// initialize fields
	for (i=0;i<f_count;i++) {
	   f.elements[e_index[i]].style.background="#EFE4CB"; // default background	   
	   f.elements[e_index[i]].value = f.elements[e_index[i]].value.replace(/^\xA0+|\xA0+$/g,""); // remove non-breaking spaces (160 or A0 in hex)
	   f.elements[e_index[i]].value = f.elements[e_index[i]].value.replace(/^\s+|\s+$/g,""); // trim spaces (lead-trail)
	}
	// required	
    if (f.subscription_id.value.length!=8) {
		f.subscription_id.style.background="yellow";
		valid = false;
	}		
	
   	if ((f.current_email.value.length<6)||(!validateEmail(f.current_email.value))) {
		f.current_email.style.background="yellow";
		valid = false;
	}		
	
    // optional
	if (!f.same_country.checked) {
		if (f.country.selectedIndex==0)	{
	    	f.country.style.background="yellow";
			valid = false;
		}
	}	
	if (!f.same_postal.checked) {
    	if (f.postal_code.value.length<2) {
			f.postal_code.style.background="yellow";
			valid = false;
		}
	}
	if (!f.same_email.checked) {		
    	if ((f.new_email.value=="")||(!validateEmail(f.new_email.value))) {
			f.new_email.style.background="yellow";
			valid = false;
		}
	}	
		
	if (!valid)
		alert("Some required fields are missing or contain incorrect values.\nPlease fill in the fields marked with the yellow color and try again.");
	return valid;
}