﻿
function validarCantidad(cantidad_id, stock, mostrar_agregar_mensaje)
{

    var cant = obtenerElemento(cantidad_id);
    
    if(cant.value == "")
	{
		alert("Debe ingresar una cantidad");
		return false;
	}
	
	expresion=/^[0-9]+$/
			
	if(!expresion.test(cant.value))
	{
		alert('La cantidad solicitada no es válida');
		return false;
	}
	
	if(cant.value < 1)
	{
		alert("Debe ingresar una cantidad superior a 1");
		return false;
	}

    if(cant.value > stock)
	{
	    if(stock == 1)
	    {
	        alert("La cantidad solicitada debe ser igual a " + stock);
	    }
	    else
	    {
	        alert("La cantidad solicitada debe ser menor o igual a " + stock);
	    }
		
		return false;
	}
		
	return true;

}

function obtenerElemento(id)
{
	var elemento;

	if(document.all) // IE5
	{
		elemento = document.all[id];
	}

	if(document.getElementById) // IE6+, Mozila, Opera
	{
		elemento = document.getElementById(id);
	}

	return elemento;

}

function trim(str)
{
  return( (""+str).replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') );
}

function ajaxCliente() {
	
	this.httpMetodo = "GET";
	this.parametrosPOST = null;
	this.respuestaServidor = null;
	this.procesarRespuesta = function(){};
	this.procesarRespuesta();
	
	this.ejecutarProceso = function() {
		
		var d = new Date();
		var id = d.getTime();
		
		if(this.url.indexOf("?") == -1)
		{
		    var URL = this.url + "?id=" + id;
		}
		else
		{
		    var URL = this.url + "&id=" + id;
		}
		
		var xmlHttp = obtenerObjetoXmlHttp();
		
		if (xmlHttp == null)
		{
			return false;
		} 
			
		var self = this;
		
		xmlHttp.onreadystatechange = function() { 
			if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
			{ 
				self.respuestaServidor = xmlHttp.responseText;
				self.procesarRespuesta();
			} 
		}; 
		
		if(this.httpMetodo != "POST" || this.httpMetodo != "GET")
		{
		    this.httpMetodo = "GET";   
		}
		
		xmlHttp.open(this.httpMetodo, URL, true);
		xmlHttp.send(this.parametrosPOST);
		return true;
	
	};

	function obtenerObjetoXmlHttp() { 
	
		var objXMLHttp = null;
		
		try // IE7, NS, FireFox, Safari, Opera
	    {
		    objXMLHttp = new XMLHttpRequest();
	    }
	    catch (e)
	    {
    		
		    try // IE6
		    {
			    objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
		    }
		    catch (e)
		    {
			    try // IE5.5
			    {
				    objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
			    }
			    catch (e){}
		    }
	    }
		
		return objXMLHttp;
	
	};

}

function validaPasswordIntouch(text)
{
	var reg_pass=/[a-zA-Z0-9_\-\.]+/;
	
	if(reg_pass.test(text)) { 
       return true;
    }
	reg_pass=/^\d{1,2}\/\d{1,2}\/\d{2,4}$/ /*Password del tipo fecha ejemplo 22/01/2007*/
	if(reg_pass.test(text)) { 
       return true;
    }
	return false;
}
function validaAlphanumericos(text)
{
	var reg_val=/[a-zA-Z0-9_\-\.\s]+/;
	
	if(reg_val.test(text)) { 
       return true;
    }
	return false;
}


function imprimir()
{
	window.print()
}

function cerrar()
{
    window.close()
}

function obtenerUrlParametro(nombre)
{
  nombre = nombre.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+nombre+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	
	return arrayPageSize
	
}

function getCurrentScroll()
{
    var scrollY;
    
    try
    {
        scrollY = document.all ? document.body.scrollTop:window.pageYOffset;
    }
    catch(err)
    {
        scrollY = 0;
    }
   
    return scrollY;
}

function abrirLightBox(width, height, src)
{

    window.open(src, "procesos", "directories=no, menubar =no,status=no,toolbar=no,location=no,scrollbars=yes,fullscreen=no,height=" + height + ",width=" + width + "px,left=100px,top=50px");   
}

function dateAdd(p_Interval, p_Number){

   var date = new Date(); 
   var strDate = "";
   
   //in the spirt of VB we'll make this function non-case sensitive
   //and convert the charcters for the coder.
   p_Interval = p_Interval.toLowerCase();
   
   if(isNaN(p_Number)){
   
      //Only accpets numbers 
      //throws an error so that the coder can see why he effed up   
      throw "The second parameter must be a number. \n You passed: " + p_Number;
      return false;
   }

   p_Number = new Number(p_Number);
   switch(p_Interval.toLowerCase()){
      case "yyyy": {// year
         date.setFullYear(date.getFullYear() + p_Number);
         break;
      }
      case "q": {      // quarter
         date.setMonth(date.getMonth() + (p_Number*3));
         break;
      }
      case "m": {      // month
         date.setMonth(date.getMonth() + p_Number);
         break;
      }
      case "y":      // day of year
      case "d":      // day
      case "w": {      // weekday
         date.setDate(date.getDate() + p_Number);
         break;
      }
      case "ww": {   // week of year
         date.setDate(date.getDate() + (p_Number*7));
         break;
      }
      case "h": {      // hour
         date.setHours(date.getHours() + p_Number);
         break;
      }
      case "n": {      // minute
         date.setMinutes(date.getMinutes() + p_Number);
         break;
      }
      case "s": {      // second
         date.setSeconds(date.getSeconds() + p_Number);
         break;
      }
      case "ms": {      // second
         date.setMilliseconds(date.getMilliseconds() + p_Number);
         break;
      }
      default: {
      
         //throws an error so that the coder can see why he effed up and
         //a list of elegible letters.
         throw   "The first parameter must be a string from this list: \n" +
               "yyyy, q, m, y, d, w, ww, h, n, s, or ms.  You passed: " + p_Interval;
         return false;
      }
   }
   
   if(date.getDate() < 10)
   {
    strDate = "0" + date.getDate() + "/";
   }
   else
   {
    strDate = date.getDate() + "/";
   }
   
   if(date.getMonth() < 10)
   {
    strDate += "0" + date.getMonth() + "/";
   }
   else
   {
    strDate += date.getMonth() + "/";
   }
      
   strDate += date.getFullYear();
   
   return strDate;
   
} 

function validarNumero(input)
{
    var reg = /^[0-9,\.]+$/;
    
    if(!reg.test(input))
    {
        return false;
    }
    
    return true;
}

function cambiarCSS(obj, css)
{
    obj.className = css;
}

