// CREATING THE REQUEST

function createRequestObject()
{
	try
	{
		xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e)
	{
		alert('Sorry, but your browser doesn\'t support XMLHttpRequest.');
	}
	return xmlhttp;
}

var http = createRequestObject();
var sess = createRequestObject();

// IMAGE REFRESHING

function refreshimg()
{
	var url = '/captcha/image_req.php';
	dorefresh(url, displayimg);
}

function dorefresh(url, callback)
{
	sess.open('POST', '/captcha/newsession.php', true);
	sess.send(null);
	http.open('POST', url, true);
	http.onreadystatechange = displayimg;
	http.send(null);
}

function displayimg()
{
	if(http.readyState == 4)
	{
		var showimage = http.responseText;
		document.getElementById('captchaimage').innerHTML = showimage;
	}
}

// SUBMISSION

function check()
{
	var submission = document.getElementById('captcha').value;
	var url = '/captcha/process.php?captcha=' + submission;
	docheck(url, displaycheck);
}

function docheck(url, callback)
{
	http.open('GET', url, true);
	http.onreadystatechange = displaycheck;
	http.send(null);
}

function displaycheck()
{
	if(http.readyState == 4)
	{
		var showcheck = http.responseText;
		if(showcheck == '1')
		{
			document.getElementById('captcha').style.border = '1px solid #49c24f';
			document.getElementById('captcha').style.background = '#bcffbf';
		}
		if(showcheck == '0')
		{
			document.getElementById('captcha').style.border = '1px solid #c24949';
			document.getElementById('captcha').style.background = '#ffbcbc';

		}
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return '';
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

$.fn.saveAsCookie = function(n,t){
   return this.each(function(){
     createCookie( (n || '') + (this.name || this.id), this.value, t );
   });
 };

$.fn.loadAsCookie = function(n){
  return this.each(function(){
    this.value = readCookie( (n || '') + (this.name || this.id) );
  });
};

 $(document).ready(function(){
   $("form#TheForm").submit(function(){
  
     $("input.save, textarea.save",this).saveAsCookie("formdata");
   
   })
   .find("input.save, textarea.save").loadAsCookie("formdata");
  
 });

   
 
var captachatimer;
function  autocheck(e)
	{
	if (e.keyCode == 27)
		{
		e.value = '';
		return false;
		}
	
	if (captachatimer)  window.clearTimeout(captachatimer);
	if (e.value != ''  ) captachatimer = window.setTimeout('check();', 900);
	
	return true;
	}
	
	
	
	
	
	
	// (C) 2000 www.CodeLifter.com
var good;
function checkEmailAddress(field) {
var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
if (goodEmail){
   good = true
} else {
   alert('Please enter a valid e-mail address.')
   field.focus()
   field.select()
   good = false
   }
}

function sendOff(){
   nmcheck = document.TheForm.cr_name.value
   if (nmcheck.length <1) {
      alert('Please enter your name.')
      return false;
   }

   nm2check = document.TheForm.cr_nationality.value
   if (nm2check.length <1) {
      alert('Please enter your Nationality.')
      return false;
   }
      nm2check = document.TheForm.cr_tel.value
   if (nm2check.length <1) {
      alert('Please enter your Telephone Number.')
      return false;
   }
   good = false
   checkEmailAddress(document.TheForm.cr_email)
   if (good != true) {
   return false;
   }
   if ((document.TheForm.cr_email.value ==
        document.TheForm.emx.value)&&(good)){

       return true;
   }
   if ((document.TheForm.cr_email.value !=
          document.TheForm.emx.value)&&(good)){
          alert('Both e-mail address entries must match.')
         return false;
   }
}