// JavaScript Document
function validateemail() {
	
  var theForm = document.frm;
  var reason = "";
  reason += validateName(theForm.name);
  //reason += validatePhone(theForm.c_no);
  reason += validateEmail(theForm.email);
  reason += validateEmpty(theForm.address,"Address");
  reason += validateEmpty(theForm.suburb,"Suburb");
  reason += validateEmpty(theForm.postcode,"Postcode");
  reason += validateEmpty(theForm.day_work,"Preferred work day");
  
  /*if(validateEmpty(theForm.code) == "")
  {

          if(window.XMLHttpRequest)
           {
            xmlhttp = new XMLHttpRequest();
           }
           else
           {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
           }
           xmlhttp.onreadystatechange = function()
           {
              if(xmlhttp.readyState == 4 )
              {
                  //alert(xmlhttp.responseText);
                  if(xmlhttp.responseText == 0)
                  {
                      theForm.code.style.background = 'Yellow'; 
                      reason += "The entered code is invalid.\n"
                  }
                  else
                  {
                    if (reason == "")
                    {
                    theForm.action="contact.php"; 
                    theForm.submit();
                    }
                  }
              }
           }
           xmlhttp.open("GET","ajax_contact.php?code="+theForm.code.value,true);
           xmlhttp.send();
      
  }*/
  
 /* if()
  {
	  reason += 	
  }*/
  /*if (!document.frm.send.checked) {
 reason += "\n - You must agree to the terms";
} */

  
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
		document.frm.submit();
		return false;
  }
  
}
function validatefax() {
  var reason = "";
  var theForm = document.frm;
  reason += validateName(theForm.first_name);
  reason += validateName(theForm.last_name);
  reason += validateEmail(theForm.email);
  reason += validateEmpty(theForm.address);
  reason += validateEmpty(theForm.time1);
  reason += validateEmpty(theForm.time2);

/*  if(validateEmpty(theForm.code) == "")
  {

          if(window.XMLHttpRequest)
           {
            xmlhttp = new XMLHttpRequest();
           }
           else
           {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
           }
           xmlhttp.onreadystatechange = function()
           {
              if(xmlhttp.readyState == 4 )
              {
                  //alert(xmlhttp.responseText);
                  if(xmlhttp.responseText == 0)
                  {
                      theForm.code.style.background = 'Yellow'; 
                      reason += "The entered code is invalid.\n"
                  }
                  else
                  {
                    if (reason == "")
                    {
                    theForm.action="contact1.php"; 
                    theForm.submit();
                    }
                  }
              }
           }
           xmlhttp.open("GET","ajax_contact.php?code="+theForm.code.value,true);
           xmlhttp.send();
      
  }*/
 if (!document.frm.am1.checked && !document.frm.pm1.checked) {
 reason += "\n - Please choose am/pm";
} 
if (!document.frm.am2.checked && !document.frm.pm2.checked) {
 reason += "\n - Please choose am/pm";
} 
var chks = document.getElementsByName('day[]');
var checkCount = 0;
//alert(chks.length);

for (var i = 0; i < chks.length; i++)
{
if (chks[i].checked)
{
checkCount++;
}
}
if (checkCount < 1)
{
alert("Please select at least one avilable day.");
return false;
}
var inter = document.getElementsByName('interests[]');
var checkCount = 0;
//alert(chks.length);

for (var i = 0; i < inter.length; i++)
{
if (inter[i].checked)
{
checkCount++;
}
}
if (checkCount < 1)
{
alert("Please select at least one Interest.");
return false;
}

if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
		document.frm.submit();
		return false;
  }
  
}

function validateName(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "Please enter a name.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateEmpty(fld,txt) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Please enter "+txt+".\n"
    } else {
        fld.style.background = 'White';
    }
 return error;  
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "Please 		enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    }
  else {
        fld.style.background = 'White';
    }
    return error;
}
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Please enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } 
  else {
        fld.style.background = 'White';
    }
    return error;
}

function Get_Cookie( check_name ) {
 // first we'll split this cookie up into name/value pairs
 // note: document.cookie only returns name=value, not the other components
 var a_all_cookies = document.cookie.split( ';' );
 var a_temp_cookie = '';
 var cookie_name = '';
 var cookie_value = '';
 var b_cookie_found = false; // set boolean t/f default f

 for ( i = 0; i < a_all_cookies.length; i++ )
 {
  // now we'll split apart each name=value pair
  a_temp_cookie = a_all_cookies[i].split( '=' );


  // and trim left/right whitespace while we're at it
  cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

  // if the extracted name matches passed check_name
  if ( cookie_name == check_name )
  {
   b_cookie_found = true;
   // we need to handle case where cookie has no value but exists (no = sign, that is):
   if ( a_temp_cookie.length > 1 )
   {
    cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
   }
   // note that in cases where cookie is initialized but no value, null is returned
   return cookie_value;
   break;
  }
  a_temp_cookie = null;
  cookie_name = '';
 }
 if ( !b_cookie_found )
 {
  return null;
 }
}

         
jQuery(document).ready(function(){
  var cookie_value = Get_Cookie("mailsuccess");
 
  if(cookie_value)
	{
   
	   cookie_vals = cookie_value.replace(/\+/g,' ');
	   if(document.getElementById("show_msg"))
	   {
	   	document.getElementById("show_msg").innerHTML = cookie_vals;
	   }
	   Delete_Cookie("mailsuccess","",""); 
	}
	else
	{
	  if(document.getElementById("show_msg"))
	  {
	    document.getElementById("show_msg").innerHTML = "";
	  }
	} 
});
  
	
function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

