// JavaScript Document
function HTTPRequest( file ) {
    
	var xmlhttp = false;
    
	this.AjaxFailedAlert = "Your brawser don't suport Javascript.\n";
    this.requestFile = file;
    this.encodeURIString = true;
    this.execute = false;
	
    if ( window.XMLHttpRequest ) { 
        this.xmlhttp = new XMLHttpRequest( );
		
        if ( this.xmlhttp.overrideMimeType ) {
            this.xmlhttp.overrideMimeType( 'text/xml' );
        }
		
    } 
	
    else if ( window.ActiveXObject ) { // for IE
 
 		try {
        	this.xmlhttp = new ActiveXObject( "Msxml2.XMLHTTP" );
		}
		catch ( e ) {
			try {
				this.xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
            } catch ( e ) {
                this.xmlhttp = null;
            }
        }
		
        if ( !this.xmlhttp && typeof XMLHttpRequest!='undefined' ) {
            this.xmlhttp = new XMLHttpRequest( );
            if ( !this.xmlhttp ){
                this.failed = true; 
            }
        } 
    }
    return this.xmlhttp ;
}

/*
	page = _pagina 
	getData = valorget
	postData = valorpost
	holder = capa
*/

function requestInto( page, getData, postData, holder, FcBack ) { 
	
	var params;
	var string = "";
	var aux = 0;
	
	if( holder )
		holder = toObject( holder );
	else
		return;
		
	if ( getData != null || postData )	{
		params = ( postData ? postData : getData );
		for( var obj in params ){
			aux++;
			if( typeof( params[obj] ) )
				string += objectToString( obj, params[obj] );
		}
	}
	
	if( !page )
		page = "web/services/index.php";
		
	ajax = HTTPRequest ( page );
	
	if( postData != "" ){
		ajax.open("POST", page + "?tiempo=" + new Date().getTime() + "" + string, true );
	} else {
		ajax.open("GET", page + "?tiempo=" + new Date().getTime() + "" + string, true );
	}
	
	ajax.onreadystatechange=function( ) {
	
		if ( ajax.readyState == 1 )
			holder.innerHTML = "<img src='web/images/loading.gif' width='10' align='center'> Load Data...";
		
		if ( ajax.readyState == 4 ) {
			if( ajax.status == 200 )	{
				holder.innerHTML = ajax.responseText;
				sethtml ( ajax.responseText );
			}else if( ajax.status == 404 )
				holder.innerHTML = "La direccion no existe";
			else
				holder.innerHTML = "Error: "+ ajax.status;
			
			if ( FcBack )	{
				var rParams = ( postData ? postData : getData );
				eval( FcBack )( ajax.status, rParams );
			}
		}
	}
	
	if( postData != "" )	{
		ajax.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded" );
		ajax.send( string );
	}
	else
		ajax.send( null );
}

function sethtml( content ) { 
    var search = content; 
    var script; 
          
    while( script = search.match(/(<script[^>]+javascript[^>]+>\s*(<!--)?)/i)) 
    { 
      search = search.substr(search.indexOf(RegExp.$1) + RegExp.$1.length); 
       
      if (!(endscript = search.match(/((-->)?\s*<\/script>)/))) break; 
       
      block = search.substr(0, search.indexOf(RegExp.$1)); 
      search = search.substring(block.length + RegExp.$1.length); 
       
      var oScript = document.createElement('script'); 
      oScript.text = block; 
      document.getElementsByTagName("head").item(0).appendChild(oScript); 
    } 
}
