
function XmlRequest(method, url, parameters, onComplete, onError) {
	var xmlHttp;
	
	if ((xmlHttp = createXMLHttp()) != null) {
		xmlHttp.onreadystatechange = function () { onReadyStateChange(xmlHttp, onComplete, onError); }
		xmlHttp.open(method, url, true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlHttp.send(parameters);
	} else {
		alert("A required object, XMLHttpRequest is not found!");
	}
}

function createXMLHttp() {
	var xmlHttp;
	
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			xmlHttp = new XMLHttpRequest();
        } catch(e) {
			xmlHttp = null;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		xmlHttp = null;
        	}
		}
    }
    return xmlHttp;
}

function onReadyStateChange (xmlHttp, onComplete, onError) {
	// only if request is completed
	if (xmlHttp.readyState == 4) {
		// only if "OK"
		if (xmlHttp.status == 200) {
			if(onComplete) onComplete(xmlHttp);
		} else {
			if(onError) onError(xmlHttp);
		}
	}
}

function ModalDialog() {
}

ModalDialog.current = null;
ModalDialog.simulation = (window.showModalDialog) ? false : true;

ModalDialog.show = function(url, width, height) {
	var features;

	if (ModalDialog.simulation){
		if (ModalDialog.current) {
			ModalDialog.current.focus();
			return;
		}

		var left, top;
		
		left = (screen.availWidth - width)/2;
		top = (screen.availHeight - height)/2;

		features = "toolbar=no,location=no,directories=no,menubar=no,status=no,scrollbars=no,resizable=no,modal=yes";
		features += ",outerHeight=" + height;
		features += ",outerWidth=" + width;
		features += ",left=" + left;
		features += ",top=" + top;
		
		ModalDialog.current = window.open(url, "modalDialog", features);
		ModalDialog.onLoad();
	} else {
		features = "center:yes;help:no;resizable:no;status:yes;scroll:no";
		features += ";dialogWidth:" + width + "px";
		features += ";dialogHeight:" + height + "px";

		window.showModalDialog(url, window, features);
	}
}

ModalDialog.onLoad = function() {
	ModalDialog.windowOnFocus = window.onfocus;
	window.onfocus = ModalDialog.holdFocus;
}

ModalDialog.onUnload = function() {
	if (!ModalDialog.simulation) return;
	
	var parentWindow = window.opener;
	
	if (parentWindow) {
		parentWindow.onfocus = ModalDialog.windowOnFocus;
		parentWindow.ModalDialog.current = null;
	}
}

ModalDialog.holdFocus = function() {
	ModalDialog.current.focus();
	return false;
}

ModalDialog.getParent = function() {
	var parent;
	
	if (ModalDialog.simulation){
		parent = window.opener;
	}else{
		parent = window.dialogArguments;
	}

	return parent;
}

function PopUp(url, width, height) {
	var features;
	var left, top;
		
	left = (screen.availWidth - width)/2;
	top = (screen.availHeight - height)/2;

	features = "toolbar=no,location=no,directories=no,menubar=no,status=no,scrollbars=yes,resizable=yes";
	features += ",outerHeight=" + height;
	features += ",outerWidth=" + width;
	features += ",left=" + left;
	features += ",top=" + top;
		
	window.open(url, "PopUp", features);
}

function PopUpAyudaBancomer(url) {
	if(document.layers){
		var w = 600;
		var h = 485;
	}
	else if(document.all){
		var w = 450;
		var h = 413;
	}
	else if(!document.all && document.getElementById){
		var w = 450;
		var h = 425;
	}

	var l = (screen.width - w) / 2.0;
	var t = (screen.height - h) / 2.0;

	window.open(url,"Ayuda","width=510,height=440,left=" + l + ",top=" + t +",fullscreen=no,channelmode=no,toolbar=no,menubar=no,status=no,directories=no,location=no,scrollbars=yes,resizable=no")
}

function formatSize(size) {
	var units = ["B", "KB", "MB", "GB", "TB"];
	var unitDecimals = [0, 0, 2, 2, 2];

	var unitsCount = units.length - 1;
	
	if (size == "") return "";
	
	size = (+size); //convert to number
	
	var i = 0;
	while (size >= 1024 && i < unitsCount) {
		size /= 1024;
        i++;
    }

    return size.toFixed(unitDecimals[i]) + " " + units[i];
}

function formatName(name) {
	if (name.length > 47)
		return name.substr(0, 47) + "...";
	else
		return name;
}

function trim(s) {
	// Remove leading spaces and carriage returns
	while (s.substring(0,1) == ' ')
		s = s.substring(1,s.length);
     
	// Remove trailing spaces and carriage returns
	while (s.substring(s.length-1,s.length) == ' ')
		s = s.substring(0,s.length-1);
     
	return s;
}

function createUniqueID() {
	return (new Date()).getTime() + "" + Math.floor((Math.random()*8999)+1000);
}

function getClassName(obj)
{
	if (obj && obj.constructor && obj.constructor.toString)
	{
		var arr = obj.constructor.toString().match(/function\s*(\w+)/);
		return arr && arr.length == 2 ? arr[1] : undefined;
	}
	else
	{
		return undefined;
	}
}
