/**
 * CL_common.js
 *
 * JavaScript commons functions>
 * 
 * @package
 * @version 1.0
 * @access public
 * @example none given
 * @date 2003-11-23
 * @author Marcelo Pecimut <pecimut@yahoo.com>
 * @comment 
 *
 */


var digits = "1234567890";
var float_chars= digits+".+-eE";
var int_chars= digits + "+-";
var phone_chars = digits + "()- ";
var zip_chars = digits+"-";
var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var lowAlpha = "abcdefghijklmnopqrstuvwxyz";
var object_focus;
var aFinMes = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 


function jsTrim(s) {return s.replace(/(^\s+)|(\s+$)/g, "");}


function document_onload(){
/*
document.load() se ejecuta en el load del documento
*/

//writeMenus();

if (object_focus!=null)
	set_focus(object_focus);

}


function asearch(parray,psearch){
/*
permite ingresar solo digitos en un campo de un formulario

parray	array	array donde esta el valor buscado
psearch	string	valor buscado
*/

	for (var j = 0; j <parray.length; j++){
		if (parray[j] == psearch){
			return (j);
		}
	}
return (false);

}

function trim(str) {
    return ltrim(rtrim(str));
}

function rtrim(str) {
    var last = str.length - 1;

    if ( str.length == 0 || str.charAt(last) != ' ' )
        return str;

    var i;

    for ( i = last ; str.charAt(i) == ' ' ; i-- ) {
    }

    return str.substring(0, str.length - i);
}

function ltrim(str) {
    if ( str.length == 0 || str.charAt(0) != ' ' )
        return str;

    var i;

    for ( i = 0 ; str.charAt(i) == ' ' ; i++ ) {
    }

    return str.substring(i);
}

function openWindow(url, w, h, scrollbars) {
    args = "width=" + w + ",height=" + h + ",resizable=yes,scrollbars=" + scrollbars + ",status=0";
    window.open(url, "", args);
}

function setValue(obj, value) {
    var i = 0;
  
    for ( ; i < obj.length ; i++ ) {
        if ( obj[i].value == value ) {
            obj.options[i].selected = true;
            break;
        }
    }
}


function openventana(theURL,NOMBRE,ancho,alto) {
	wname ="CHROMELESSWIN"
	W=ancho;
	H=alto;
	windowCERRARa 		= "imagenes/close_d.gif"
	windowCERRARd 		= "imagenes/close_a.gif"
	windowCERRARo 		= "imagenes/close_o.gif"
	windowNONEgrf 		= "imagenes/none.gif"
	windowCLOCK		= "imagenes/clock.gif"
	windowREALtit		= "apdirectory.com"
	windowTIT 	    	= "<img src=./imagenes/logobarra.gif border=0>"
	windowBORDERCOLOR   	= "#000000"
	windowBORDERCOLORsel	= "#FFFFFF"
	windowTITBGCOLOR    	= "#FF6600"
	windowTITBGCOLORsel 	= "#000000"
	openchromeless(theURL, wname, W, H, windowCERRARa, windowCERRARd, windowCERRARo, windowNONEgrf, windowCLOCK, windowTIT, windowREALtit , windowBORDERCOLOR, windowBORDERCOLORsel, windowTITBGCOLOR, windowTITBGCOLORsel)
}


//----------------------------------------------------------------
function page_load(url,param, value) {
// carga una pagina con un parametro

url = url+'?'+param+'='+value;

window.location.href = url;
//alert (url);

}


function isNumericKey(pcharCode)
 {
 //chequea si el caracter es un numero {0..9}{.}{,}{+}
 
 // This function will check what key is pressed and return false
 // if not one of the following keys is pressed :
 // 0-9 (96-105)
 // . (decimal point on numeric keyboard : 110, as normal character : 190)
 // Backspace(8), TAB(9), SHIFT(16), CTRL(17), ALT(18)
 // Delete(46)
 
 //var charCode= {navigator.appName=="Netscape") ? e.which : e.keyCode;

if ((pcharCode < 44 || pcharCode > 57))
    return false;
 else
    return true;
} 
 
function isDigitKey(pcharCode)
 {
 // acepta solo numeros {0..9} y {-}(45)

if ((pcharCode < 48 || pcharCode > 57) && pcharCode != 45 )
    return false;
 else
    return true;
} 


//---------------------------------------
/**
 * Check for alphanumerics chars
 */
function IsAlphaNumeric(YourAlphaNumeric)
{
	var schar = "";
	var Template = /^[a-z 0-9αινσϊρό]+$/i //alphanumeric standard chars (english, spanish)
	
	schar = String.fromCharCode(YourAlphaNumeric);
	return (schar.match(Template)) ? 1 : 0
}

//---------------------------------------
/**
 * Check for valid SQL chars
 */
function IsSQLValidChar(YourAlphaNumeric)
{
	var schar = "";
	var Template = /^['\\]+$/i //sql invalid chars

	schar = String.fromCharCode(YourAlphaNumeric);

	return (schar.match(Template)) ? 0 : 1
}

//--------------------------------------------------------
// date funcs
  function finMes(nMes, nAno){ 
   return aFinMes[nMes - 1] + (((nMes == 2) && (nAno % 4) == 0)? 1: 0); 
  } 

   function padNmb(nStr, nLen, sChr){ 
    var sRes = String(nStr); 
    for (var i = 0; i < nLen - String(nStr).length; i++) 
     sRes = sChr + sRes; 
    return sRes; 
   } 

   function makeDateFormat(nDay, nMonth, nYear){ 
    var sRes; 
	sRes = padNmb(nYear, 4, "0") + "-" + padNmb(nMonth, 2, "0") + "-" + padNmb(nDay, 2, "0") ; 
    
    
    return sRes; 
   } 
    
  function incDate(sFec0){ 
   var nDia = parseInt(sFec0.substr(8, 2), 10); 
   var nMes = parseInt(sFec0.substr(5, 2), 10); 
   var nAno = parseInt(sFec0.substr(0, 4), 10); 


   nDia += 1; 
   if (nDia > finMes(nMes, nAno)){ 
    nDia = 1; 
    nMes += 1; 
    if (nMes == 13){ 
     nMes = 1; 
     nAno += 1; 
    } 
   } 
   return makeDateFormat(nDia, nMes, nAno); 
  } 

  function decDate(sFec0){ 

   var nDia = parseInt(sFec0.substr(8, 2), 10); 
   var nMes = parseInt(sFec0.substr(5, 2), 10); 
   var nAno = parseInt(sFec0.substr(0, 4), 10); 

   
   
   nDia -= 1; 
   if (nDia == 0){ 
    nMes -= 1; 
    if (nMes == 0){ 
     nMes = 12; 
     nAno -= 1; 
    } 
    nDia = finMes(nMes, nAno); 
   } 
   return makeDateFormat(nDia, nMes, nAno); 
  } 

//--------------------------------------------------------
  function addToDate(sFec0, sInc){ 
   var nInc = Math.abs(parseInt(sInc)); 
   var sRes = sFec0; 
   if (parseInt(sInc) >= 0) 
    for (var i = 0; i < nInc; i++) sRes = incDate(sRes); 
   else 
    for (var i = 0; i < nInc; i++) sRes = decDate(sRes); 
   return sRes; 
  } 


//--------------------------------------------------------
/**
 * languaje: javascript
 * conver ISO format date to mm/dd/yyyy format
 */
function date_format_iso_to_mmddyyyy(s_date){

var	nday = parseInt(s_date.substr(8, 2), 10); 
var	nmonth = parseInt(s_date.substr(5, 2), 10); 
var	nyear = parseInt(s_date.substr(0, 4), 10); 
var s_ret_date = nmonth+'/'+nday+'/'+nyear;

return s_ret_date;

}


//--------------------------------------------------------
/**
 * languaje: javascript
 * return the difference between two dates
 */
function DateDiff( start, end, interval, rounding ) {
    var iOut = 0;
    
    // convert ISO(yyyy-mm-dd) -> mm/dd/yyyy
	var s_start = date_format_iso_to_mmddyyyy(start);
	var s_end = date_format_iso_to_mmddyyyy(end);
    
    
    // Create 2 error messages, 1 for each argument. 
    var startMsg = "Check the Start Date and End Date\n"
        startMsg += "must be a valid date format.\n\n"
        startMsg += "Please try again." ;
		
    var intervalMsg = "Sorry the dateAdd function only accepts\n"
        intervalMsg += "d, h, m OR s intervals.\n\n"
        intervalMsg += "Please try again." ;

    var bufferA = Date.parse( s_start ) ;
    var bufferB = Date.parse( s_end ) ;
    	
    // check that the start parameter is a valid Date. 
    if ( isNaN (bufferA) || isNaN (bufferB) ) {
        alert( startMsg ) ;
        return null ;
    }
	
    // check that an interval parameter was not numeric. 
    if ( interval.charAt == 'undefined' ) {
        // the user specified an incorrect interval, handle the error. 
        alert( intervalMsg ) ;
        return null ;
    }
    
    var number = bufferB-bufferA ;
    
    // what kind of add to do? 
    switch (interval.charAt(0))
    {
        case 'd': case 'D': 
            iOut = parseInt(number / 86400000) ;
            if(rounding) iOut += parseInt((number % 86400000)/43200001) ;
            break ;
        case 'h': case 'H':
            iOut = parseInt(number / 3600000 ) ;
            if(rounding) iOut += parseInt((number % 3600000)/1800001) ;
            break ;
        case 'm': case 'M':
            iOut = parseInt(number / 60000 ) ;
            if(rounding) iOut += parseInt((number % 60000)/30001) ;
            break ;
        case 's': case 'S':
            iOut = parseInt(number / 1000 ) ;
            if(rounding) iOut += parseInt((number % 1000)/501) ;
            break ;
        default:
        // If we get to here then the interval parameter
        // didn't meet the d,h,m,s criteria.  Handle
        // the error. 		
        alert(intervalMsg) ;
        return null ;
    }
    
    return iOut ;
}





//--------------------------------------------------------
/*
// FUNCIONES PARA CONSULTAR AL USUARIO AL APRETAR UN BOTO SI ESTA SEGURO
TODO: implementarlas



function HMError(strEType,strError,strOther,strEN)
{
strError = unescape(strError).replace(/\+/g," ");
strError = strError.replace(/\\n/g,"\n");
switch(strEType)
{
//
case "A":
	alert(strError);
	break;
case "M":
	if (ie)
		DoModal(strOther,strEN);
	else
		DoFakeModal(strOther,strEN);
	break;
//
case "C":
	return(confirm(strError));
	break;
}



TODO: implementarlas

function DoModal(strOther,strEN)
{
rv = window.showModalDialog("/cgi-bin/dasp/error_modalshell.asp?strEN="+strEN+"&r="+Math.round(Math.random()*1000000),"","dialogWidth:360px;dialogHeight:217px;help:0;scroll:0;status:0;");
if (rv.help)
	CallPaneHelp(rv.help);
if (rv.url)
{
	if (strOther=="attach")
		DoSaveMSG();
	else
		location.href=rv.url;
}

}

function DoFakeModal(strOther,strEN)
{
ErrOther = strOther;
window.open("/cgi-bin/dasp/error_modalshell.asp?strEN="+strEN+"&r="+Math.round(Math.random()*1000000), "newwin", "resizable=no,width=360,height=217");
}
*/
// EOF


