// JavaScript Document
var debug = false;
var keyElement = document.getElementById("_key");
var formName = GetAttribute(keyElement, 'formName', '');
if (formName.length > 0)
{
	var theForm;
	if ($(formName))
	{
		theForm = $(formName);
	}
	else
	{
		theForm = document.getElementsByName(formName)[0];
	}
	
	Event.observe(window,'load',function() {
		
		if (theForm.readAttribute('onsubmit') != '')
		{
			theForm.setAttribute('onsubmit','');
		}
		
		 Event.observe(theForm,'submit', function(event) {
					   if (!_do_validation())
					   {
							Event.stop(event);   
					   }
				 });
		 });
}


function _do_validation()
{
	var key = '';
	var postParams = '';
	var guid = '';
	var thisURL = GetAttribute(keyElement, 'thisURL', '');
	
	if (keyElement)
	{
		key = $('_publicKey').value;
		guid = $('_guid').value;
		postParams = 'checkvalue=' + key + '&guid=' + guid + '&key=' + keyElement.value;
	}
	
	//alert(thisURL + '/captcha/scrCaptcha.asp');
	SendAjaxHttpPost('/captcha/callbacks/validate.asp',postParams,'_process_validation', null)
		
	return false;
}

function _process_validation(resp,func)
{
	//var keyElement = document.getElementById('_key');
	var validateFunction = GetAttribute(keyElement, 'validateFunction', 'ValidationDummy');
	var passFormToFunction = GetAttribute(keyElement, 'passFormToFunction', 'false');
	var tmp = '';
	
	if (passFormToFunction.length > 0)
	{
		tmp = '$("' + formName + '")';	
	}
	
	if (eval(validateFunction + '(' + tmp + ')'))
	{
		if (keyElement)
		{
			//alert('test: ' + keyElement.value);
			if (resp == 'True')
			{
				//var formName = GetAttribute(keyElement, 'formName', 'form1');
				eval('document.'+formName+'.submit()');
				//alert('Submitting Form 1');
			}
			else
			{
				alert('Image text was not entered correctly');
				//var formName = GetAttribute(keyElement, 'formName', 'form1');
				//eval('document.'+formName+'.submit()');
			}
		}
	}
	else
	{
		//alert('Submitting Form 2');
		//eval('document.'+formName+'.submit()');	
	}
}

function ValidationDummy()
{
	return true;
}

function GetAttribute(pElement, pAttributeName, pDefaultValue) 
{
  var vResult = pElement.getAttribute(pAttributeName, 0);
  if (vResult == null)
     vResult = pDefaultValue; 
  return vResult; 
} // GetAttribute()


function GetXmlHttp() {	
	var xmlhttp = false;
	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
  }
	else if (window.ActiveXObject)// code for IE
	{
		try 
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp=false;
			}
		}
	}
	return xmlhttp;
}

function PassAjaxResponseToFunction(url, callbackFunction, params)
{		
  var xmlhttp = new GetXmlHttp();
  //now we got the XmlHttpRequest object, send the request.
  if (xmlhttp)
  {
    xmlhttp.onreadystatechange = function () 
                                {
	                                if (xmlhttp && xmlhttp.readyState==4)
	                                {//we got something back..
										var result = 0;
										try
										{
											result = xmlhttp.status;
										}
										catch (e)
										{
											result = 200;	
										}
		                                if (result==200)
		                                {
			                                var response = xmlhttp.responseText;
			                                var functionToCall = callbackFunction+'(response,'+params+')';
			                                if(debug){
				                                alert(response);
				                                alert (functionToCall);
			                                }
			                                eval(functionToCall);
		                                } else if(debug){
			                                alert(xmlhttp.responseText);
		                                }
	                                }
                                }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
  }
}

function SendAjaxHttpPost(url,postParams,callbackFunction,params)
{		
  var xmlhttp = new GetXmlHttp();
  //now we got the XmlHttpRequest object, send the request.
  if (xmlhttp)
  {
    xmlhttp.onreadystatechange = function () 
                                {
	                                if (xmlhttp && xmlhttp.readyState==4)
	                                {//we got something back..
										//alert(xmlhttp.responseText);
		                                if (xmlhttp.status==200)
		                                {
											//alert(xmlhttp.responseText);
											var response = xmlhttp.responseText;
											var functionToCall = callbackFunction
											//alert(functionToCall);
											if (params)
											{
												functionToCall += '('+params+',response)';
											}
											else
											{
												functionToCall += '(response)';
											}
			                                if(debug){
				                                alert(xmlhttp.responseText);
				                                alert (functionToCall);
			                                }
											eval(functionToCall);
		                                } else if(debug){
			                                alert(xmlhttp.responseText);
		                                }
										//alert(xmlhttp.responseText);
	                                }
                                }
    xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", postParams.length);
    //xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(postParams);
  }
}