﻿// JScript File

// Valid Characters
// Validate Integer Numbers
function ValidateIntegerValue(campo, e) {
    var key; var keychar;
    if (window.event)
        key = window.event.keyCode;
    else if (e) key = e.which;
    else return true;
    keychar = String.fromCharCode(key);
    if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27))
    //return true;         
        event.returnValue = true;
    else if ((("0123456789").indexOf(keychar) > -1)) event.returnValue = true;
    //else return false; 
    else event.returnValue = false;
}



//Validate Max Lenght of a field
function maxLen(obj,i) {
	if (obj.value.length >= i) {	    
		obj.value = obj.value.slice(0,i);
	}
}

//Avoid Enter
function event_Enter(e) {
var pK = e ? e.which : window.event.keyCode;
return pK != 13;
}




// Valid Characters (Not used all characters should be valid, but can be used for other pospuses on the form)
function valid_charecters(campo,e) {       
        var key; var keychar; 
        if (window.event) 
          key = window.event.keyCode; 
        else if (e) key = e.which; 
        else return true; 
        keychar = String.fromCharCode(key);         
        if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ) 
        event.returnValue = true;
        //return true;         
        else if ((("0123456789ABCDEFGHIJKLMNÑOPQRSTUVWXYZabcdefghijklmnñopqrstuvwxyz,.() _$%/;:@#-¿?¡!").indexOf(keychar) > -1)) event.returnValue = true; 
        //else return false; 
        else event.returnValue = false;

	}


	// Formato a los numeros con y sin decimales	
	function format(obj, decimal) {

	    if (obj.value != '') {
	        anynum = eval(obj.value);
	        divider = 10;
	        switch (decimal) {
	            case 0:
	                divider = 1;
	                break;
	            case 1:
	                divider = 10;
	                break;
	            case 2:
	                divider = 100;
	                break;
	            case 3:
	                divider = 1000;
	                break;
	            default:
	                divider = 1000;
	        }

	        workNum = Math.abs((Math.round(anynum * divider) / divider));

	        workStr = "" + workNum

	        if (workStr.indexOf(".") == -1) { workStr += "." }

	        dStr = workStr.substr(0, workStr.indexOf(".")); dNum = dStr - 0
	        pStr = workStr.substr(workStr.indexOf("."))

	        while (pStr.length - 1 < decimal) { pStr += "0" }

	        if (pStr == '.') pStr = '';

	        //--- Agrega coma a miles    
	        if (dNum >= 1000) {
	            dLen = dStr.length
	            dStr = parseInt("" + (dNum / 1000)) + "," + dStr.substring(dLen - 3, dLen)
	        }

	        //-- Agrega coma a millones
	        if (dNum >= 1000000) {
	            dLen = dStr.length
	            dStr = parseInt("" + (dNum / 1000000)) + "," + dStr.substring(dLen - 7, dLen)
	        }
	        retval = dStr + pStr

	        //-- Agrega coma a billones
	        if (dNum >= 1000000000) {
	            dLen = dStr.length
	            dStr = parseInt("" + (dNum / 1000000000)) + "," + dStr.substring(dLen - 10, dLen)
	        }
	        retval = dStr + pStr

	        //-- Pone parentesis si es negativo
	        if (anynum < 0) { retval = "(" + retval + ")"; }

	        obj.value = retval;
	    }
	}

	// Save Image Link functions
	function saveImageAs(imgNameWithPath) {
	    win = window.open(imgNameWithPath, 'win');
	    window.setTimeout('checkImageReady();', 100);
	}

	function checkImageReady() {
	    if (win.document.readyState == 'complete') {
	        win.document.execCommand("SaveAs");
	        win.close();
	    } else {
	        window.setTimeout('checkImageReady();', 100);
	    }
	}



