

function FormatNumber(num, format, shortformat)
{
	if(format==null){
		format = "(###) ###-#### ";			// Telephone w/ Area Code
	}					
	if(shortformat==null)	{
		var shortformat = "";
	}
	var validchars = "0123456789";
	var tempstring = "";
	var returnstring = "";
	var extension = "";
	var tempstringpointer = 0;
	var returnstringpointer = 0;
	count = 0;

	// Get the length so we can go through and remove all non-numeric characters
	var length = num.value.length;

	// We are only concerned with the format of the phone number - extensions can be left alone.
	if (length > format.length){
		length = format.length;
	};
	
	// scroll through what the user has typed
	for (var x=0; x<length; x++){
		if (validchars.indexOf(num.value.charAt(x))!=-1){
			tempstring = tempstring + num.value.charAt(x);
		};
	};
	// We should now have just the #'s - extract the extension if needed
	if (num.value.length > format.length)	{
		length = format.length;
		extension = num.value.substr(format.length, (num.value.length-format.length));
	};
	
	// if we have fewer characters than our short format, we'll default to the short version.
	for (x=0; x<shortformat.length;x++)	{
		if (shortformat.substr(x, 1)=="#"){
			count++;
		};
	}
	if (tempstring.length <= count)	{
		format = shortformat;
	};
	//Loop through the format string and insert the numbers where we find a # sign
	for (x=0; x<format.length;x++){
		if (tempstringpointer <= tempstring.length){
			if (format.substr(x, 1)=="#"){
				returnstring = returnstring + tempstring.substr(tempstringpointer, 1);
				tempstringpointer++;
			}else{
				returnstring = returnstring + format.substr(x, 1);
			}
		}
	}
	// We have gone through the entire format, let's add the extension back on.
	returnstring = returnstring + extension;
	//we're done - let's return our value to the field.
	num.value = returnstring;
}	


function loadXML(sXML){
	//load xml file
	// code for IE
	if (window.ActiveXObject){
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async=false;
		xmlDoc.loadXML(sXML);
//		getmessage();
	}
	// code for Mozilla, etc.
	else if (document.implementation && document.implementation.createDocument){
		var domParser = new DOMParser();
		xmlDoc = domParser.parseFromString(sXML, "text/xml"); 
	}
	else{
		alert('Your browser cannot handle this script');
	}
	return xmlDoc;
}


function XMLtoString(xmlNode) {
	var sText = false;

	if (window.ActiveXObject){
		sText = xmlNode.xml;
	}
	else{
		var serializer = new XMLSerializer();
		sText = serializer.serializeToString(xmlNode);
	}
	return sText;
}

function writeImageButton(sSrc,sFunction,sTit,tbi,ID,sOnKey){
	document.write( imageButton(sSrc,sFunction,sTit,tbi,ID,sOnKey) );
}

function imageButton(sSrc,sFunction,sTitle,tbi,ID,sOnKey){
	var dDate = new Date();
	var sImgID = "Img_"+dDate.getTime()+"_"+(IDFORM++);
	var sObj = "document.getElementById('"+sImgID+"')";
	var sOnOver = " onmouseover = \""+sObj+".style.border='1px solid'\"  onfocus = \""+sObj+".style.border='1px solid'\" ";
	var sOnOut = " onmouseout = \""+sObj+".style.border='0px solid'\"  onblur = \""+sObj+".style.border='0px solid'\" ";
	var sOnKeyUp = " ";
	if( sOnKey != undefined && sOnKey != "" ){
		sOnKeyUp = " onkeydown=\"if (event == null) event = window.event;var edit = event.srcElement ? event.srcElement:event.originalTarget;var kc=event.charCode ? event.charCode : event.which ? event.which : event.keyCode;if(kc==9) return  "+sOnKey+".firstFocus();\" ";
	}
	sRet = "<a ";
	sRet += "HREF=\"javascript:"+sFunction+"\" ";
	sRet += "TITLE=\""+sTitle+"\" ";
	sRet += " tabIndex="+tbi+" "
		+" id=\""+ID+"\" "+sOnOver+sOnOut+sOnKeyUp+" "
		+" >";
	sRet += "<IMG SRC=\""+sSrc+"\" "
		+" BORDER=0  ID=\""+sImgID+"\" >";
	sRet += "</a>";
	return sRet;
}

function DBDateToString(sDate){
	if(sDate.length == 8){
		sRet = sDate.substring(6,8) + "/" + sDate.substring(4,6) + "/" + sDate.substring(0,4);
		return sRet;
	}
	else if (sDate.length == 10){
		sRet = sDate.substring(8,10) + "/" + sDate.substring(5,7) + "/" + sDate.substring(0,4);
		return sRet;
	}
	else	return "";
}

function insertDebug(sText){
	document.getElementById("Main_debug").innerHTML = " ::_ " + sText;
}


function centerApp( oForm ){
	var iWMax = screen.availWidth;
	var iHMax = screen.availHeight;
	var iFW = oForm.offsetWidth;
	var iFH = oForm.offsetHeight;
	var iX = ( iWMax / 2 ) - ( iFW / 2 );
	var iY = ( ( iHMax / 2 ) - ( iFH / 2 ) ) - ( iFH / 2 );
	oForm.style.left = iX;
	oForm.style.top = iY;
}

function startApp(sForm){
	frmMenu_hideSM();
	if(sForm == "Salir"){
		var sYes = confirm("Esta seguro que desea salir del sistema?");
		if( sYes == true ) 
			window.close();
		return;
	}
	var objForm = eval("new "+sForm+"()");
	maximizar(sForm+"Div");
	centerApp( eval(sForm+"Div") );
	objForm.init();
}


function writeFormHeader(sFrmName, sTitle, iTam, sVisibility,iH){
	var sText = "<style>"
		+"#"+sFrmName+"{ "
		+"position:absolute;  "
		+"left:0; top:0;  "
		+"background-color:#EFF0E6; "
		+"color:#000000; "
		+"width:"+iTam+"; "
		+"height:"+iH+"; "
		+"padding:0 0 0 0; "
		+"border:1px solid #AAAAAA; "
		+"visibility:"+sVisibility+"; "
		+"} "
		+"#"+sFrmName+"_form{ "
//		+"background-color:   #B4C6D8; "
//		+"background-color:   #EFF0E6; "
		+"background-color:   #FFFFFF; "
		+"border:1px solid #B4C6D8; "
		+"padding:5;"
		+"height:95%; "
		+"border-style:solid;"
		+"border-top-color:#B3B3B3;"
		+"border-bottom-color:#C1D6EB;"
		+"border-left-color:#B3B3B3;"
		+"border-right-color:#C1D6EB;"
		+"border-width:1px;"
		+"} "
		+"#"+sFrmName+"_title{ "
		+"cursor:move;  "
//		+"background-color:#B4C6D8; "
//		+"background-color:#0066AA; "
		+"background:URL('Icons/barn.png'); "
		+"color:#FFFFFF; "
		+"font-size:12px; "
		+"height:23; "
		+"width:100%;	 "
		+"z-index:5; "
		+"border:0px solid #EEEEEE;"
		+"} "
		+"#"+sFrmName+"_status{ "
		+"font-size:10px; "
		+"width:100%; "
		+"} "
		+"#"+sFrmName+"_resize{ "
		+"cursor:nw-resize; "
		+"} "
		+"</style> "
		+"<div id=\""+sFrmName+"\" class='dragableElement'> "
/*		+"<b class=\"rtop\"><b class=\"r1_0\"></b><b class=\"r2\"></b><b class=\"r3\"></b><b class=\"r4\"></b></b> "*/
		+"<table width=100% cellpadding=0 cellspacing=0 border=0><TR><TD>"
		+"<div id=\""+sFrmName+"_title\" drageable=\"true\"> "
		+"<b>&nbsp;&nbsp;"+sTitle+"</b> "
		+"</div> "
		+"</TD>"
		+"<td width=1% nowrap class=\"frmStatus0\">"
		+imageButton("Icons/min_down.gif", "minimizar('"+sFrmName+"','"+sTitle+"')","Minimizar Ventana")
		+imageButton("Icons/close_down.gif", sFrmName.replace("Div","")+"Obj.close();","Cerrar Ventana")
/*		+"<input type=image id=bMin src=\"Icons/gtk-min.png\" "
		+"onclick='minimizar(\""+sFrmName+"\",\""+sTitle+"\")' "
		+"title='Minimizar ventana' >"
		+"<input type=image src=\"Icons/gtk-close.png\" onclick='"+sFrmName.replace("Div","")+"Obj.close();'  title='Cerrar ventana'>"*/
		+"&nbsp;</td>"
		+"</TR></table>"
		+"<div id=\""+sFrmName+"_form\" > "; 
	document.writeln(sText);
	sText = document.getElementById("taskDiv").innerHTML;
	sText += "<div id=\""+sFrmName+"_min\" style=\"display:inline;background-color:#B4C6D8;border:1px solid #8496A8;color:#FFFFFF;cursor:pointer; background:URL('Icons/barn.png'); \" onclick=\"maximizar('"+sFrmName+"')\" >";
//	sText += "<div id=\""+sFrmName+"_min\" style=\"display:table-cell;background-color:#B4C6D8;border:1px solid #8496A8;color:#FFFFFF;cursor:pointer;background:URL('Icons/barn.png');\" onmouseover=\"this.style.background='#A6B8CA' \" onmouseout=\"this.style.background='URL(\'Icons/barn.png\')' \"  onclick=\"maximizar('"+sFrmName+"')\" valign=top>";
//	sText += "<b>"+sTitle+"</b>";
	sText += "</div>";
	document.getElementById("taskDiv").innerHTML = sText;
	document.getElementById(sFrmName+"_min").style.visibility = 'hidden';
}

function minimizar(sForm,sTitle){
	document.getElementById(sForm+"_min").innerHTML = "<table align=left><TR><TD background='Icons/barn.png' nowrap>"
		+imageButton("Icons/restore_down.gif", "maximizar('"+sForm+"')","Restaurar Ventana")
		+"</TD><TD background='Icons/barn.png'>"
		+"<b>"+sTitle+"</b>&nbsp;&nbsp;"
		+"</TD></TR></table>";

	document.getElementById(sForm+"_min").style.visibility = 'visible';
	sForm = sForm.replace("Div","");
	eval(sForm+"Obj.close()");
//	document.getElementById(sForm).style.visibility = 'hidden';
}

function maximizar(sForm){
	document.getElementById(sForm+"_min").innerHTML ="";
	document.getElementById(sForm+"_min").style.visibility = 'hidden';
	document.getElementById(sForm).style.visibility = 'visible';
	document.getElementById(sForm).style.zIndex = INDEXDEPTH++;
}

function setEnabled( objID, bVal ){
	if( bVal ){
		document.getElementById(objID).style.display = "inline";
	}
	else{
		document.getElementById(objID).style.display = "none";
	}
}

function writeFormFoot(sFrmName, bResize){
	var sText = "</div> "
		+"<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"frmStatus\" width=\"100%\"> "
		+"<TR> "
		+"<TD class=\"frmStatus\"> "
		+"<div id=\""+sFrmName+"_status\" class=\"frmStatus\">::</div> "
		+"</TD> "
		+"<TD width=\"1\" valign=\"botton\"> ";
	if( bResize ){
		 sText += "<div id=\""+sFrmName+"_resize\" resizer=\"true\"> "
			+"<img border=0 src=\"dis_resizese.png\" id=\""+sFrmName+"_resize\"> "
			+"</div> ";
	}
	else{
		 sText += "";
	}
	sText += "</TD> "
		+"</TR> "
		+"</table> "
/*		+"<b class=\"rbottom\"><b class=\"r4\"></b><b class=\"r3\"></b><b class=\"r2\"></b><b  class=\"r1\"></b></b> "*/
		+"</div> ";
	document.writeln(sText);
}

function insertStatus(sFrmName, sTxt, sType){
	if(sType == "")
		document.getElementById(sFrmName+"_status").innerHTML = ":: " + sTxt;
}


function oElm(sObjName){
	return document.getElementById(sObjName);
}


function writeFormDiv(sFrmName, sTitle, iTam, sVisibility,iH){
	writeFormHeader(sFrmName, sTitle, iTam, sVisibility,iH);
	writeFormFoot(sFrmName, sTitle);
}

function lyBusy(){
	this.start = function(){
		var iBottom = document.body.clientHeight;
		document.getElementById("lyBusyDiv").style.top = iBottom - 60;
		document.getElementById("lyBusyDiv").style.visibility = "visible";
	}
	this.stop = function(){
		document.getElementById("lyBusyDiv").style.visibility = "hidden";
	}
	return this;
}

function addFormTab(url, sName, bClose){
	var r = SOAPClient._getURL(url);
	xmlDoc = loadXML(r);
	document.getElementById("divCatContentTitle0").innerHTML = sName;
	document.getElementById("divCatInnerContent").innerHTML = XMLtoString(xmlDoc.firstChild.childNodes[1].childNodes[0]);
	for( var f = 0 ; f < xmlDoc.firstChild.childNodes[0].childNodes.length ; f++){
		if(window.execScript){
			window.execScript(xmlDoc.firstChild.childNodes[0].childNodes[f].childNodes[0].nodeValue);
		}
		else {
			this.eval(xmlDoc.firstChild.childNodes[0].childNodes[f].childNodes[0].nodeValue);
		}
	}
}

function completaCeros(x,n) {
	alert(x);
	if( x.length <= 0 ) return "";
    sRet = x;
    while( sRet.length < n )
        sRet = "0"+sRet;
    return sRet;
}


function agregaMosca(idMos){
	var url = "addMosca.php";
	var pl = new SOAPClientParameters();
	pl.add("idMos",idMos);
	var r = SOAPClient._getURL(url,pl);
	oElm('cantMenuContact').innerHTML = " <b>("+r+")</b>";
	oElm('cantImgContact').innerHTML = " <b>(Cargada en el formulario de contacto "+r+")</b>";
}

