// JavaScript Document
/*
v.1.0
por Victor Ocampo Cicero / webmaster@vicale.com.mx

Ultima modificacion: 25.02.2010

www.vicale.com.mx
*/
//Inicio variables y ambiente
var target = "";
var hand = function(str) 
{ 
	document.getElementById(target).innerHTML = str;
} 

var ajax = new Ajax(); 

function Ajax () {
	this.req = null; 
    this.url = null; 
    this.method = 'POST'; 
    this.async = true; 
    this.status = null; 
    this.statusText = ''; 
    this.postData = null; 
    this.readyState = null; 
    this.responseText = null; 
    this.responseXML = null; 
    this.handleResp = null; 
    this.responseFormat = 'text', // 'text', 'xml', or 'object' 
    this.mimeType = null; 
    
    this.init = function() 
    { 
        // The init method goes through each possible way of creating an 
        // XMLHttpRequest object until it creates one successfully. 
        // This object is then returned to the calling function
        
        if (!this.req)
        { 
            try 
            { 
                // Try to create object for Firefox, Safari, IE7, etc. 
                this.req = new XMLHttpRequest(); 
            } 
            catch (e) 
            { 
                try 
                { 
                    // Try to create object for later versions of IE. 
                    this.req = new ActiveXObject('MSXML2.XMLHTTP'); 
                } 
                catch (e) 
                { 
                    try 
                    { 
                        // Try to create object for early versions of IE. 
                        this.req = new ActiveXObject('Microsoft.XMLHTTP'); 
                    } 
                    catch (e) 
                    { 
                        // Could not create an XMLHttpRequest object. 
                        return false; 
                    } 
                } 
            } 
        } 
        return this.req; 
    };
    
    this.setMimeType = function(mimeType) 
    { 
        this.mimeType = mimeType; 
    };
    
    this.handleErr = function() 
    {         
        var errorWin; 
        try         
        { 
            errorWin = window.open('', 'errorWin'); 
            errorWin.document.body.innerHTML = this.responseText; 
        } 
        catch (e) 
        { 
            alert('An error occurred, but the error message cannot be ' 
                + 'displayed. This is probably because of your browser\'s ' 
                + 'pop-up blocker.\n' 
                 + 'Please allow pop-ups from this web site if you want to ' 
                 + 'see the full error messages.\n' 
                 + '\n' 
                 + 'Status Code: ' + this.req.status + '\n' 
                 + 'Status Description: ' + this.req.statusText); 
        } 
    };
   
    this.abort = function() 
    { 
        if (this.req) 
        { 
            this.req.onreadystatechange = function() {  };  // Remove Handler before change state with req.abort()
            this.req.abort(); 
            this.req = null; 
        } 
    };
    
    // This first part of doReq calls init to create an instance of the XMLHttpRequest class, 
    // and displays a quick alert if it's not successful.
    //
    // States    
    // 0: uninitialized - open has not been called yet. 
    // 1: loading - send has not been called yet. 
    // 2: loaded - send has been called, but the response is not yet available. 
    // 3: interactive - The response is being downloaded, and the responseText property holds partial data. 
    // 4: completed - The response has been loaded and the request is completed. 

    this.doReq = function() 
    { 
        if (!this.init()) 
        { 
            alert('Could not create XMLHttpRequest object.'); 
            return; 
        } 
        
        this.req.open(this.method, this.url, this.async); 
                
        //alert("mimetyped!"+this.mimeType);
		//req.overrideMimeType(this.mimeType);
		if (this.mimeType) 
        { 
            try 
            { 
                req.overrideMimeType(this.mimeType); 
				alert("mimetyped!");
            } 
            catch (e) 
            { 
                // couldn't override MIME type  --  IE6 or Opera? 
            } 
        } 
        var self = this; // Fix loss-of-scope in inner function 
        
        this.req.onreadystatechange = function() 
        { 
            var resp = null; 
            if (self.req.readyState == 4) 
            { 
                switch (self.responseFormat) 
                { 
                    case 'text': 
                                    resp = self.req.responseText; 
                                    break; 
                    case 'xml': 
                                    resp = self.req.responseXML; 
                                    break; 
                    case 'object': 
                                    resp = req; 
                                    break; 
                } 
                
                if (self.req.status >= 200 && self.req.status <= 299) 
                { 
                    self.handleResp(resp); 
                } 
                else 
                { 
                    self.handleErr(resp); 
                } 
            } 
        }; 
        
		//alert (this.postData);
		this.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.req.setRequestHeader("Content-length", this.postData.length);
      	this.req.setRequestHeader("Connection", "close");
        this.req.send(this.postData);
    };
    
    
    this.doGet = function(url, hand, format) 
    { 
        this.url = url; 
        this.handleResp = hand; 
        this.responseFormat = format || 'text'; 
        this.doReq();
    };
}
//Final Inicio de variables y ambiente

function doit (query, where) 
{
	ajax.setMimeType('text/html'); 
	where.innerHTML = "<p align='center'><br><br><br><img src='img/loading.gif' alt='Cargando...' width='32' height='32'></p>";
	ajax.doGet(query, hand);
}

function clear_form(oForm) {

    var elements = oForm.elements;

    oForm.reset();

    for(i=0;i<elements.length;i++) {

        field_type = elements[i].type.toLowerCase();

        switch (field_type) {

            case "text":

            case "password":

            case "textarea":

                elements[i].value = "";

                break;

            case "radio":

            case "checkbox":

                if (elements[i].checked) {

                    elements[i].checked = false;

                } 

                break;

            case "select-one":

            case "select-multi":

                elements[i].selectedIndex = -1;

                break;

            default:

                break;

        } 

    } 

} 

