// CREATING THE REQUEST

function createXmlHttpRequestObject() 
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
 
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) 
    {
      try 
      { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

var http = createXmlHttpRequestObject();
var sess = createXmlHttpRequestObject();
var xmlHttp = createXmlHttpRequestObject();
var xmlHttpCap = createXmlHttpRequestObject();
var showcheck;
// IMAGE REFRESHING

function refreshimg()
{
	//var url = 'captcha/image_req.php';
	dorefresh();
}

function dorefresh()
{
	if (sess){
		try{
			sess.open('POST', 'captcha/newsession.php', true);
			sess.onreadystatechange = dorefreshimg;
			sess.send(null);
		}
		catch (e)
		{
			alert("sess: Can't connect to server:\n" + e.toString());
		}	
		//dorefreshimg();
	}
}

function dorefreshimg()
{
	if(sess.readyState == 4)
	{
		if (http){
			try{
				
				http.open('POST', 'captcha/image_req.php', true);
				http.onreadystatechange = displayimg;
				http.send(null);
			}
			catch (e)
			{
			  alert("http: Can't connect to server:\n" + e.toString());
			}
		}	
	}
}

function displayimg()
{
	if(http.readyState == 4)
	{
			
		//var showimage = http.responseText;
		showimage = '<a href="index.php" onclick="refreshimg(); return false;" title="haga clic para cambiar la imagen"><img src="captcha/images/image.jpg?' + rand(10000) + '" width="135" height="27" alt="imagen de seguridad" border="0" /></a>';
		myDiv = document.getElementById("captchaimage");
	    myDiv.innerHTML = showimage;
	}
}

function rand ( n )
{
  return ( Math.floor ( Math.random ( ) * n + 1 ) );
}
// SUBMISSION

function process(parameters)
{
  // only continue if xmlHttp isn't void
  // AJAX get product data 
  // svenka design studio
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
      // initiate reading a file from the server
      xmlHttp.onreadystatechange = handleRequestStateChange;
	  //xmlHttp.onreadystatechange = alertContents;
      xmlHttp.open("POST", "captcha/post.php", true);
	  xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlHttp.setRequestHeader("Content-length", parameters.length);
      xmlHttp.setRequestHeader("Connection", "close");
      xmlHttp.send(parameters);
	  
    }
    // display the error in case of failure
    catch (e)
    {
      alert("Can't connect to server:\n" + e.toString());
    }
  }
  
  
}


// function called when the state of the HTTP request changes
function handleRequestStateChange() 
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4) 
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) 
    {
      try
      {
        // do something with the response from the server
        handleServerResponse();
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    } 
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" + 
            xmlHttp.statusText);
 
    }
  }
}

// handles the response received from the server
function handleServerResponse()
{
	
	var resp = xmlHttp.responseText;
	var html = resp;
	
	myDiv = document.getElementById("ccontenido");
	//display the HTML output
	if (resp !="")
		myDiv.innerHTML = html;
}


/* POST COMMENTS 
----------------------------------------------------------------------------------*/
function enviar(obj) {
	document.getElementById("benviar").disabled=true;
	check();	
}

function check()
{
	var submission = document.getElementById('captcha').value;
	var url = 'captcha/process.php?captcha=' + submission;
	docheck(url, displaycheck);
}

function docheck(url, callback)
{
	xmlHttpCap.open('GET', url, true);
	xmlHttpCap.onreadystatechange = displaycheck;
	xmlHttpCap.send(null);
}

function displaycheck()
{
	if(xmlHttpCap.readyState == 4)
	{
		showcheck = xmlHttpCap.responseText;
		if(showcheck == '1')
		{
			document.getElementById('captcha').style.border = '1px solid #49c24f';
			document.getElementById('captcha').style.background = '#bcffbf';
			
			email = document.getElementById("email").value;
			nombre = document.getElementById("nombre").value;
			telefono = document.getElementById("telefono").value;
			comentario = document.getElementById("comentario").value;
			
			if ( nombre !="" && comentario !="" && email !="" ){			
				var poststr = "nombre=" + encodeURI( nombre ) +
				"&email=" + encodeURI( email) +
				"&telefono=" + encodeURI( telefono) +
				"&comentario=" + encodeURI( comentario );
				process(poststr);
			}
		}
		if(showcheck == '0')
		{
			document.getElementById('captcha').style.border = '1px solid #c24949';
			document.getElementById('captcha').style.background = '#ffbcbc';
			document.getElementById("benviar").disabled=false;
		}
	}
}