// JavaScript Document

// Main code checking loop

function checkWholeForm(theForm) {
	var why = "Please check the following items in the form:\n";
	var no_error_why = why;
	
	// Check user's First name.
    
	var temp = "";
	temp += checklength(theForm.first_name.value);
	var tempval = 0;
	if("" == temp)
	{
		 tempval = checklatin(theForm.first_name.value);
		 if(0 == tempval)
		 {
			 why += "First Name does not have valid characters\n";
		 }
	 
	}
	else
	{
		 why += "First Name" + temp;
	}
	temp = "";

	// Check user's Last name.	

	temp += checklength(theForm.last_name.value);
	tempval = 0;
	if(temp == "")
	{
		 tempval = checklatin(theForm.last_name.value);
		 if(0 == tempval)
		 {
			 why += "Last Name does not have valid characters\n";
		 }
	 
	}
	else
	{
		 why += "Last Name" + temp;
	}

	// Verify the Company field.
	
	temp = "";

	temp += checklength(theForm.company.value);
	tempval = 0;
	if("" == temp)
	{
		 tempval = checklatin(theForm.company.value);
		 if(0 == tempval)
		 {
			 why += "Company Name does not have valid characters\n";
		 }
	 
	}
	else
	{
		 why += "Company" + temp;
	}
	temp = "";
	
	// Verify email. First verify length.
	
	temp += checklength(theForm.email.value);
	if("" == temp)
	{
		 //if length is correct then check validity of email
		 tempval = checkEmail(theForm.email.value);       
		 if(0 == tempval)
		 {
			 why += "Email id is invalid\n";
		 }
		 else
		 {
			 // Confirm email should be tested only if email id is valid:
			 //Verify the Confirm email field
			 temp = "";    
			 temp += checklength(theForm.email2.value);
			 if("" == temp)
			 {
				  //if length is correct then check validity of email
				  why += checkRetypeEmail(theForm.email.value,theForm.email2.value);       
			 }
			 else
			 {
				  why += "Confirm email" + temp;
			 }
		 }
	}
	else
	{
		why += "Email" + temp;
	} 
	 
	if (why != no_error_why)
    {
		alert(why);
		return false;
	}

	// Assemble list of interests from check boxes.

	var checked_interests = "Checked Interests:\n";
	var first_time = true;
	
	/* Debug code.
	document.writeln("first_time: " + first_time + "<br />");
	document.writeln("checked_interests: " + checked_interests + "<br />");
	document.writeln("interest count: " + theForm.interest.length + "<br />");
	--- */
	
	for (i=0, n=theForm.interest.length; i<n; i++) {

		// document.writeln("  interest[" + i + "] " + theForm.interest[i].checked + "<br />");
	
		if (theForm.interest[i].checked)
        {
			if (first_time == true)
            {
				checked_interests += theForm.interest[i].value;
				first_time = false;
			}
			else
            {
				checked_interests += ", " + theForm.interest[i].value;
			}
  		}
	}

	// document.writeln("first_time: " + first_time + "<br />");
	// document.writeln("checked_interests: " + checked_interests + "<br />");

	if (first_time != true)
		theForm.description.value = checked_interests;
	else
		theForm.description.value = "";

	// document.writeln("Form description: '" + theForm.description.value + "'<br />");

	return true;
}

// email

function checkEmail (strng)
{
   var filter = /^([a-zA-Z0-9_\-\+%]+\.{0,1})+@([a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,4}$/; 
   if(filter.test(strng))
   {
     var ind = 0;
     ind = strng.indexOf("@");
     if("." == strng.charAt(ind-1) || "." == strng.charAt(ind+1))
     {
        return 0;
     }
     else
     {
        return 1;
     }
     
     
   }
   else
   {
     return 0;
   }
   
}

function checkRetypeEmail (strng, strng2)
{
   var error = "";
   if (strng != strng2)
   {
     error = " Email addresses typed do not match.\n";
	 return error;
   }
   return error;
}


// new validation for length of the string entered.

function checklength(strng)
{
  var err = "";
  if(strng.length > 250)
  {
     err = " should be less than 250 characters\n";
  }
  else if(strng.length < 1)
  {
     err = " field cannot be empty\n";
  }
  return err;
    
}

function checklatin(strn)
{

   var filter = /^[\x20-\x7E\xC0-\xFF]+$/;
   if(filter.test(strn))
   {
     return 1;
   }
   else
     return 0;
}

