function RaisedBut(but)
{
	but.className='hot';
}
function FlatBut(but)
{
	but.className='tclass';
}
function Clicked(but)
{
	but.className='redhot';
}

function NextPage()
{  
    if (window.opener) {
	   self.close();
	 } else {
	   location.href = "page_one-2b.html";
	 }
}
function GoText(){
   next.innerText='Back to Main'
}
function noFrmGraphic(){
document.getElementById("urname").className='gry';
document.getElementById("email").className='gry';
document.getElementById("feedback").className='gry';
return true;
}
function RestoreFrmGraphic(){
document.getElementById('urname').className='grfnclr';
document.getElementById('email').className='grfnclr'
document.getElementById('feedback').className='grfnclr'
//return false;
}


// ===================== form processing and ajax ==========================
var mess = "";
var has_errors = 0;
var form = document.forms['myform'];
var ajax_post_res = false;




function validName(na_str){
   	var ok_name = "/^[ a-zA-Z0-9\-_\.]+$/";
   	var res = "";
 	
	if (na_str == "") {                     // name field needs a value
			has_errors++;
			mess = "Please enter a name.  Be sure you";
			mess += " only use characters that are letters, numbers, hyphen, underscore or period. Suggested ";
			mess += " format:\nFirstName (or) \nFirstName LastName"
			alert(mess);
			HiLiteError(form,1);
		return false;
	}
 	
   	res = eval( "na_str.match(" + ok_name + ")" );        // and value must be alphanumeric only, ie no punctuation,etc.
	if (res == null){
			has_errors++;
			mess = "The name you entered is invalid.  Be sure you";
			mess += " only use characters that are letters, numbers, hyphen, underscore or period. Suggested ";
			mess += " format:\nFirstName (or) \nFirstName LastName"
			alert(mess);
			HiLiteError(form,1);
			return false;
    }
 	return true;
}
 
 
 
function validEmail(em_str,em_str2){
	 if (em_str == "") {
        mess = "Please provide an email so that we may reply to your feedback";
        alert(mess);
        HiLiteError(form,2);
        return false;
    }
    if (em_str != em_str2) {
		mess = "There is an email address discrepancy; your email could not be confirmed.";
		alert(mess);
//		HiLiteError(form,3);
		HiLiteError(form,3);
		return false;
	}
   	// check the email address as follows:
 	/*  Valid email consists of:
      	1 or more alphanumeric chars, periods, hyphens or underscores, 
      	1 @ followed by
      	1 or more alphanumeric chars or hyphen followed by
      	a period and 2-6 alphabetical chars.
 	*/		
   var  ok_email = "/^([a-zA-Z0-9\-_\.]+)\@[\-a-zA-Z0-9]+\.[a-zA-Z]{2,6}$/";
   var   res = eval( "em_str.match(" + ok_email + ")" );

   if (res == null){
		has_errors++;
		mess = "The email address you entered is invalid.  Be sure you";
		mess += " don't have extra @s or periods. Please follow ";
		mess += " this format:\na@b.co (or) \na@b.com"

		alert(mess);
		HiLiteError(form,2);
		return false;
   }
   return true;
}



// did user provide comments 	 
function validMsg(msg,na_str)
{
	if (msg == "") {
      has_errors++;
	  alert("Hey " + na_str + " -- you neglected to provide any comments!");
	  HiLiteError(form,4);
	  return false;
	}
	return true;
}
 
 
  
// did user enter mandatory code
function getCaptcha(numEnt){  
  if (numEnt == "") {
       has_errors++;
       alert("If you are human, please enter the designated code.  All machines may disregard this message since the \"Contact\" feature is only intended for human beings.");
	   HiLiteError(form,5);
	   return false;  
  }
  return true;
}
// confirmation message
function confirmData(na_str,em_str,msg,numEnt){
 var ans= confirm("Your name is " + na_str + ". Your email is " + em_str + ".  while your message is:\n\n" + msg + "\n\n Also you entered a code of " + numEnt + ".  If any of this information is incorrect, please click CANCEL, otherwise confirm that you wish to send this message by clicking OKAY.");
  	if (!ans) {
     	has_errors++;
		return false;
  	}
	return true;
}

function val(form)
{
	var na_str =  form.urname.value;
	var em_str =  form.email.value;
	var cnf_em =  form.email2.value;
	var msg    =  form.feedback.value;
	var numEnt = form.num_ent.value;
    has_errors = 0;       // global var reset to zero so will get correct result if revalidating.  
    
	if (!validName(na_str)) {
		return false;
	} 	
    if (!validEmail(em_str,cnf_em)){
		return false;
	}	
    if (!validMsg(msg,na_str)) {
		return false;
	}
	if (!getCaptcha(numEnt)) {
	    return false;
	}
		
	if (!confirmData(na_str,em_str,msg,numEnt)) {
	   return false;
	}
//alert(has_errors);
	
  //  START OF AJAXING ....
    if (has_errors == 0) {				            // if no errors then okay to POST
      ajax_post_res = getForm2Post();               // if ajax_post_res == false means ajax failed to POST
// TESTING ....
// ajax_post_res = false;
// REMOVE when done TESTING
      return !ajax_post_res;    	               //  then POSTing will be done the old-fashioned way.
    }	  //*/
	else
	if (has_errors >= 1){
	    return false;
	}
 // END OF AJAXING ...                             
} // end validate


// error highlighting
function HiLiteError(form,n){

var obj = null;
     if (n == 4) 
          obj = form.feedback;

     else
     if (n == 2) 
          obj = form.email;
		  
	 else
	 if (n == 3)
	      obj = form.email2;
			 
	 else
	 if (n == 1)
		    obj = form.urname;
	 else
	 if (n == 5)
	         obj = form.num_ent;		

obj.style.background='deeppink';
obj.style.color='white';
      obj.focus();
      obj.select();

}
// restoring default form field color
function RestoreLemon(ffield) {
	ffield.style.background='white';
	ffield.style.color='black';
}

//=============================== AJAX and related functions =================:
// Source: http://www.captain.at/howto-ajax-form-post-request.php
function createQuery(form)
{
	var elements = form.elements;
	var pairs = new Array();

	for (var i = 0; i < elements.length; i++) {

		if ((name = elements[i].name) && (value = elements[i].value))
			pairs.push(name + "=" + encodeURIComponent(value));
	}

	return pairs.join("&");
}


   var http_request = false;
   function makePOSTRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            //http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');   // preventing FFx bug 
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         //alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
	  return true;
   }

   function alertContents() {
      if (http_request.readyState == 4)
	  {
         if (http_request.status == 200)
		 {
            //alert(http_request.responseText);
            result = http_request.responseText;
		    // may wish to remove form by using javascript/DOM code!
			document.getElementById('myspan').innerHTML = result;
         }
		 else
		 {
            //alert('There was a problem with the request.');
         }
      }
   }
   
   function getForm2Post() {
      var form = document.forms['myform'];
      var poststr = createQuery(form);
      var post_res = makePOSTRequest('../smarty_contactForm/ajax_contactform.php', poststr);             // ajax-php: exp_contactform.php
	                                                                              // ajax fail: contactform.php
//alert("value of post res is " + post_res);
	  return post_res;
   }


