// JavaScript Document

function MM_preloadImages() { //v3.0
  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];}}
}

// ===== Set Up =========================================================
// Preload the images to be used with the form
// ======================================================================

var Pic = new Array

Pic[0] = 'img/btn_clear.gif'
Pic[1] = 'img/btn_over_clear.gif'
Pic[2] = 'img/btn_over_clear.gif'
Pic[3] = 'img/btn_send.gif'
Pic[4] = 'img/btn_over_send.gif'
Pic[5] = 'img/btn_over_send.gif'

// ----------------------------------------------------------------------
// This section of code preloads the images you named in the Pic[] array
// above, so images will be ready as soon as the page opens.

var p = Pic.length
var preLoad = new Array()
var i = 0;
for (i = 0; i < p; i++){
     preLoad[i] = new Image()
     preLoad[i].src = Pic[i]
}

// ----------------------------------------------------------------------
// The next very short function, switchImage(whichImage,imageNumber),
// handles all image switching in response to mouseOver, mouseOut, and
// mouseDown events.
//
// The variable whichImage carries the *name* of the image to be
// switched (see the comments in the HTML below) -- in this case, either
// 'resetImage' or 'submitImage'.
//
// The variable imageNumber carries the number of the image to be called,
// as per the numbers in the Pic[] array up above.
//
// Read the HTML code comments below to see how to attach this function
// to the images. Note that it's actually hooked to <a href> tags around
// the images, rather than to the images themselves, since NS4 does not
// allow attaching events to images.

function switchImage(whichImage,imageNumber){
   document.images[whichImage].src = preLoad[imageNumber].src
}



// ----------------------------------------------------------------------
// The doSubmit() function fires whenever the submitImage is clicked.
// You will wish to modify this function to perform the action you wish
// to take place when the submitImage is clicked.  See the comments
// within the function.

function doSubmit(){

    //return(isReady())
	//return(ValidateExample1())
	return(ValidateForm(contact_form));
   // optional -- but it gets the lines off the image in IE
   self.focus()
   
   // if you wanted to call an html page, this is where you would
   // do it, in the format of the following example line...
   // window.location='http://www.codebrain.com/java/index.html'
}


function doReset(){

   // optional -- but it gets the lines off the image in IE
   self.focus()

   // clear the fields in the form...
   document.contact_form.name.value = ""
   document.contact_form.email.value = ""
   document.contact_form.phonenumber.value = ""
   document.contact_form.subject.value = ""
   document.contact_form.ordernumber.value = ""
   document.contact_form.comments.value = ""
   
}

function isEmpty(aField) {
   if ((aField.value.length==0) ||
   (aField.value==null)) {
      return true;
   }
   else { return false; }
}

function emailCheck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Please enter a valid email.")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Please enter a valid email..")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Please enter a valid email.")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Please enter a valid email.")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Please enter a valid email.")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Please enter a valid email.")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Please enter a valid email.")
		    return false
		 }

 		 return true					
	}

function CheckEmail()
{
	var emailID=document.contact_form.email
	
	if (emailCheck(emailID.value)==false){
		emailID.focus()
		return false
	}
	return true
 }

function ValidateForm(form)
{

   if(isEmpty(form.name)) 
   { 
      alert('You have not entered your name.') 
      form.name.focus(); 
      return false; 
   } 
 	
  if(isEmpty(form.email)) 
  { 
      alert('You have not entered your email.') 
      form.email.focus(); 
     return false; 
  }   
  else
 	CheckEmail();
 
   if(isEmpty(form.subject)) 
   { 
      alert('You have not entered a subject.') 
      form.subject.focus(); 
      return false; 
      }

   if(isEmpty(form.comments)) 
   { 
      alert('You have not entered your comments.') 
      form.comments.focus(); 
      return false; 
      } 
 
return true;
 
} 
