function agregarAFavoritos() {
	if (window.sidebar) {
		// Mozilla Firefox Bookmark
		//alert("FIREFOX!");
		window.sidebar.addPanel(document.title, location.href,"");
	} else if( window.external ) {
		// IE Favorite
		//alert("YES IE");
		window.external.AddFavorite( location.href, document.title);
	}	else if(window.opera && window.print) {
		// Opera Hotlist
		//return true; 
	}
}

function seleccionados() {

	if (!OneChecked()) {
		alert("Debe seleccionar al menos un articulo");
		return (false);
	}
	document.form0.varchecks.value = "";
		
	var aaa = VerChecks();
	
	direccion="<?echo($actupath);?>" + direccion;
	document.form0.action = direccion;
	document.form0.submit();

}

function VerChecks() {
	
	ml = document.form0;
	len = ml.elements.length;
	
	for(var i = 0 ; i < len ; i++) {
		if ((ml.elements[i].name == "chkpp") && (ml.elements[i].checked)) {
			document.form0.varchecks.value = document.form0.varchecks.value + ml.elements[i].value + ",";
		}
	}
	return (true);
	
}

function OneChecked(){

	ml = document.form0;
	len = ml.elements.length;
	
	for(var i = 0 ; i < len ; i++) {
		if ((ml.elements[i].name == "chkpp") && (ml.elements[i].checked)) {
			return true;
		}
	}
	return false;
	
}

function Redondear(dblTemp){
	
	var strTempo = Math.round(dblTemp*100)/100 ;	
	return(Redondear2(strTempo));
}

function Redondear2(dblTemp){

	var intPosEsta 	= 0 ;
	var intLen 	= 0 ;
	var intFin 	= 0 ;
	var strTempo 	= "" ;
	
	strTempo = "" + dblTemp + "" ;
	
	intPosEsta = strTempo.indexOf(".");

	if(intPosEsta>0){
		intPosEsta += 1;
		intLen = strTempo.length;
		intFin = intPosEsta + 2 ;
		
		if(intLen < intFin){
			intFalta = intFin - intLen;
			for(i=0;i<intFalta;i++){
				strTempo = strTempo + '0' ;
			}
		}else{
			strTempo = strTempo.substring(0,intFin);
		}
	}else{
		strTempo = strTempo + '.00' ;
	}
	return(strTempo);
	
}

function FinalizarCarga(intForm,strPage){
	var TheForm 	= document.forms[intForm];
	TheForm.action 	= "" + strPage + "" ;
	TheForm.submit();
}

function SeguirCargando(nameForm,strPage){
	var TheForm = eval("document." + nameForm);
	TheForm.action 	= "" + strPage + "" ;
	sSubmitear(nameForm);
}

function CerrarWindows(){
	parent.close();
}

function validaMail(nameForm){
	
	TheForm = eval("document." + nameForm);
				
	var b=/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/
	
	if (eval(TheForm.strEmail.value.length)){    
	  if(! b.test(TheForm.strEmail.value)){
	  	alert("Dirección de Email inválida. Verifíquela.");
	  	return false;	  	
	  };   
	};
	return true;   
	      
}

function hacerSubmit(nameForm){
	var TheForm;	
	TheForm = eval("document." + nameForm);
	TheForm.submit();	
}

function registrar(nameForm){
	
	if (!validaMail(nameForm)){
		//alert("Dirección de Email inválida. Verifíquela.");				
	}else{
			TheForm = eval("document." + nameForm);
			
			if(isRegistrar(TheForm)){
				TheForm.submit();
			};
		};
		
}

function isRegistrar(TheForm){	
	
	var strValue = "" ;
	var dblTemp = 0 ;
	var dbltempo = 0 ;
	var strError = "";
	var strTitulo = "";
	var strTituloSimple = "El siguiente campo debe completarse:\n";
	var strTituloCompuesto = "Los siguientes campos deben completarse:\n";

	var i = 0 ;
	var cont = 0;
	var j= 0;
	
	if (TheForm.strPass.value != TheForm.strPass2.value){
		if(dbltempo==0){
		strError+="\n Las Claves no son iguales";
		cont++;
		};
	};
	
	for (i=0; i<TheForm.elements.length; i++) {
	
		if (TheForm.elements[i].type == "text" || TheForm.elements[i].type == "password" ){
			dbltempo = eval(TheForm.elements[i].value.length);
	  	} 

		if (TheForm.elements[i].name == "strUser"){
			if(dbltempo==0){
			strError+="\n Usuario";
			cont++;
			};
		};

		if (TheForm.elements[i].name == "strPass"){
			if(dbltempo==0){
			strError+="\n Clave";
			cont++;
			};
		};
		
		if (TheForm.elements[i].name == "strNombre"){
			if(dbltempo==0){
			strError+="\n Nombre";
			cont++;
			};
		};
		if (TheForm.elements[i].name == "strApellido"){
			if(dbltempo==0){
			strError+="\n Apellido";
			cont++;
			};
		};
		if (TheForm.elements[i].name == "strEmail"){
			if(dbltempo==0){
			strError+="\n Email";
			cont++;
			};
		};
		
		if (TheForm.elements[i].name == "strUser") {
		
			erroneos = "áíóúñéÑ/ÁÉÍÓÚ: ";
			cant = erroneos.length;
			usuario = TheForm.strUser.value;

			for (j=0 ; j < cant ; j++) {
   			erroneo = erroneos.charAt(j);
				if (usuario.indexOf(erroneo) != -1) {
					strError+="\n El nombre de usuario contiene caracteres inválidos.";
					cont++;
				};
			};
		};
	};
		
	if(cont!=0){
		if(cont==1){
		strTitulo = strTituloSimple + strError ;
		}else{
		strTitulo = strTituloCompuesto + strError ;
		};
		alert(strTitulo);
		return(false);
	};
	return(true);
	
}

function dblPonerTotal(intForm){
	var TheForm 	= document.forms[intForm];
	var strValue 	= "" ;
	var dblTemp 	= 0 ;
	var dbltempo 	= 0 ;
	var iCont	= 0 ;
	var strTempo	= "" ;
	
	for (i=0; i<TheForm.elements.length; i++) {
		if (TheForm.elements[i].type == "text"){
			strValue = TheForm.elements[i].name.substring(0,11);

			if (strValue == "dblSubtotal"){
				dbltempo = eval(TheForm.elements[i].value) ;
				dblTemp	= eval(dblTemp + dbltempo) ;
			}
		}
	}

	strTempo = Redondear(dblTemp);	
	TheForm.dblTotal.value="" + strTempo + "";

}

function ponerValorSubtotal(intForm,intCopias,dblPrecios,intImg){
	
	var TheForm 	= document.forms[intForm];
	var dblTemp 	= 0 ;
	var dbltempo 	= 0 ;
	var strTempo 	= "";
	var i 		= 0 ;
	var strValue = "" ;

	dblTemp	= intCopias * dblPrecios ;

	strTempo = Redondear(dblTemp);

	for (i=0; i<TheForm.elements.length; i++) {
			if(TheForm.elements[i].name == "dblSubtotal_" + intImg + "" ){
				TheForm.elements[i].value = strTempo ;
			}
	}

	TheForm.strMarca.value = TheForm.strMarca.value + "," +  intImg ;
	dblPonerTotal(intForm);

}

function ponerValorSubtotal(intForm,intCopias,dblPrecios,intImg){
	
	var TheForm 	= document.forms[intForm];
	var dblTemp 	= 0 ;
	var dbltempo 	= 0 ;
	var strTempo 	= "";
	var i 		= 0 ;
	var strValue = "" ;


	dblTemp	= intCopias * dblPrecios ;

	strTempo = Redondear(dblTemp);


	for (i=0; i<TheForm.elements.length; i++) {
		if(TheForm.elements[i].name == "dblSubtotal_" + intImg + "" ){
			TheForm.elements[i].value = strTempo ;
		}
	}

	TheForm.strMarca.value = TheForm.strMarca.value + "," +  intImg ;
	dblPonerTotal(intForm);
}

function verJs(intForm){
	var TheForm = document.forms[intForm];
	var strValue = "" ;
	var i = 0 ;
	for (i=0; i<TheForm.elements.length; i++) {
			strValue += "Tipo =>" + TheForm.elements[i].type + "Name =>" + TheForm.elements[i].name + "Value =>" + TheForm.elements[i].value + "\n";
	}
	alert(strValue);
}

function openWindowsImg(strNameImg,strAlias){
	var Propiedades = "alwaysRaised=yes, height=300, width=300, resizable=yes, screenX=100 , screenY=100 ,toolbar=no";
	window.open("verDetailFiles.php?strImg=" + strNameImg + "&strAlias=" + strAlias + "","Visualizar_Imagenes",Propiedades);
}

function Submitear(intForm){
	TheForm.submit();
}

function valSubmitear(){
	var TheForm = document.forms[1];
	for (i=0; i<TheForm.elements.length; i++) {
		alert(  " (Name)=> " + TheForm.elements[i].name + " (Tipo) => " + TheForm.elements[i].type + " (Valor)=> " + TheForm.elements[i].value );
	}
}

function intContarFiles(nameForm){
	
	var TheForm = eval("document." + nameForm);
	var intCount 	= 0;

	for (i=0; i<TheForm.elements.length; i++) {
		if (TheForm.elements[i].type == "file"){
			if (TheForm.elements[i].value != "") {
			intCount++;
			}		
		}
	}
	return intCount;
}

function isOkEnviar(nameForm){		
	
	var strFilesValid = ".bmp.gif.jpeg.jpg"; 
	var bnlValor 	= "";	
	var TheForm = eval("document." + nameForm);	

	bnlValor = isTipoFilesValid(nameForm,strFilesValid);
	
	if(bnlValor){
		TheForm.bnlEnviar.value = "1";
		TheForm.submit();
		//llamo al cuadro de Cargando...
		subiendo();
	}
	
}

function isTipoFilesValid(nameForm,strExtensionesValidar){
	var bnlError 		= true;
	var intTempo 		= 0;
	var strValue 		= "";
	var intPosEsta  	= 0;
	var strExtension	= "";
	var strTempo		= ""
  
	intTempo = intContarFiles(nameForm); 

	var TheForm = eval("document." + nameForm);

	if(intTempo==0){
		alert("\nDebe seleccionar al menos un archivo.\n");
		return(false);
	}

	intTempo = 0 ;
	
	for (i=0; i<TheForm.elements.length; i++) {
		if (TheForm.elements[i].type == "file"){
			
			strValue = TheForm.elements[i].value;
			
			if (strValue != "") {
				intPosEsta = strValue.lastIndexOf(".");

				if (intPosEsta == -1) {
					alert("\nEl archivo seleccionado como Foto \""+strValue+"\" no posee extensión.\n");
					TheForm.elements[i].focus();
					return(false);
				}else{

					strExtension = strValue.slice(intPosEsta+1, strValue.length)
					strExtension = strExtension.toLowerCase();
					strExtensionesValidar = strExtensionesValidar.toLowerCase();
					
					intPosEsta = strExtensionesValidar.lastIndexOf("."+strExtension+"");

					if (intPosEsta == -1) {
						alert("\nEl archivo seleccionado como Foto \""+strValue+"\" no posee extensión valida.\n");
						TheForm.elements[i].focus();
						intTempo++;
						return(false);
					}else{

						if (TheForm.elements[i+1].value.length >= 50) {
							alert("\nLa descripción ingresada para la Foto \""+strValue+"\" es demasiado larga.\n Puede contener como máximo hasta 50 caracteres.\n");
							document.fmFiles.elements[i+1].focus();
							intTempo++;
							return(false);
						}
					}
				}
			}		
		}
	}

	if(intTempo!=0){
		bnlError = false;
	}
	return(bnlError);
	
}

function sSubmitear(nameForm){

	var TheForm = eval("document." + nameForm);
	TheForm.submit();

}

function EliminarSeleccionados(nameForm,nameField,strPage){

	if(isValEliminados(nameForm,nameField)){
		var TheForm = eval("document." + nameForm);
		TheForm.action 	= "" + strPage + "" ;
		sSubmitear(nameForm);
	}
}

function EliminarTodos(nameForm,strPage){

	if(confirm("¿ Desea eliminar todas las imágenes de este pedido ?")){
		var TheForm = eval("document." + nameForm);
		TheForm.action 	= "" + strPage + "" ;
		sSubmitear(nameForm);
	}	
	
}

function isValEliminados(nameForm,nameField){

	if(CheckAllSum(nameForm,nameField)==0){
		alert("\n Debe elegir un elemento.");
		return(false);
	}
	return(true);
}

function CheckAllSum(nameForm,nameField){
	var total=0;
	var TheForm = eval("document." + nameForm);
	var len = TheForm.elements.length;

	for(i=0;i<len;i++){
		if(TheForm.elements[i].name==nameField){			
			if(eval("TheForm.elements["+i+"].checked")==true){
				total += 1;
			}
		}
	}	
	return total;
}

function validarCantidadFotos(arrayNombres){
	
	/*
	valido que todas las imágenes tengan al menos 1 como cantidad seleccionada en alguna
	de sus instancias
	*/

	arrayNombres2 = arrayNombres.split(":");
	isOk = true;
			
	
	for(i=0; i < arrayNombres2.length; i++){
		
		pass=false;
		
		
		for (j=0; j < document.fmrFiles.elements.length; j++){
		  
		  if(document.fmrFiles.elements[j].name.substring(0,(12+arrayNombres2[i].length)) == "selCantidad_"+arrayNombres2[i]){
				if(document.fmrFiles.elements[j].options[document.fmrFiles.elements[j].selectedIndex].value != 0){
						pass=true;
						break;
				}
			}		
				
		}
		
		if(!pass){
			isOk = false;
			break;	
		}
	}
	if(!isOk){
		alert("Hay imágenes con cantidad igual a 0.\nPor favor corríjalo e intente nuevamente.");
		return false;
	}


	return true;	
	
}


function calcularTotal(sumaDesdeSQL){
	
	
	TheForm = document.fmrFiles;
	cantfotos=0;
	

	suma = parseFloat(0);
	for (i = 0 ; i < TheForm.elements.length; i++) {
				
		if ((TheForm.elements[i].name.substring(0,11)) == "selCantidad" ){
			cantfotos += parseFloat(TheForm.elements[i].value);
		}	
	
		if ((TheForm.elements[i].name.substring(0,11)) == "dblSubtotal" ){
			suma += parseFloat(TheForm.elements[i].value);
		}
	}

	suma += sumaDesdeSQL;
	
	total.innerHTML = "$ " + Redondear(suma);//
	//copias.innerHTML = cantfotos;	
		
}

function checkeaTodos(){

theForm = document.fmrFiles;
var estado = false; 	
	if( theForm.checkear.checked == true) estado = true;
	for (i = 0; i < theForm.elements.length; i++){
		if (theForm.elements[i].type == "checkbox" && theForm.elements[i].name.substring(0,3) == "che"){
			theForm.elements[i].checked = estado;
		}
	}
};

function cc(cual,color){
	cual.bgColor = color;
}

function mostrar(inValor){
	var item;
	var imagen;
	item = eval("div"+inValor);
	if(item.style.display == "none"){
		item.style.display = "";
	}else{
		item.style.display = "none";
	}
}

function popprecios(){
	window.open ("precios.html","precios","top=20 left=20 width=420 height=185 scrollbars=no statusbar=no toolbars=no")
}

  // ancho de imagen
var maxWidth=70;
  // alto de imagen
var maxHeight=70;
  
  // Tipos de archivo

  // id de la imagen de preview
var outImage="previewField";
  // imagen para mostrar en archivos no validos



function preview(what,cual){
	
  var defaultPic="dot.gif";
	var item;  
  var source=what.value;
	fileTypes=new Array("bmp","gif","png","jpg","jpeg");
	
	
	myString = new String(source);
	rExp = /\\/gi;
	source = myString.replace(rExp,"/"); 
	
	source = source.replace("\\","/");

  source = "file:///" + source;
 	
 	var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
  //alert(ext)
  
 
	globalPic=new Image();
	
	for (i=0 ; i < fileTypes.length ; i++){
		globalPic.src=defaultPic;
		if (fileTypes[i]==ext) {
			globalPic.src=source;
			break;
		}
	}
	
	item = "preview"+cual

  var field=document.getElementById(item);

  var x=parseInt(globalPic.width);
  var y=parseInt(globalPic.height);
  
  //alert(x + "---" + y)
  if (x > maxWidth) {
    y*=maxWidth/x;
    x=maxWidth;
  }
  if (y > maxHeight) {
    x*=maxHeight/y;
    y=maxHeight;
  }
  field.style.display=(x<1 || y<1)?"none":"";
  //alert("------>"+source)

  field.src=source;
  field.width=x;
  field.height=y;
}


function validaSucursales(){
	
	theForm = document.fmrFiles;

	for (i = 0; i < theForm.elements.length;i++){	
		if (theForm.elements[i].checked == true && theForm.elements[i].name == "radSucursal"){
			hacerSubmit('fmrFiles')
			return true;	
		};
	};	
	alert("Debe seleccionar un punto de entrega");
	return false;	
}


//maneja botonera de imagestore
function ce(tab,estado){
	if(estado == 1){
		tab.className="taboffhover";
		tab.style.cursor="hand";
	} else{
		tab.className="taboff";
	}
}

function geturl(URL){
	document.location = URL;	
}

