// JavaScript Document
// JavaScript Document
var http = getXmlHttpObject();
var http2 = getXmlHttpObject();
var http_request= false; 
var wheretoreplace="shoppingcart";
var aux_ele="";
var nale=0;
var DivRelleno='...';
var idSelected=null;

//funcion para obtener objetos por su id cross browser
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
  d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
   var NChar="";
   for (i = 0; i < sText.length&&IsNumber; i++) 
      { 
	      Char = sText.charAt(i); 
	      if (ValidChars.indexOf(Char) >= 0 ) 
    	     {
				 NChar+=Char;
	         }else
			 {
				IsNumber=false;
			}
      }
   return NChar;
}


function getXmlHttpObject()
{
    var xmlhttp;

    /*@cc_on
    @if (@_jscript_version >= 5)
    try{
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e){
    try{
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e){
    xmlhttp = false;
    }
    }
	@else
    xmlhttp = false;
    @end @*/

    if (!xmlhttp && typeof XMLHttpRequest != 'undefined'){
        try{
            xmlhttp = new XMLHttpRequest();
        }
        catch (e){
            xmlhttp = false;
        }
    }
    return xmlhttp;
}
// method to send by post

   
function makePOSTRequest(url, parameters) 
{
    http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);//metodo de envio
      http_request.setRequestHeader("Content-type", "text/html"); //application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http.responseText);
            result = http_request.responseText;
            MM_findObj(wheretoreplace).innerHTML = result; 
			finishfunction();
         } else {
            alert('There was a problem with the request.');
         } 
       }
     	if(http_request.readyState == 1)
	{
//		MM_findObj(wheretoreplace).innerHTML = '<img src="images/ajax-loader2.gif" alt="Cargando...">';
	MM_findObj(wheretoreplace).innerHTML =DivRelleno;//'';		
	}
     
   }


function handleHttpResponse()
{
			
		if (http.readyState == 4)
		{
			results = http.responseText;
			var banner=MM_findObj(wheretoreplace);
			banner.innerHTML =  results;
		}
}



function sendQuerystring(div,pagina)
{
    wheretoreplace=div;
	http.open("GET", pagina, true);
	http.setRequestHeader("Connection", "close");
	//alert(MM_findObj('banner').innerHtml);
	MM_findObj(wheretoreplace).innerHtml=DivRelleno;	
	//alert(MM_findObj('banner').innerHtml);
    http.onreadystatechange = handleHttpResponse;
    http.send(null);
}

function getNewText(id,type) 
{
	var Boxtext=type==1?'Toevoegen aan project':'Verwijderen';
	var ExtraUrl=type==1?'showItem':'removeitem';
	return "<a href=\"#\" title=\""+Boxtext+"\" onclick=\"return "+ExtraUrl+"('"+id+"')\">&gt;&gt;&gt;"+Boxtext+"</a>";
}

function verifica_valor(id)
{
	var idAmount=MM_findObj('amo_'+id);
	
	idAmount.value=parseInt(idAmount.value,10);
	idAmount.value=IsNumeric(idAmount.value);
	
		
	if(!IsNumeric(idAmount.value))
	{idAmount.value=0;}
	else
	idAmount.value=parseInt(idAmount.value,10);
	
	
}

function addsc(id)
{
	idAmount=MM_findObj('amo_'+id);
	//if(!IsNumeric(idAmount.value))
	
	
	if(idAmount.value!=0)
	{
	
	if(idAmount.value>0)
			{
			alert('Este producto se ha agregado a su cesta de compra');
			}
	}
	//idSelected.className="item item_selected";
	var pagina="includes/sc_ajax.php?cod_sc="+id+"&ammount="+idAmount.value;
	sendQuerystring('shoppingcart',pagina);
/*	var Text=getNewText(id,2);
	var DivText=MM_findObj('text'+id);
	DivText.innerHTML=Text;*/
	
	return false;	
}

function removeitem(id)
{
	idSelected=MM_findObj('planten'+id);
	idSelected.className="item";
	var pagina="includes/sc_ajax.php?rem=1&cod="+id;
	sendQuerystring('shoppingcart',pagina);
	var Text=getNewText(id,1);
	var DivText=MM_findObj('text'+id);
	DivText.innerHTML=Text;
	return false;	
}
