/*************************************************************
                     Global Variables
 *************************************************************/
var browserType         = (navigator.appName=="Netscape")? "Netscape" : (navigator.appName=="Microsoft Internet Explorer")? "IE" : "Others";
var browserVersion		= navigator.appVersion; 
var browserAgent		= navigator.userAgent; 
var digits              = "0123456789";
var lowerCaseLetters    = "abcdefghijklmnopqrstuvwxyz";
var upperCaseLetters    = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var whiteSpace          = " ";
var underScore          = "_";
var decimalPtDelimiter  = ".";
var accountNumDelimiter = "-";
var phoneNumDelimiter   = "()-+ ";

function custommsg() {
	document.getElementById("form-message").style.display=""; 
	document.getElementById("form-message").innerHTML='<BR>Thank you for downloading the resources of Insurance System (ISU).'+
	'<BR><BR>'+
	'<TABLE>'+
		'<TR>'+
		'<TD>&nbsp;</TD>'+
		'<TD class=style5>'+
			'<A href="http://www.ifshk.com/download/isu_setup.exe">Download ISU-Lite</A>'+
		'</TD>'+
		'</TR>'+
		'<TR>'+
		'<TD>&nbsp;</TD>'+
			'<TD class=style5>'+
				'<A href="http://www.ifshk.com/download/isulite.pdf">ISU-Lite User Manual</A>'+
		'</TD>'+
		'</TR>'+
		'<TR>'+
			'<TD>&nbsp;</TD>'+
			'<TD class=style5>'+
				'<A href="http://www.ifshk.com/download/isuliteform.pdf">ISU-Lite Form User Manual</A>'+
			'</TD>'+
		'</TR>'+
	'</TABLE>';
	document.getElementById("form-container").style.display="none";
	document.getElementById("form-desc").style.display="none";
} 


function onSubmitForm(f){
	f.action='http://spreadsheets.google.com/formResponse?key=p7UgtaFWXI4GZJX2aJMNHfQ'; 
	if (isBlankForm(f)) {
		custommsg(); 
		return false;
	} else if (validate(f)){ 
		custommsg(); 
		return true; 
	} else {
		return false;
	}
}

// check if the form is blank
function isBlankForm(f){
	if (isEmpty(f.entry_0) && isEmpty(f.entry_2) && isEmpty(f.entry_5)&& isEmpty(f.entry_3)){
		return true;
	}else {
		return false;
	}
}


// validate the form
function validate(f){
	/*
	if (isEmpty(f.entry_2)){
		alert("Contact Person should be filled.");
		return false;
	}else if (isEmpty(f.entry_5)){
		alert("Email should be filled.");
		return false;
	}else */
	if (!isEmail(f.entry_5,"Email is invalid.")){
		return false;
	}else if (!isTelNO(f.entry_3,"Tel number is invalid.")){
		return false;
	}/*else if (!isFaxNO(f.entry_4,"Fax number is invalid.")){
		return false;
	}  */
	return true; 
}

/* Input : ff  - form.formfield (an obj)
           msg - the whole sentence of error message
   Output: Null (just to alert msg & focus on the formfield)
*/
function printError (ff,msg)
{   
   alert (msg); 
   if (!ff.disabled){
	   ff.focus();   
   }
}//end of printError()

/* Aim   : Check whether the email address are inputed correctly 
   Remark: No input is also true.
           Email address must be of form receipentName@hostName/domainName.GpN.CC
           CC is the country code. It can be nothing or 2 alphabets only.
           GpN is the Group Name (e.g. net,com,edu,sex,ust...). It must be 3 alphabets.
           In receipentName, only digits,alphabets,'-_.' are allowed.
		   In DomainName and hostname, only digits,alphabets,'-.' are allowed.
 	       There is only one '@'
		   The first letter behind @ must be alphabes/digits.
		   No consecutive '.' allowed.		   
*/
function isEmail (ff,msg)
{      
   if (isWhiteSpace(ff)) 
	  return true;    
   
   //General Checking
   var ffLength = ff.value.length;
   if ((ffLength>=7)&&

	   (ff.value.indexOf("..")==-1)&& //No consecutive '.' allowed.
	   (isUserDefinedChars(digits+lowerCaseLetters+upperCaseLetters+"@._-",ff)))
   {   
	  var atPos=ff.value.indexOf("@");
	  var receipentName=ff.value.substring(0,atPos);        
	  var wordsAfterAt=ff.value.substring(atPos+1,ffLength);	  
	  var firstFullStopPos=wordsAfterAt.indexOf(".")+receipentName.length+1;
      var lastFullStopPos=wordsAfterAt.lastIndexOf(".")+receipentName.length+1;
	  var secondLastFullStopPos=wordsAfterAt.lastIndexOf(".",(lastFullStopPos-1-receipentName.length-1))+receipentName.length+1;
	  var firstHostName=wordsAfterAt.substring(0,firstFullStopPos); 

	  
	  if ((atPos>0)&&		  
          (isUserDefinedChars(digits+lowerCaseLetters+upperCaseLetters+"_-.",ff.value.substring(0,atPos)))&& //check chars before @
		  (firstHostName.length>=1)&&
		  (isUserDefinedChars(digits+lowerCaseLetters+upperCaseLetters,firstHostName.substring(0,1)))) //The first letter behind @ must be alphabes/digits.
	  {	 	 
		 if (lastFullStopPos!=-1)
		 {
            var lastWords=ff.value.substring(lastFullStopPos+1,ffLength);			
			//receipentName@firstHostName.????.????.????.GpN.CC
			if ((lastWords.length==2)&&(isUserDefinedChars(lowerCaseLetters+upperCaseLetters,lastWords)))
		    {   
               if (secondLastFullStopPos!=-1) {                 
				  var secondLastWords=ff.value.substring(secondLastFullStopPos+1,lastFullStopPos); 
				  if ((secondLastWords.length==3)&&(isUserDefinedChars(lowerCaseLetters+upperCaseLetters,secondLastWords)))				
				     if  (isUserDefinedChars(digits+lowerCaseLetters+upperCaseLetters+"-.",ff.value.substring(atPos+1,secondLastFullStopPos)))
                         return true;    				   
               }
            }
			//receipentName@firstHostName.????.????.????.GpN
			else if  ((lastWords.length==3)&&(isUserDefinedChars(lowerCaseLetters+upperCaseLetters,lastWords)))
			{  
               if  (isUserDefinedChars(digits+lowerCaseLetters+upperCaseLetters+"-.",ff.value.substring(atPos+1,lastFullStopPos)))
                  return true; 
			}//end of else if 
		 }//end of if		 
      }//end of if    
   }//end of if
   if (msg!=null)
      printError(ff,msg);
   return false;
}//end of isEmail()

/* Aim     : to check the telephone number whether only contains digits or phoneNumDelimiter  
   Remark  : No input is also true.
*/
function isTelNO (ff,msg)
{
   if (isUserDefinedChars (digits+phoneNumDelimiter,ff))
      return true;
   if (msg!=null)
      printError(ff,msg);   
   return false; 
}//end of isTelNO()

/* Aim     : to check the fax number whether only contains digits or phoneNumDelimiter  
   Remark  : No input is also true.
*/
function isFaxNO (ff,msg)
{
   if (isUserDefinedChars (digits+phoneNumDelimiter,ff))
      return true;
   if (msg!=null)
      printError(ff,msg);   
   return false; 
}//end of isTelNO()

/* Aim   : check whether the form field has no input &
           check whether the form field has only whitespace input  
   Remark: It also elimilates all spaces at the start/end of the input string
*/
function isWhiteSpace (ff,msg)
{  	 
   if (!isEmpty(ff))
   {
      for (var i=0; i < ff.value.length; i++) {
         //-->Check that current character isn't whitespace.		    		 
         if (ff.value.charAt(i) !=whiteSpace) { 	
			chopHeadTailSpace (ff); //Eliminate the space at the head and tail
			return false;
         }
      }//end of for
   }   
   if (msg!=null)
	  printError(ff,msg);	   
   return true;
}//end of isWhiteSpace()

/* Aim:    Elimilate the space at the start and the end of the input string.
           Then we will set the formfield value.  
   Remark: must use before isWhiteSpace()
*/
function chopHeadTailSpace (ff)
{ 
   var head=0,tail=ff.value.length-1;
   if (ff.type!="file")
   { 
      ff.value+=whiteSpace;                                                  
   	  while ((ff.value.charAt(head)==whiteSpace)&&(head<ff.value.length)) 
   	     head++;                                                          
   	  while ((ff.value.charAt(tail)==whiteSpace)&&(tail>0))               
   	     tail--;                                                          
   	  if ((head!=0)||(tail!=(ff.value.length-1)))	                       
	    ff.value=ff.value.substring(head,tail+1);                        
   }
}//end of chopHeadTailSpace()

/* Aim    : check whether all inputted chars are our defined chars   
   Input  : userDefinedChars - is a string, eg. lowerCaseLetters+underScore
            ff - it can be an HTMLinputElement Object or a value. 
			     If it is a value, no printError() supported.   
   Remarks: no input will return true 
*/
function isUserDefinedChars (userDefinedChars,ff,msg)
{
   var c;
   
   if (typeof(ff)=="object")
   { 	
      for (var i=0; i<ff.value.length; i++)
      {  
	     c=ff.value.charAt(i);         
	     if (userDefinedChars.indexOf (c) ==-1) {
            if (msg!=null)
               printError(ff,msg);	   
		    return false;      	
         }
      }//end of for
   }
   else
   {	   
      for (var i=0; i<ff.length; i++)               
   	  {                                            
	     c=ff.charAt(i);                        
	     if (userDefinedChars.indexOf (c) ==-1) {  
	  	    return false;      	                
   	    }                                         
   	  }                                            
   }
   return true;                                 
}//end of isUserDefinedChars()

/* Aim: check whether the form field has no input 
*/
function isEmpty(ff)
{  
   return ((ff == null) || (ff.value.length == 0));
}//end of isEmpty()

