var _form;
//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 {

 }
}

//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
   if(receiveReq.responseText=='false'){
   	set_error(document.getElementById('err_captcha'), CAPTCH_ERR);
   	//Get a reference to CAPTCHA image
	   var img = document.getElementById('imgCaptcha'); 
	   //Change the image
	   img.src = '/sharedobj/php/captcha/raf_captcha.php?' + Math.random()+'&color='+_color;
   }
   else{
   	unset_error(document.getElementById('err_captcha'));
   	_form.submit();
   }
 }
}

//Called every time when form is perfomed
function validate_captcha(theForm) {
 //Set the URL
 var url = '/sharedobj/php/captcha/validate_captcha.php?validate=1';
 //Set up the parameters of our AJAX call
 var postStr = theForm.cpcheck.name + "=" + encodeURIComponent( theForm.cpcheck.value );
 //Call the function that initiate the AJAX request
 makeRequest(url, postStr);
}

function validate_form(frm){
	var frm_valid = true;
	_form = frm;
	if(!validate_username()){
		frm_valid = false;
	}
	if(!validate_emails()){
		frm_valid = false;
	}
	if(!check_captcha()){
		frm_valid = false;
	}
	
	if(frm_valid)
		validate_captcha(frm);
		
	
	return false;
}

function validate_username(){
	if(document.getElementById('username').value.length<7){
		set_error(document.getElementById('err_username'), USERNAME_ERR)
		return false;
	}else{
		unset_error(document.getElementById('err_username'));
		return true;
	}
}

function check_captcha(){
	if(document.getElementById('cpcheck').value.length<4){
		set_error(document.getElementById('err_captcha'), CAPTCH_ERR)
		return false;
	}else{
		unset_error(document.getElementById('err_captcha'));
		return true;
	}
}

function validate_emails(){
	var patern =/^([a-zA-Z0-9\._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/ ;
	var found = false;
	for(var i=0;i<3;i++){
		val = document.getElementById('email'+i).value;
		if(val.length && val.match(patern)){
			found = true;
			unset_error(document.getElementById('err_email'+i));
		}
		else if(val.length != 0){
			set_error(document.getElementById('err_email'+i), EMAIL_ERR);
			return false;
		}
	}
	if(found){
		return true;
	}
	else{
		set_error(document.getElementById('err_email0'), EMAIL_ERR);
		return false;
	}
}

function set_error(el, msg){
	el.innerHTML = msg;
	el.style.display = 'block';
}

function unset_error(el){
	el.style.display = 'none';
}