$BASE_URL           = '';

function checkCapabilities(){
    var xmlHttp2;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp2=new XMLHttpRequest();
    }catch (e){
        // Internet Explorer
        try{
            xmlHttp2=new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e){
            try{
                xmlHttp2=new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e){
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
  return xmlHttp2;
}

function boxLoad( boxName, boxClass, replaceDoc, params ) {
	
    var $xmlHttp    = checkCapabilities();
    var $box        = document.getElementById( boxName );
    var $loadBox
    
    if( boxClass ){
        $loadBox    = boxClass
    }else{
        $loadBox    = 'ajaxLoad';
    }
	
    if( $xmlHttp && $box ){
        
        $xmlHttp.onreadystatechange = function(){
            if( $xmlHttp.readyState < 4 ){
                $box.innerHTML = '<img class="ajaxLoad" src="/images/various/loading.gif" align="center" valign="center" />';
            }
            
            if( $xmlHttp.readyState == 4 ){
				$box.innerHTML = $xmlHttp.responseText;
            }
        }
        
        if( params ) params = '?'+params;
		
        $xmlHttp.open( "GET", $BASE_URL + replaceDoc +  params , true ); 
        $xmlHttp.send(null);
    }
}

function loadValue( $box, replaceDoc, params ) {
    var $xmlHttp    = checkCapabilities();
	
    if( $xmlHttp ){
        $xmlHttp.onreadystatechange = function(){
            if( $xmlHttp.readyState == 4 ){
                document.getElementById($box).value = $xmlHttp.responseText;
            }
        }
		
        $xmlHttp.open( "GET", $BASE_URL + replaceDoc +  params , true ); 
        $xmlHttp.send(null);
    }
}

function boxLoadAction( boxName, boxClass, replaceDoc, params, getFunction ) {

    var $xmlHttp    = checkCapabilities();
    var $box        = document.getElementById( boxName );
    var $loadBox;
    
    if( boxClass ){
        $loadBox    = boxClass
    }else{
        $loadBox    = 'ajaxLoad';
    }
    
    //$loadBox    = 'ajaxLoad';
    //alert($loadBox);
    
    if( $xmlHttp && $box ){
        
        $xmlHttp.onreadystatechange =
        function(){
            
            if( $xmlHttp.readyState < 4 ){
                $box.innerHTML = '<img class="ajaxLoad" src="/images/various/loading.gif" align="center" valign="center" />';
            }
            
            if( $xmlHttp.readyState == 4 ){
                $box.innerHTML = $xmlHttp.responseText;
                getFunction();
            }
            
        }
        
        if( params ) params = '?'+params;
        
        $xmlHttp.open( "GET", $BASE_URL + replaceDoc +  params , true ); 
        $xmlHttp.send(null);
    }
}

function boxLoadPOST( boxName, boxClass, replaceDoc, params ) {

    var $xmlHttp    = checkCapabilities();
    var $box        = document.getElementById( boxName );
    var $loadBox
    
    if( boxClass ){
        $loadBox    = boxClass
    }else{
        $loadBox    = 'ajaxLoad';
    }
    
    if( $xmlHttp && $box ){
        
        $xmlHttp.onreadystatechange =  function(){
            if( $xmlHttp.readyState < 4 ){
                $box.innerHTML = '<img src="/images/various/loading.gif" align="center" valign="center" />';
            }
            
            if( $xmlHttp.readyState == 4 ){
                $box.innerHTML = $xmlHttp.responseText;
            }
        }
        
        $xmlHttp.open( "POST", $BASE_URL + replaceDoc , true );
        $xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        $xmlHttp.setRequestHeader("Content-length", params.length);
        $xmlHttp.setRequestHeader("Connection", "close");
        $xmlHttp.send(params);
    } 
    return;
}

function boxLoadPOSTaction( boxName, boxClass, replaceDoc, params, postFunction ) {

    var $xmlHttp    = checkCapabilities();
    var $box        = document.getElementById( boxName );
    var $loadBox;
    
    if( boxClass ){
        $loadBox    = boxClass;
    }else{
        $loadBox    = 'ajaxLoad';
    }

    if( $xmlHttp && $box ){
        
        $xmlHttp.onreadystatechange = function(){
            if( $xmlHttp.readyState < 4 ){
                $box.innerHTML = '<img class="ajaxLoad" src="/images/various/loading.gif" align="center" valign="center" />';
            }
            
            if( $xmlHttp.readyState == 4 ){
                $box.innerHTML = $xmlHttp.responseText;
                postFunction();
            }
        }
        
        $xmlHttp.open( "POST", $BASE_URL + replaceDoc , true );
        $xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        $xmlHttp.setRequestHeader("Content-length", params.length);
        $xmlHttp.setRequestHeader("Connection", "close");
        $xmlHttp.send(params);
    } 
    return;
}

function boxPOSTxml( replaceDoc, params){
    var $xmlHttp    = checkCapabilities();

    if( $xmlHttp ){
        $xmlHttp.onreadystatechange = function(){
            if( $xmlHttp.readyState == 4 )
				return $xmlHttp.responseXML.documentElement;
        }
        
        $xmlHttp.open( "POST", $BASE_URL + replaceDoc , true );
        $xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        $xmlHttp.setRequestHeader("Content-length", params.length);
        $xmlHttp.setRequestHeader("Connection", "close");
        $xmlHttp.send(params);
    } 
    return;
}

function boxReturnPOST( boxLoad, replaceDoc, params, postFunction ) {
    var $xmlHttp    = checkCapabilities();
	var $box        = document.getElementById( boxLoad );
	var $toReturn   = "";
	
    if( $xmlHttp ){
        
        $xmlHttp.onreadystatechange =  function(){
            if( $xmlHttp.readyState < 4 ){
                $box.innerHTML = '<img class="ajaxLoad" src="/images/various/loading.gif" align="center" valign="center" />';
            }
            
            if( $xmlHttp.readyState == 4 ){
                $box.innerHTML = $xmlHttp.responseText;
				if( postFunction )
					postFunction();
            }		
        }
        
        $xmlHttp.open( "POST", $BASE_URL + replaceDoc , true );
        $xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        $xmlHttp.setRequestHeader("Content-length", params.length);
        $xmlHttp.setRequestHeader("Connection", "close");
        $xmlHttp.send(params);
    } 
    return ;
}

function parseFormParams( formID ){
    var $form       = document.getElementById( formID );
    var $strParams  = '';

    for( $i = 0 ; $i < $form.elements.length ; $i++ ){
        if( $strParams.length ) $strParams += '&'
        
        if( $form.elements[$i].name ){
            switch ( $form.elements[$i].type ){
                case 'checkbox':
                    $strParams += $form.elements[$i].name+'='+($form.elements[$i].checked?'1':'0');
                    break;
                
                case 'radio':
                    $strParams += $form.elements[$i].name+'='+($form.elements[$i].checked?'1':'0');
                    break;
                
                default:
                    $strParams += $form.elements[$i].name+'='+$form.elements[$i].value.replace('&','&amp;'); 
                    break;
            }
        }
    }
    return $strParams;
}

function loadSelect( boxName, replaceDoc, params, show ){
    var $xmlHttp    = checkCapabilities();
    var $box        = document.getElementById( boxName );
	var $atr 		= document.all ? "className" : "class"; 
	
	if( $xmlHttp && $box ){
			
        $xmlHttp.onreadystatechange = function(){
            if( $xmlHttp.readyState == 4 ){
				$result = $xmlHttp.responseText;
					
	            $arr 	= $result.split(";");
					
	            while( $box.hasChildNodes() ){
	                $box.removeChild( $box.lastChild );
	            }
                    
	            $columnOption	= document.createElement('option');
	            $columnOption.setAttribute('value', '');
	            $columnOption.appendChild( document.createTextNode('--- Seleccione ---') );
                $box.appendChild( $columnOption );
                        
                if( show ){
                    $columnOption	= document.createElement('option');
                    $columnOption.setAttribute('value', '0');
                    $columnOption.appendChild( document.createTextNode('Otro') );
                    $box.appendChild( $columnOption );
                }
                        
	            for( $i = 0; $i < ($arr.length-1) ; $i++){
	                $data 			= $arr[$i].split("|");
	                $columnOption	= document.createElement('option');
	                $columnOption.setAttribute('value', $data[0]);
					//$columnOption.setAttribute($atr,'capital');
	                $columnOption.appendChild( document.createTextNode($data[1]) );
	                          
	                $box.appendChild( $columnOption );
	            }     
            }
        }
        
        $xmlHttp.open( "POST", $BASE_URL + replaceDoc , true );
        $xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        $xmlHttp.setRequestHeader("Content-length", params.length);
        $xmlHttp.setRequestHeader("Connection", "close");
        $xmlHttp.send(params);
    } 
    return;
}
