/**
 * (C) Copyright 2009, All rights reserved.
 *
 *
 * This is the startup page for preferences
 * 
 * @author  Markus Roccaro&Tom DiZoglio
 * @version 2009-09-17
 */

var DEBUG=false;

function MM_swapImgRestore()
{
   var i,x,a=document.MM_sr;
   for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++)
      x.src=x.oSrc;
}

function MM_preloadImages()
{
   try
   {
	   var d=document;
	   if(d.images)
	   {
	      if(!d.MM_p)
	         d.MM_p=new Array();
	      var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
	      for(i=0; i<a.length; i++)
	      {
	         if (a[i].indexOf("#")!=0)
	         {
	            d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];
	         }
	      }
	   }
   }
   catch(e)
   {
	   if(DEBUG)
		  alert("EXCEPTION Pre-load failed: "+e+"\n\t"+e.name+"\n\t"+(e.number&0xFFFF)+"\n\t"+e.description);
   }
}

function MM_findObj(n, d)
{
   var p,i,x;
   if(!d)
      d=document;
   if((p=n.indexOf("?"))>0&&parent.frames.length)
   {
      d=parent.frames[n.substring(p+1)].document;
      n=n.substring(0,p);
   }
   if(!(x=d[n])&&d.all)
      x=d.all[n];
   for(i=0;!x&&i<d.forms.length;i++)
      x=d.forms[i][n];
   for(i=0;!x&&d.layers&&i<d.layers.length;i++)
      x=MM_findObj(n,d.layers[i].document);
   if(!x && d.getElementById)
      x=d.getElementById(n);
   return x;
}

function MM_swapImage()
{
   var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array;
   for(i=0;i<(a.length-2);i+=3)
   if((x=MM_findObj(a[i]))!=null)
   {
      document.MM_sr[j++]=x;
	  if(!x.oSrc)
         x.oSrc=x.src; x.src=a[i+2];
   }
}

function onLoadEvent()
{
   try
   {
      MM_preloadImages('images/nav2_01.gif','images/nav2_02.gif','images/nav2_03.gif','images/nav2_04.gif','images/button_download_2.gif');
   }
   catch(e)
   {
	  if(DEBUG)
         alert("EXCEPTION onLoadEvent failed: "+e+"\n\t"+e.name+"\n\t"+(e.number&0xFFFF)+"\n\t"+e.description);
   }	
}

//function to check empty fields
function isEmpty(strName, strQuestion)
{
   //change "field1, field2 and field3" to your field names
   var strName = document.forms[0].name.value 
   var strQuestion = document.forms[0].message.value

   // Name field
   if(strName==""||strName==null||!isNaN(strName)||(strName.length == 0))
   {
      if(DEBUG)
         alert("\"Name\" is a mandatory field.\nPlease update and retry.")
      return true;
   }

   // Question field 
   if(strQuestion==""||strQuestion==null||strQuestion.length == 0)
   {
      if(DEBUG)
         alert("\"Question\" is a mandatory field.\nPlease update and retry.")
      return true;
   }

   return false;
}

/**
 * This will find an element by Id
 * 
 * @param elemId
 * @return
 */
function findElementById(elemId)
{
   var elem=document.getElementById(elemId);
   if(elem)
      return elem;

   /* This is the handling for IE */
   if(document.all)
   {
      elem=document.all[elemId];
      if(elem)
          return elem;
 
      for(var i = (document.all.length-1); i >= 0; i--)
      {
          elem=document.all[i];
          if(elem.id==elemId)
              return elem;
      }
   }
   return null;
}

//sets the visibility flag for an object
function toggleVisibility(id,visible)
{
   var elem=findElementById(id);
   if(elem)
      elem.style.visibility=visible?"visible":"hidden";
}

/*
 * Trims trailing whitespace chars.
 * strValue - String to be trimmed.
 * returns: Source string with right whitespaces removed.
 */
function rightTrim(strValue)
{
   var objRegExp = /^([\w\W]*)(\b\s*)$/;

   if(objRegExp.test(strValue))
   {
      //remove trailing a whitespace characters
      strValue = strValue.replace(objRegExp, '$1');
   }
   return strValue;
}

/*
 * Trims Trims leading whitespace chars.
 * strValue - String to be trimmed.
 * returns: Source string with left whitespaces removed.
 */
function leftTrim(strValue)
{
   var objRegExp = /^(\s*)(\b[\w\W]*)$/;

   if(objRegExp.test(strValue))
   {
      //remove leading a whitespace characters
      strValue = strValue.replace(objRegExp, '$2');
   }
   return strValue;
}

/*
 * Removes leading and trailing spaces.
 * strValue - Source string from which spaces will be removed.
 * returns: Source string with whitespaces removed.
 */
function trimAll(strValue)
{
   var objRegExp = /^(\s*)$/;

   //check for all spaces
   if(objRegExp.test(strValue))
   {
      strValue = strValue.replace(objRegExp, '');
      if(strValue.length == 0)
         return strValue;
   }

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue))
   {
      //remove leading and trailing whitespace characters
      strValue = strValue.replace(objRegExp, '$2');
   }
   return strValue;
}

/************************************************
  Validates that a string contains a valid email pattern.

  strEmail - String to be tested for validity

  returns True if valid, otherwise false.

  Accounts for email with country appended
  does not validate that email contains valid URL
  type (.com, .gov, etc.) or valid country suffix.
*************************************************/
function isValidEmail(strEmail)
{
   var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
   var strEmail = document.forms[0].email.value;

   // Make sure no invalid characters
   for (i=0; i<invalidChars.length; i++)
   {
      if (strEmail.indexOf(invalidChars.charAt(i),0) > -1)
      {
         if(DEBUG)
            alert('Email address contains invalid characters.');
         return false;
      }
   }

   // Make sure no non ascii characters
   for (i=0; i<strEmail.length; i++)
   {
      if (strEmail.charCodeAt(i)>127)
      {
         if (DEBUG)
            alert('Email address contains non ascii characters.');
         return false;
      }
   }

   // Verify format of email now that could have a valid one
   var atPos = strEmail.indexOf('@',0);
   if (atPos == -1)
   {
      if (DEBUG)
         alert('email address must contain an @');
      return false;
   }
   if (atPos == 0)
   {
      if (DEBUG)
         alert('email address must not start with @');
      return false;
   }
   if (strEmail.indexOf('@', atPos + 1) > - 1)
   {
      if (DEBUG)
         alert('email address must contain only one @');
      return false;
   }
   if (strEmail.indexOf('.', atPos) == -1)
   {
      if (DEBUG)
         alert('email address must contain a period in the domain name');
      return false;
   }
   if (strEmail.indexOf('@.',0) != -1)
   {
      if (DEBUG)
         alert('period must not immediately follow @ in email address');
      return false;
   }
   if (strEmail.indexOf('.@',0) != -1)
   {
      if (DEBUG)
         alert('period must not immediately precede @ in email address');
      return false;
   }
   if (strEmail.indexOf('..',0) != -1)
   {
      if (DEBUG)
         alert('two periods must not be adjacent in email address');
      return false;
   }
   var suffix = strEmail.substring(strEmail.lastIndexOf('.')+1);
   if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum')
   {
      if (DEBUG)
         alert('invalid primary domain in email address');
      return false;
   }

   return true;
}

// Validate a field has less than maxchars
function checkMaxCharacters(maxchars, id)
{
   var form=findElementById(id);
   if(form.box.value.length > maxchars)
   {
      if(DEBUG)
         alert('Too much data in the text box! Please remove '+(document.ourform.box.value.length - maxchars)+' characters');
      return false;
   }
   else
   {
      return true;
   }
}

// function that performs all functions, defined in the onsubmit event handler
function formValidation(id)
{
   var form=findElementById(id);
   if(form)
   {
	  //if (isEmpty(leftTrim(form.name))||isEmpty(leftTrim(form.message))||isEmpty(form.email))
      if (isEmpty(form.name)||isEmpty(form.message)||isEmpty(form.email))
      {
         toggleVisibility('error_box_invalid_email', false);
         toggleVisibility('error_box_empty_fields', true);
         return false;
      }

      if (!isValidEmail(form.email))
      {
         toggleVisibility('error_box_invalid_email', true);
         toggleVisibility('error_box_empty_fields', false);
         return false;
      }

      if(id == "blacklist_form")
      {
         if (isEmpty(form.website))
         {
            toggleVisibility('error_box_invalid_email', false);
            toggleVisibility('error_box_empty_fields', true);
            return false;
         }
      }
   }
   toggleVisibility('error_box_invalid_email', false);
   toggleVisibility('error_box_empty_fields', false);
   return true;
}

function formSubmit(id)
{
	if (formValidation(id))
	{
		var form=findElementById(id);
		form.submit();
	}
}

function limitKeyDown(id,MaxLen)
{
   var elem=findElementById(id);
   //return (elem.value.length <= MaxLen);
   if(elem.value.length <= MaxLen)
	   return false;
   else
   {
	   elem.value = elem.value.substring(0,MaxLen);
	   return true;
   }
//   if (elem.value.length > MaxLen) 
//      elem.value = elem.value.substring(0,MaxLen);
}

/**
 * Faq specific code used to expand and contract faq's
 * 
 * @param tbodyid
 * @param iconId
 * @param size
 * @return
 */
function expandContractFaq(tbodyid,iconId,size) {
   var elemIcon=findElementById(iconId);
   var elemBody=findElementById(tbodyid);
   
   if (elemIcon.src.indexOf("grey")>=0){
      elemIcon.src="images/arrow_blue_"+size+".gif";
      elemBody.style.display = "";
   } else {
      elemIcon.src="images/arrow_grey_"+size+".gif";
      elemBody.style.display = "none";
   }
}
