/* SARENA */
var isIE5=(window.navigator.appName=="Microsoft Internet Explorer"&&window.navigator.appVersion.substring(window.navigator.appVersion.indexOf("MSIE")+5,window.navigator.appVersion.indexOf("MSIE")+8)==5.0);
if (isIE5) {
	var undefined = 'undefined';
}
/* SARENA */

function isArray(a){
    if ( a.constructor ) {
 		//alert ("Constructor" + a.constructor.toString().match(/array/i));
		return (typeof(a) == 'object') && a.constructor == Array;
    } else {
    	try {
        	return (a.length > 0);
      	} catch(e) {
        	return false;
      	}
    }
}
  

/* Funciones para DialogModal */
function OpenDialogModal(URL,param,TargetForm,TargetField,DoSubmit) {
  // URL: ruta y nombre de la pagina popup
  // param: debe venir de la forma param1=valor1&param2=valor2...
  // TargetForm: nombre del form destino
  // TargetField: nombre del campo que está en TargetForm y se le asignará el resultValue
  // DoSubmit: 1: Hacer submit. 0: No hacer submit
  var URLFull = URL + "?" + param;
  arguments = window.showModalDialog(URLFull,"Mensaje","dialogHeight: 160px; dialogWidth: 450px; dialogTop: 100px; dialogLeft: 50px; edge: Raised; center: Yes; help: No; resizable: No; status: No;");

  if ((arguments) && (arguments!="") && (TargetField) && (TargetForm)) {
    eval("document." + TargetForm + "." + TargetField).value = arguments;
    if (DoSubmit)
      eval("document." + TargetForm).submit();
  }
  return;
}

/* ----------------------------- */

function jsTrim(sf_string){
	if (sf_string!=undefined){
		sf_string=sf_string.replace(/ +/g," ");
		sf_string=sf_string.replace(/^ /,"");
		sf_string=sf_string.replace(/ $/,"");
	}
	return(sf_string);
}


function textCounter(field, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
}

/* Super trim de strings */
function replace(str,text,by) {
// Replaces text with by in string
    var strLength = str.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return str;

    var i = str.indexOf(text);
    if ((!i) && (text != str.substring(0,txtLength))) return str;
    if (i == -1) return str;

    var newstr = str.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(str.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function pString(sf_string){
	sf_string = sf_string.replace(/ +/g," ");
	sf_string = sf_string.replace(/^ /,"");
	sf_string = sf_string.replace(/ $/,"");
	sf_string = replace(sf_string,",","&#130;");
	sf_string = replace(sf_string,"\'","'");
	sf_string = replace(sf_string,"\"","&#148;");
	return(sf_string);
}

function unpString(sf_string) {
    sf_string = replace(sf_string,"&#148;","\"");
    sf_string = replace(sf_string,"&#130;",",");
    return sf_string;
}

//Popup Generico

function dopopup(url, title, tool, loc, dir, stat, menu, scroll, resize, w, h, l, t)
		{
		var NewWin = window.open(url,title,"modal=yes, toolbar="+tool+",location="+loc+",directories="+dir+",status="+stat+",menubar="+menu+",scrollbars="+scroll+",resizable="+resize+",width="+w+",height="+h+",left="+l+",top="+t);
		NewWin.focus();
		}

//Validacion de campo requerido
function dorequerir(f, v, message){
	if (jsTrim (f.value) == ''){
		alert(v + " " + message);
        f.focus();
        return false;
	}else{
		return true;
	}
}

// valida si el string es un email
function isMail(string){
	if (string == ""){
		return(false);
	}else{
		valor = string;
		largo = valor.length;
		x = 0;
		y = 0;
		for(i=0; i<largo; i++){
			control = valor.substring(i,i+1);
			if (control == "@"){
				x = i;
			}
			if (control == " "){
				y = 1;
			}
		}
		if (x == 0 || y == 1){
			return(false);
		}else{
			valor1 = valor.substring(x + 1, valor.length);
			largo = valor1.length;
			x = 0;
			for (i=0; i<largo; i++){
				control = valor1.substring(i,i+1);
				if (control == "."){
					x = i;
				}
			}
			if (x == 0){
				return(false);
			}else{
				if ((x+1) == largo){
					return(false);
				}else{
					return(true);
				}
			}
		}
	}
}
function changespan(name, data){
	obj=eval(name);
	if (document.layers) {
		document.layers.obj.document.write(data);
		document.layers.obj.document.close();
	}
	else {
		if (document.all) {
			obj.innerHTML = data;
	    }
	}
}

function checkXML(){
  /*<%
    if ( (SESSION("USER_BROWSER") = "IE5") OR (SESSION("USER_BROWSER") = "IE55") OR (SESSION("USER_BROWSER") = "IE6") OR (SESSION("USER_REAL_BROWSER") = "IE7")) then
  %>*/
   if (false) {
	   try{
			objDOM = new ActiveXObject("Msxml2.DOMDocument.4.0");		
	   }
	   catch (e){
	   		/*<%
			if (SESSION("USER_REAL_BROWSER") = "IE7") then
			%>*/
				document.write("Por favor, instalá <a href='msxml4.cab'>este</a> archivo. GRACIAS!!");
			/*<%
			 else
			%>*/ 
				document.write("<object id=\"MSXML4\" classid=\"clsid: 88d969c0-f192-11d4-a65f-0040963251e5\" codebase=\"msxml4.cab\" type=\"application/x-oleobject\" STYLE=\"display: none\"></object>");
			//<% end if %>
	   }
   }
   /*<%
    end if
   %>*/
}


//funcion utilizada para determinar en los custom fields
//en el momento que cambia el valor de un combo, si debe ocultarse o no ocultarse algún grupo
function VerifyGroupHide(obj) {
    var arrGroups, objGroup;
    var GroupsIds = eval("GroupsManager"+obj.name); //los grupos que maneja este select para ocultar o mostrar
    if(GroupsIds) { //si el objeto existe
        if(GroupsIds!="") //si hay algún grupo que maneja este select
        { 
			//muestro todos los grupos
		    var GroupHide;
			var GroupSearch;
			
	    	if(obj.type == "select-one")
	    	{
	    		if(obj.selectedIndex)
	    		{
					GroupHide = obj.item(obj.selectedIndex).getAttribute("grouphide"); //si es un combo
				}
				else
				{
					GroupHide = GroupsIds; //Hide all groups if nothing is selected
				}
			}
			else
			{
				GroupHide=obj.getAttribute("grouphide"); //si es un input porque el campo es de solo lectura
			}
            
            arrGroups = GroupsIds.split(",");
            if(GroupHide!=null && GroupHide!="") {
                GroupSearch = "," + GroupHide + ",";                
            } else {
                GroupSearch = "";
            }

            for (var i = 0; i<arrGroups.length;i++) {
                if(GroupSearch.indexOf("," +arrGroups[i] + ",")<0) { //si este ítem no tiene que ocultarse
                    objGroup = document.getElementById("Group"+arrGroups[i]);
                    if(objGroup) {
                        objGroup.style.display="";
                    }
                }
            }
            
            
            //oculto los que corresponden a este ítem
            try{
                if(GroupHide!=null && GroupHide!="") {
                    arrGroups=GroupHide.split(",");
                    for(i = 0; i<arrGroups.length;i++) {
                        objGroup = document.getElementById("Group"+arrGroups[i]);
                        if(objGroup) {
                            objGroup.style.display="none";
                        }
                    }
                }
            } catch(er) {}
        }
    }
}

function OcultarGruposCustomFields() {
    for(var i = 0;i<arrCombosCustomFields.length;i++) {
        VerifyGroupHide(document.getElementById(arrCombosCustomFields[i]));
    }
}

function LeerAJAX(str) {
    var xmlhttp;
	if (window.XMLHttpRequest) {		
		xmlhttp=new XMLHttpRequest();	
	} 
	else if(window.ActiveXObject) {		
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");	
	}
	xmlhttp.open("GET", str, false);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	xmlhttp.send('');
	return xmlhttp.responseText;
}


/*
funciones de imagen que sirven para hacer preview de la imagen actual, y eliminarla en caso de ser necesario
*/
function previewImage(imagenpreview, cambiaimagen, imageneliminar, imagenselected, imagendefault, objImage) {
    var imgURL = 'file:///' + objImage.value;

    imagenpreview.src = imgURL;

	imagenpreview.onerror = function validImage(){
										this.src = imagendefault;
									 }
	if(!imageneliminar) {
		if(jsTrim(imgURL) != ""){
			imagenpreview.src = imgURL;
    		imagenpreview.style.display="";
		} else {
			imagenpreview.src = imagendefault;
			imagenpreview.style.display = "none";
		}
		imagenpreview.hspace = "10";
		imagenpreview.vspace = "20";
    }

	if(jsTrim(imgURL) != "") {
		imagenselected = imgURL;
	} else {
		imagenselected = imagendefault;
	}
	cambiaimagen.value = 1;
}

function eraseIMG(imagenpreview, cambiaimagen, imageneliminar, valbool) {
	if (valbool) {
		imagenpreview.style.display="none";
	} else {
		imagenpreview.style.display="inline";
	}
	cambiaimagen.value = 1;
	imageneliminar = valbool;
}
