// these functions clear the values in the form when the input box is clicked
// this enable the input label to put in the input box e.g. 'enter your name'
function InitForm()
{
    $k = document.getElementsByTagName("INPUT");
    $morevalue = new Array();
    for($count = 0;$count < $k.length;$count++)
    {
       if($k[$count].type != "submit")
       {
          $morevalue[$k[$count].name] = $k[$count].value;
          $k[$count].onfocus = input_focus;
          $k[$count].onblur = input_blur;
       }
    }

    $k = document.getElementsByTagName("TEXTAREA");
    for($count = 0;$count < $k.length;$count++)
    {
       $morevalue[$k[$count].name] = $k[$count].value;
       $k[$count].onfocus = input_focus;
       $k[$count].onblur = input_blur;
    }
 }
// these functions are called from InitForm()
    function input_focus()
    {
       if(this.value == $morevalue[this.name])
       {
          this.value = "";
       }
    }

    function input_blur()
    {
       if(this.value == "")
       {
          this.value = $morevalue[this.name];
       }
    }


// function for setting the value of name, email and button text on forms
function InitVals(pNameVal, pEmailVal, pButton)
{
   $k = document.getElementsByTagName("INPUT");
   for($count = 0;$count < $k.length;$count++)
   {
      if($k[$count].name == "fname")   // set the name
      {
         $k[$count].value = pNameVal;
      }
      if($k[$count].name == "email")   // set the email
      {
         $k[$count].value = pEmailVal;
      }
      if($k[$count].type == "submit")
      {
         $k[$count].value = pButton;   // set the button
      }
   }
   return true;
}

// function for setting the value of name, email and button text on forms
function InitValsUser(pUser1, pUser2, pUser3, pUser4)
{
   $k = document.getElementsByTagName("INPUT");
   for($count = 0;$count < $k.length;$count++)
   {
      if($k[$count].name == "user1")   // set user1
      {
         $k[$count].value = pUser1;
      }
      if($k[$count].name == "user2")   
      {
         $k[$count].value = pUser2;
      }  
      if($k[$count].name == "user3")  
      {
         $k[$count].value = pUser3;
      } 
      if($k[$count].name == "user4") 
      {
         $k[$count].value = pUser4;
      }      
   }
   return true;
}
// validation of simple forms requiring first name and email
function ProcessForm(fForm, sListId, sData)
{
   var bReturn = false;
   fForm.list.value = sListId;                 // the number of the list to be added to
   fForm.user1.value = window.location.href;   // custom field 1 is the calling page
   fForm.user2.value = sData;               // this is the version of the page, discount code etm
   bReturn = ValidateForm( fForm.email.value, fForm.fname.value );
   return bReturn;
}

function ProcessFormCallBack(fForm, sListId)
{
    var bReturn = false;
    var sPhone = "";
    
    fForm.list.value = sListId; 
    bReturn = ValidateForm( fForm.email.value, fForm.fname.value );
    if (bReturn == true)
    {
        sPhone = trimAll( fForm.user2.value );   
        if (sPhone.length < 6)
        {
            bReturn = false;
            alert ("Please enter your contact phone number complete with country and area code" );
        }        
    }
    return bReturn;
}


function ValidateForm(psEmail, psName)
{
   var bReturn = true;
   var sEmail, sName;

   sName = trimAll( psName );
   if (sName.length <= 0)
   {
      bReturn = false;
      alert ("Please enter your first name" );
   }
   else
   {
      sEmail = trimAll( psEmail )
      bReturn = validateEmail( sEmail );
      if ( bReturn == false )
      {
         alert( "Please enter a valid email address" );
      }
   }
   return bReturn;
}

function trimAll(sString) 
{
   while (sString.substring(0,1) == ' ')
   {
      sString = sString.substring(1, sString.length);
   }
   while (sString.substring(sString.length-1, sString.length) == ' ')
   {
      sString = sString.substring(0,sString.length-1);
   }
   return sString;
}

function validateEmail(email)
{
    if(email.length <= 0)
   {
     return false;
   }
    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;
}