var bugChars = '!#$^&*()+|}{[]?><~%:;/,=`"\'';

function displayDate() { 
	var date = new Date();
    var weekDays = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
    var monthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");    
	var dateString = weekDays[date.getDay()] + ",  " + monthNames[date.getMonth()] + "  " + date.getDate() + ",  " + date.getFullYear();
	var dateLength = dateString.length;
	var leftPadding = (390 - (dateLength * 6)/2) - 19;   // 28 or 25
	document.write("<DIV style='position:absolute; top:0; padding-top:10px; padding-left:" + leftPadding + "px;'>");     
    document.write("<SPAN class='date'><NOBR>" + dateString);
    document.write("</NOBR></SPAN><BR/>");   
    document.write("</DIV>");
}

function displayDate_2() { 
	var date = new Date();
    var weekDays = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
    var monthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");    
	document.write("<DIV class='date'>");    
    document.write("<SPAN class='date'><NOBR>" + weekDays[date.getDay()] + ", ");
    document.write(monthNames[date.getMonth()] + " ");
    document.write(date.getDate() + ", ");
    document.write(date.getFullYear());
    document.write("</NOBR></SPAN><BR/>");   
    document.write("</DIV>");
}


function displayDate_test() { 
	
	var str = "Wednesday,  September  22,  2009";
	var dateLength = str.length;
	var leftPadding = (390 - (dateLength * 6)/2) - 19;    //28  or 25
    document.write("<DIV style='position:absolute; top:0; padding-top:10px; padding-left:" + leftPadding + "px;'>");  
	document.write("<SPAN class='date'><NOBR>" + str);
    document.write("</NOBR></SPAN><BR/>");   
    document.write("</DIV>");
}

function checkContactForm() {
   var name = document.forms.contact_form.elements['Name'];
   var email = document.forms.contact_form.elements['Email_Address'];
   if (trim(name.value) == null || trim(name.value) == "") {
      alert('Name is required. Please enter your Name.');
	  name.focus();
	  name.style.backgroundColor="#999966";
      return false;
   }
   if (trim(email.value) == null || trim(email.value) == "") {
      alert('E-mail Address is required. Please enter your E-mail Address.');
	  email.focus();
	  email.style.backgroundColor="#999966";
      return false;
   }

   if (validate_email_address(trim(email.value)) == false){
	  //email.value = "";
	  email.focus();
	  email.style.backgroundColor="#999966";
	  return false;
   }

   return true;
}

function clear_colors() {
   var name = document.forms.contact_form.elements['Name'];
   var email = document.forms.contact_form.elements['Email_Address'];

   name.style.backgroundColor="#F0ECE1";  
  
   email.style.backgroundColor="#F0ECE1"; 

     
}

function CharsInBag(ch) {
   var i;
   var empty_char = "";
    // Search through string's characters one by one.
    // If character is not in bag.
    for (i = 0; i < ch.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = ch.charAt(i);
		if(i>0)
			empty_char = ch.charAt(i-1);
        if (bugchars.indexOf(c) != -1 || (empty_char == "." && c == ".")) 
			return false;
    }
    return true;
}

function isInteger(ch) {
   var i;
   for (i = 0; i < ch.length; i++)
    {   
        // Check that current character is not a number.
        var c = ch.charAt(i);
        if ((c >= "0") && (c <= "9") && (c != ".")) 
			return false;
    }
    // All characters are numbers.
    return true;
}

function validate_email_address(email) {
	var at = "@";
	var dot = ".";
	var at_length = email.indexOf(at);
	var str_length = email.length;
	var dot_length = email.indexOf(dot);
	var last_dot = email.lastIndexOf(dot);
	if (email.indexOf(at) == -1) {
	   alert("Invalid E-mail Address!");
	   return false
	}
	if (email.indexOf(at) == -1 || email.indexOf(at) == 0 || email.indexOf(at) == str_length) {
	   alert("Invalid E-mail Address!");
	   return false;
	}
	if (email.indexOf(dot) == -1 || email.indexOf(dot) == 0 || email.indexOf(dot) == str_length || email.substring(last_dot+1) == "") {
	    alert("Invalid E-mail Address!");
	    return false;
	}
		 
	 if (email.indexOf(at, (at_length + 1)) != -1) {
	    alert("Invalid E-mail Address!");
	    return false;
	 }

	 if (email.substring(at_length - 1, at_length) == dot || email.substring(at_length + 1, at_length + 2) == dot) {
	    alert("Invalid E-mail Address!");
		return false;
	 }

	 if (email.indexOf(dot, (at_length + 2)) == -1) {
	    alert("Invalid E-mail Address!");
	    return false;
	 }
		
	 if (email.indexOf(" ") != -1) {
	    alert("Invalid E-mail Address!");
	    return false;
	 }
	  
	 if(CharsInBag(email) == false) {
	    alert("Invalid E-mail Address!");
	    return false;
	 }

	 var arrEmail = email.split("@");
	 var dot_length = arrEmail[1].indexOf(".");
	 if(isInteger(arrEmail[1].substring(dot_length + 1)) == false) {
	    alert("Invalid E-mail Address!");
	    return false;
	 }
 	 return true;					
}



function trim(str) {
	return str.replace(/^\s+|\s+$/g,"");
}
