﻿var container = 'ctl00_contentplaceholderMP_';

function OpenSearchModalDialog(url, parameters, element, buttonForPostBack) 
{
    //parameters = 'center:yes;help:no;status:yes;dialogWidth:600px;dialogHeight:600px;scroll:yes'; 
	var sReturnValue = OpenViewModalDialog(url, parameters);

	if (typeof(sReturnValue) == 'string' || typeof(sReturnValue) == 'number')
	{
		var eInput = document.getElementById(element);
		eInput.value = sReturnValue;
        
		if (buttonForPostBack != null && typeof(buttonForPostBack) == 'string' && buttonForPostBack != '') 
		{
			var eButton = document.all[buttonForPostBack];
			eButton.click();
		}
	}
}

function OpenEditModalDialog(url, parameters, buttonForPostBack)
{
	OpenViewModalDialog(url, parameters);
	
	if (buttonForPostBack != null && typeof(buttonForPostBack) == 'string' && buttonForPostBack != '') 
	{
		var eButton = document.all[buttonForPostBack];
		eButton.click();
	}
}

function OpenViewModalDialog(url, parameters) 
{
	return window.showModalDialog(url, '', parameters);	
}

function CloseModalDialog()
{
    window.close();
}  

function OpenPopup(url, parameters )
{
    window.open(url,'', parameters);
}

function textboxMultilineMaxLenght(txt,maxLen)
{   
   if(txt.value.length > (maxLen-1))
      return false;   
}

//Valida que fecha hasta no sea menor a fecha desde
function ValidateEndDateAgainstInitialDate(sender, e) 
{
    //Se obtienen los controles que contienen las fechas    
    var idControls = sender.Controls.split(',')
    var controlInitialDate = document.getElementById(container + idControls[0]);
    var controlEndDate = document.getElementById(container + idControls[1]);
    //Cuando no existe MasterPage, se quita el container
    if(controlInitialDate == null && controlEndDate == null)
    {
        controlInitialDate = document.getElementById(idControls[0]);
        controlEndDate = document.getElementById(idControls[1]);
    }
    
    //Si alguno de los controles está vacio no se valida
    if(controlInitialDate.value == '' || controlEndDate.value == '')
        return;
    
    //Se obtiene la fecha inicial
    var dateNumbers = controlInitialDate.value.split('/');
    var initialDate = new Date(dateNumbers[2], dateNumbers[1] - 1, dateNumbers[0]);
    
    //Se obtiene la fecha final
    dateNumbers = controlEndDate.value.split('/');
    var endDate = new Date(dateNumbers[2], dateNumbers[1] - 1, dateNumbers[0]);
    
    //Se valida    
    e.IsValid = initialDate <= endDate;

}

//Valida una fecha contra la fecha actual
function ValidateDateAgainstToday(sender, e) 
{
	var myControl = document.all[sender.controltovalidate];
    
	if (myControl.value == '') 
	{
		e.IsValid = true;
		return;
	}
		
	var sOperator = sender.operator;
    
    var dateNumbers = myControl.value.split('/');
    
    var myDate=new Date();
    myDate.setDate(dateNumbers[0]);
    myDate.setMonth(dateNumbers[1] - 1);
    myDate.setFullYear(dateNumbers[2]);
    
    var myDate2=new Date();
    	
	switch (sOperator) {
		case ">":
			e.IsValid = myDate > myDate2;
			break; 
		case ">=":
			e.IsValid = myDate >= myDate2;
			break;
		case "<=":
			e.IsValid = myDate <= myDate2;
			break;
		case "<":
			e.IsValid = myDate < myDate2;
			break;
		case "==":
			e.IsValid = myDate == myDate2;
			break;
	}
} 

//Valida el formato de fechas
function IsDate(sender, e)
{ 
    var dtStr = document.getElementById(sender.controltovalidate).value.toString();
	// Definicion de caracter separador de dia/mes/anio, anio minimo y maximo
    var dtCh= "/";
    var minYear=1900;
    var maxYear=2070;
	var daysInMonth = DaysArray(12)
	//la posicion de la primera "/" en el string de la fecha
	var pos1=dtStr.indexOf(dtCh);
	//la posicion de la segunda "/" en el string de la fecha
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strDay=dtStr.substring(0,pos1);
	var strMonth=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}

	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	
	//Si no encuentro las dos "/" en el string
	if (pos1==-1 || pos2==-1){
		e.IsValid = false;
		return;
	}
	//Si el mes esta fuera del rango 1-12
	if (strMonth.length<1 || month<1 || month>12){
		e.IsValid = false;
		return;
	}
	//Si el dia esta fuera del rango 1-28, 1-29, 1-30, 1-31 segun el mes
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		e.IsValid = false;
		return;
	}
	//Si el anio no tiene los 4 digitos o esta fuera del rango anio minimo-anio maximo
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		e.IsValid = false;
		return;
	}
	//Si encuentra otra "/" despues de la segunda o la cadena de fecha sin las barras
	//no es un numero entero
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		e.IsValid = false;
		return;
	}

	if (month>1 && year>=2070){
		e.IsValid = false;
		return;
	}

	
	e.IsValid = true;
	return;
}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Valido que el caracter sea un numero.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // Todos los caracteres son numeros.
    return true;
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Busca caracteres uno por uno.
    // Si el caracter no esta contemplado en bag lo apendea a returnstring.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// Febrero tiene 29 dias en los anios divisibles por 4
	// Excepto los divisibles por 100 pero no por 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function onUpdating(updateProgress) {
    // get the update progress div
    var updateProgressDiv = $get(updateProgress);
    // make it visible
    updateProgressDiv.style.display = '';
}

function onUpdated(updateProgress) {
    // get the update progress div
    var updateProgressDiv = $get(updateProgress);
    // make it invisible
    updateProgressDiv.style.display = 'none';
}
