function URLDecode( urltodecode )
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = urltodecode;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   //document.URLForm.F1.value = plaintext;
   //document.URLForm.F1.select();
   return plaintext;
};


var GlobalDivName;
var ResultParams = new Array();
var Ok = 0;
NORMAL_STATE = 4;
INIT_STATE = 0;
HttpObj = CreateXMLHttp();

function CallAjax()
{
	ImgObj = document.getElementById("ImageBoxCurrentImage");
	if(ImgObj)
	{
		ImgSrc = ImgObj.src;
		ImageName = ImgSrc.split("/");
		//alert(URLDecode(ImageName[4]));
		ImageName = URLDecode(ImageName[4]);
		if(ImageName)
			RefreshAjaxDiv('ScriptDiv','favorite_painting.asp?PaintingSrc='+ImageName);
	}
	
	
}

function CreateXMLHttp()
{
	if (typeof XMLHttpRequest != "undefined") 
	{
        return new XMLHttpRequest();
  } 
	else if (window.ActiveXObject) 
	{
      var aVersions = [ "MSXML2.XMLHttp.5.0",
        "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
        "MSXML2.XMLHttp","Microsoft.XMLHttp"];

      for (var i = 0; i < aVersions.length; i++) 
	  	{
        try 
				{
            var oXmlHttp = new ActiveXObject(aVersions[i]);
            return oXmlHttp;
        } 
				catch (oError) 
				{
            //Do nothing
    		}
      }
    }
    throw new Error("XMLHttp object could be created.");
}

function RefreshAjaxDiv(DivName, Urls, AjaxMethod, PostVars, EmptyDiv, DivShowAjaxState, MsgLoading,PostFunc2Show)
{	
	MsgLoading=0;
	DivShowAjaxState = "AjaxLoadDiv";
	
	if(MsgLoading==1)
		MsgLoading = MsgImage;
	
	if (EmptyDiv && document.getElementById(DivName))
		document.getElementById(DivName).innerHTML = "";

	if (HttpObj.readyState == NORMAL_STATE || HttpObj.readyState == INIT_STATE)
	{
		if( AjaxMethod!= 'POST' )
		{
			AjaxMethod = 'GET';
			PostVars = null;
		}
		
		HttpObj.open(AjaxMethod, Urls, true);

		HttpObj.onreadystatechange=function()
		{
			/*Los Estados son:
			0 = uninitialized
			1 = loading
			2 = loaded
			3 = interactive
			4 = complete*/
			switch (HttpObj.readyState){
				case 1:
					  //Si Existe la Div de Muestra de Estado la muestra.
					  if(DivShowAjaxState)
						  if (document.getElementById(DivShowAjaxState)){
								if (MsgLoading)
										document.getElementById(DivShowAjaxState).innerHTML=MsgLoading;
								document.getElementById(DivShowAjaxState).style.display = "inline";
						  }
					  break;
				case 4:
					  if(HttpObj.status==200){						
							if (HttpObj.responseText)
							{
								var ResponseText = HttpObj.responseText;
								Div = document.getElementById(GlobalDivName);
								
								if(ResponseText.indexOf("//JS_RET;")!=-1)
								{
									//ResponseText = ResponseText.substring(20);
									ResponseText = ResponseText.replace("//JS_RET;"," ");
									//alert(ResponseText);
									eval(ResponseText);
								}
								else
								{
									if(GlobalDivName!="ScriptDiv" && GlobalDivName!="ErrorDiv")
									{
										CrearDiv(GlobalDivName,ResponseText);
									}
									else
									{
										if(GlobalDivName=="ErrorDiv" && ResponseText)
										{
											Div.style.border="solid 2px white";
											Div.style.backgroundColor = "#7E9FCC";
										}
										
										Div.innerHTML = ResponseText;
										$('#ErrorDiv').show("slow");
									}
								}

								if(PostFunc2Show)
									eval( PostFunc2Show );
							}
					  }
					  else
					  	alert("No se pudo recuperar la información: " + HttpObj.statusText);
						
						//Si Existe Div de Estado la Oculta
						if(DivShowAjaxState)
						{
							if (document.getElementById(DivShowAjaxState))
								document.getElementById(DivShowAjaxState).style.display = "none";
						}
					  break;
			}
		}
		
		if(document.body)
			document.body.style.cursor='wait';

		GlobalDivName = DivName;
		HttpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		
		HttpObj.send(PostVars);

		if(document.body)
			document.body.style.cursor='default';
	}
}


