/*
	For the rest of the code visit http://www.WebCheatSheet.com	
	Copyright 2006 WebCheatSheet.com	
*/
//Gets the browser specific XmlHttpRequest Object 
function getXmlHttpRequestObject() {
 if (window.XMLHttpRequest) {
    return new XMLHttpRequest(); //Mozilla, Safari ...
 } else if (window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP"); //IE
 } else {
    //Display our error message
    alert("Your browser doesn't support the XmlHttpRequest object.");
 }
}

//Our XmlHttpRequest object
var receiveReq = getXmlHttpRequestObject();

//Initiate the AJAX request
function makeRequest(url, param) {
//If our readystate is either not started or finished, initiate a new request
 if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
   //Set up the connection to captcha_test.html. True sets the request to asyncronous(default) 
   receiveReq.open("POST", url, true);
   //Set the function that will be called when the XmlHttpRequest objects state changes
   receiveReq.onreadystatechange = updatePage; 

   receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   receiveReq.setRequestHeader("Content-length", param.length);
   receiveReq.setRequestHeader("Connection", "close");

   //Make the request
   receiveReq.send(param);
 }   
}

//Called every time our XmlHttpRequest objects state changes
function updatePage() {
 //Check if our response is ready
 if (receiveReq.readyState == 4) {
   //Set the content of the DIV element with the response text
   document.getElementById('result').innerHTML = receiveReq.responseText;
   //Get a reference to CAPTCHA image
   img = document.getElementById('imgCaptcha'); 
   //Change the image
   img.src = 'create_image.php?' + Math.random();
 }
}

//Called every time when form is perfomed
function getParam(theForm) {
////Set the URL
var url = 'captcha.php';
//Set up the parameters of our AJAX call
var postStr = theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value );
//Call the function that initiate the AJAX request
 makeRequest(url, postStr);
}

var Validate = {
	elements:[],
	//Some General purpose functions
	hasClass:function(ele,cls) {
		return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
	},
	addClass:function (ele,cls) {
		if (!this.hasClass(ele,cls)) ele.className += " "+cls;
	},
	removeClass:function (ele,cls) {
		if (this.hasClass(ele,cls)) {
			var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
			ele.className=ele.className.replace(reg,' ');
		}
	},
	stopEvent:function(e) {
		//e.cancelBubble is supported by IE - this will kill the bubbling process.
		e.cancelBubble = true;
		e.returnValue = false;
		//e.stopPropagation works in Mozilla.
		if (e.stopPropagation) {
			e.stopPropagation();
			e.preventDefault();
		}
	},
	success:function(ele) {
		this.removeClass(ele,"validation-error");
		this.addClass(ele,"validation-success");
	},
	failure:function(ele) {
		this.removeClass(ele,"validation-success");
		this.addClass(ele,"validation-error");
	},
	//Logic starts here...
	//Deny keyboard input for some specific chars.
	validateKeys : function(e,ele) {
		//Find Which key is pressed
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;
		var character = String.fromCharCode(code);

		//We need only valid keypresses - not stuff like shift, Enter, Backspace etc.
		if(	e.ctrlKey || 
			code == 46 //Delete Key
		) return;
		if(e.shiftKey) {
			if(!(code >= 37 && code <= 40)) return; //Up,Down,Right and Left - same keycode as %,&,' and (
		} else {
			if(!(code >= 41 && code <= 126)) return;
		}
	
		if(ele.getAttribute("allowedkeys")) {
			var allowed_chars = new RegExp(ele.getAttribute("allowedkeys"));
			if(!allowed_chars.test(character)) { //If a character was entered that is not allowed.
				this.stopEvent(e);
			}
		}
	},
	//See if the match is made - this is called on every keyup event.
	testMatch : function(ele) {
		if(ele.getAttribute("match")) {
			var match_reg = new RegExp(ele.getAttribute("match"));
			if(match_reg.test(ele.value)) this.success(ele);
			else {
				this.failure(ele);
				return false;//Don't continue if it is a faluire
			}
		}
		if(ele.getAttribute("equals")) {
			if(ele.value == eval(ele.getAttribute("equals"))) this.success(ele);//Yes I know, 'evil eval'.
			else {
				this.failure(ele);
				return false;
			}
		}
		if(ele.getAttribute("istrue")) {
			if(eval(ele.getAttribute("istrue"))) this.success(ele);//Again, eval.
			else {
				this.failure(ele);
				return false;
			}
		}
		return true;
	},
	//Show error message on form submission
	attachForm : function(form_id) {
		var ths = this;//Closure
		$(form_id).onsubmit=function(e) {
			if(!e) var e = window.event;
			var ele = this;
			var err = 0;
			for(var i=0,len=ths.elements.length; i<len; i++) {//Go thru all validation elements
				var ele = ths.elements[i];
				if(!ths.testMatch(ele)) {
					err++;
					if(document.getElementById("validation-error-message-"+ele.id)) continue; //Error message there already.
					//Insert an error message span after the input element.
					var msg = ele.getAttribute('errormessage');
					if(!msg) msg = "Error!!";
					
					var error_message = document.createElement("span");
					error_message.className = "validation-error-message";
					error_message.setAttribute("id","validation-error-message-"+ele.id);
					error_message.appendChild(document.createTextNode(msg));

					ele.parentNode.insertBefore(error_message,ele.nextSibling);
				}
			}
			if(err) {//Stop the submit action
				ths.stopEvent(e);
			}
		}
	},
	init : function () {
		var all_elements = document.getElementsByTagName("*");
		var ths = this;
		for(var i=0;ele=all_elements[i],i<all_elements.length;i++) {
			if(!ele.className.match(new RegExp('(\\s|^)live\-validate(\\s|$)'))) continue;
			this.elements.push(ele);
			//Attach the keyup event to the function. We are doing this in a round-about way because we need the 'this' keyword functionality
			if(ele.getAttribute("allowedkeys")) ele.onkeypress = function(e) {
				if(!e) var e = window.event;
				ths.validateKeys(e,this);
			}

			if(ele.getAttribute("match") || ele.getAttribute("equals") || ele.getAttribute("istrue")) {
				this.testMatch(ele);//The first mach should happen at page load
				ele.onkeyup = function() {
					ths.testMatch(this);
				}
			}
		}
	}
}
var keyStr = "ABCDEFGHIJKLMNOP" +
               "QRSTUVWXYZabcdef" +
               "ghijklmnopqrstuv" +
               "wxyz0123456789+/" +
               "=";
function decodesnip(input) {
     var output = "";
     var chr1, chr2, chr3 = "";
     var enc1, enc2, enc3, enc4 = "";
     var i = 0;
     // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
     var base64test = /[^A-Za-z0-9\+\/\=]/g;
     if (base64test.exec(input)) {
        alert("There were invalid characters in the input text.\n" +
              "Valid characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
              "Expect errors in decoding.");
     }
     input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
     do {
        enc1 = keyStr.indexOf(input.charAt(i++));
        enc2 = keyStr.indexOf(input.charAt(i++));
        enc3 = keyStr.indexOf(input.charAt(i++));
        enc4 = keyStr.indexOf(input.charAt(i++));
        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;
        output = output + String.fromCharCode(chr1);
        if (enc3 != 64) {
           output = output + String.fromCharCode(chr2);
        }
        if (enc4 != 64) {
           output = output + String.fromCharCode(chr3);
        }
        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";
     } while (i < input.length);
     return unescape(output);
  }