//
// Copyright: A Better Deal for ALL (C) 2006 all rights reserved.
// Designed & Developed by: A Better Deal for All ABN: 64 648 123 459
// URL: http://www.BathurstInternet.com.au/
// Author: Graham Hagney.
// Program Name: Auto.js
// Date Created: Dec 2009
// Description: 
//    creation of Captcha image & AutoTab

function randomString() {
//----------------------------------------
	//document.getElementById('submit').disabled=true;
	document.getElementById('submit').value='Check Form';

	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 15;
	var randomChars = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomChars += chars.substring(rnum,rnum+1);
	}
	document.getElementById('YourMessage').value = randomChars;
	document.getElementById('CaptchaSrc').src = 'http://captchator.com/captcha/image/'+document.getElementById('YourMessage').value;
	//alert('randomChars='+randomChars+'.....'+document.getElementById('CaptchaSrc').src);
}

var AjaxResponseText = '';
var myRequest = '';
var objectId = '';

  function displayAjax(myURL,DivId) {
//----------------------------------------
     // Clear myRequest
     myRequest = false;
     objectId = DivId;
 	 document.getElementById('CaptchaProcessingImage').style.display = "block";

     
     // For browsers: Safari, Firefox, etc. use one XML model
     if (window.XMLHttpRequest) {
          myRequest = new XMLHttpRequest();
          if (myRequest.overrideMimeType) {
               myRequest.overrideMimeType('text/xml');
          }
     } else if (window.ActiveXObject) {
          // For browsers: IE, version 6 and before, use another model
          try {
               myRequest = new
                    ActiveXObject("Msxml2.XMLHTTP");
          } catch (e) {
               try {
                    myRequest = new
                         ActiveXObject("Microsoft.XMLHTTP");
               } catch (e) {}
          }
     }
     // Make sure the request object is valid
     if (!myRequest) {
          alert('Error: Cannot create XMLHTTP object');
          return false;
     }
     
     // Link to display function activated when result returned
     myRequest.onreadystatechange = displayReturn;
     // Open the URL request
     myRequest.open('GET', myURL, true);
     // Send request
     myRequest.send(null);
   }



  function displayReturn() {
//----------------------------------------
     // Check to make sure result came through, 4=complete 
     if (myRequest.readyState == 4) {
         //Check HTTP status code.....
	    //alert ( 'myRequest.readyState='+myRequest.readyState+'....myRequest.status='+myRequest.status);
          if (myRequest.status == 200) {
               // Display the responseText
			    //alert ( 'AjaxResponseText='+myRequest.responseText);
				//AjaxResponseText = myRequest.responseText;
				document.getElementById('CaptchaProcessingImage').style.display = "none";
				if (myRequest.responseText == '1') {
					document.getElementById('CaptchaResult').value='Yes';
					document.getElementById('submit').value='Submit';
					document.getElementById('submit').disabled=false;
					document.getElementById('CaptchaError').style.display = 'none';
				} else {
					document.getElementById('CaptchaError').style.display = "block";
					document.getElementById('CaptchaError').innerHTML = 'The code you entered did not match the image. Please retry the new code';
					randomString();
					document.getElementById('submit').value='Check Form';
				    //alert ( 'captcha incorrect');
				}
				//document.getElementById(objectId).innerHTML = AjaxResponseText;
          } else {
			    alert ( 'captcha status='+myRequest.status+'....AjaxResponseText='+myRequest.responseText);

				document.getElementById('CaptchaError').style.display = "block";
				document.getElementById('CaptchaError').innerHTML = 'There was a problem processing your code code. Please retry the new code';
				// ??document.getElementById('CaptchaProcessingImage').style.display = "none";
				randomString();
				document.getElementById('submit').value='Check Form';
          }
     }
}


  function Captcha_onblur() {
//----------------------------------------
  CaptchaURL = '/Port-Office-Hotel-Contact/SiteData/checkcaptcha.php?code='+document.getElementById('YourMessage').value+'&answer='+document.getElementById('CaptchaAnswer').value+'/';
  displayAjax(CaptchaURL,'CaptchaError');
}


// Auto-tab code

var isNN = (navigator.appName.indexOf("Netscape")!=-1);

  function autotab(input,len, e) {
//---------------------------------
var keyCode = (isNN) ? e.which : e.keyCode; 
var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
if(input.value.length >= len && !containsElement(filter,keyCode)) {
input.value = input.value.slice(0, len);
input.form[(getIndex(input)+1) % input.form.length].focus();
}
  

  function containsElement(arr, ele) {
//------------------------------------
var found = false, index = 0;
while(!found && index < arr.length)
  if(arr[index] == ele)
     found = true;
  else
    index++;

return found;
}

  function getIndex(input) {
//----------------------------
var index = -1, i = 0, found = false;
while (i < input.form.length && index == -1)
  if (input.form[i] == input)index = i;
  else i++;

return index;
}

return true;

}



