/*
 * Copyright  Yage. All Rights Reserved.
 *
 * YAGE evolucion digital
 * Av. Brasil 951 y Mariano Echeverria
 * Quito-Ecuador
 * www.yage.com.ec
 */
 
function winStatus(msg) {
	window.status = msg;
}

function setLocation(url) {
	location = url;
}

function getOpenerUrl() {
    var sPath = location.pathname;
    var i = sPath.indexOf ('/:');
    var j = sPath.indexOf ('/', ++i);
    if (i != -1) sPath = sPath.substring (j, sPath.length);
	return escape(location.protocol + '//' + location.host + sPath + location.search);
}

function openWindow(url, title, w, h, x, y, scrollbars, location, directories, status, menubar, toolbar, resizable) {
	var settings='width='+w+',height='+h+',top='+x+',left='+y+',scrollbars='+scrollbars+',location='+location+',directories='+directories+',status='+status+',menubar='+menubar+',toolbar='+toolbar+',resizable='+resizable;
	nWin = open(url, title, settings);
	nWin.focus();
	return nWin;
}


function openCenterWindow(sHRef,name,nWidth,nHeight,sScrolls) {
	var nLeft = (screen.width - nWidth) / 2;
	var nTop = (screen.height - nHeight) / 2 - 20;
	openWindow(sHRef, name, nWidth, nHeight, nLeft, nTop, sScrolls, 'no', 'no', 'yes', 'no', 'no', 'no')
}


// dialogWidth (cm, mm, in, pt, pc, or px)
// dialogHeight (cm, mm, in, pt, pc, or px)
// dialogLeft (cm, mm, in, pt, pc, or px)
// dialogTop (cm, mm, in, pt, pc, or px)
// center:{ yes | no | 1 | 0 | on | off }
// dialogHide:{ yes | no | 1 | 0 | on | off }
// edge:{ sunken | raised }
// help:{ yes | no | 1 | 0 | on | off }
// resizable:{ yes | no | 1 | 0 | on | off }
// scroll:{ yes | no | 1 | 0 | on | off }
// status:{ yes | no | 1 | 0 | on | off }
function modalDialog(url, args, dialogWidth, dialogHeight, dialogLeft, dialogTop, center, dialogHide, edge, help, resizable, scroll, status) {
	var sFeatures = "";
	if (center != 'yes' && center != '1' && center != 'on')
		sFeatures = "dialogWidth: "+dialogWidth+"px; dialogHeight: "+dialogHeight+"px; dialogLeft:"+dialogLeft+"px; dialogTop: "+dialogTop+"px; center: "+center+"; dialogHide: "+dialogHide+"; edge: "+edge+"; help: "+help+"; resizable: "+resizable+"; scroll: "+scroll+"; status: "+status+";";
	else
		sFeatures = "dialogWidth: "+dialogWidth+"px; dialogHeight: "+dialogHeight+"px; center: "+center+"; dialogHide: "+dialogHide+"; edge: "+edge+"; help: "+help+"; resizable: "+resizable+"; scroll: "+scroll+"; status: "+status+";";
	return window.showModalDialog(url, args, sFeatures);
}

function modelessDialog(url, args, dialogWidth, dialogHeight, dialogLeft, dialogTop, center, dialogHide, edge, help, resizable, scroll, status) {
	var sFeatures = "";
	if (center != 'yes' && center != '1' && center != 'on')
		sFeatures = "dialogWidth: "+dialogWidth+"px; dialogHeight: "+dialogHeight+"px; dialogLeft:"+dialogLeft+"px; dialogTop: "+dialogTop+"px; center: "+center+"; dialogHide: "+dialogHide+"; edge: "+edge+"; help: "+help+"; resizable: "+resizable+"; scroll: "+scroll+"; status: "+status+";";
	else
		sFeatures = "dialogWidth: "+dialogWidth+"px; dialogHeight: "+dialogHeight+"px; center: "+center+"; dialogHide: "+dialogHide+"; edge: "+edge+"; help: "+help+"; resizable: "+resizable+"; scroll: "+scroll+"; status: "+status+";";
	return window.showModelessDialog(url, args, sFeatures);
}




function openUrlPreview(index,control) {
	var control =document.forms[index].elements[control];
	url  = CONTEXT_PATH + control.value;
	if (control.value!=''){
		if (url){
			var x = 100;
			var y = Math.floor((screen.height - 300) / 2);
		    var wup = openWindow(url, 'Preview', 500, 500, x, y, 'yes', 'no', 'no', 'yes', 'no', 'no', 'no');
		    if (wup.opener == null) wup.opener = self;
			wup.focus();
		}
	}
}

function openUrlPreviewPath(index,purl) {
	url  = purl;
	if (url){
		var x = 100;
		var y = Math.floor((screen.height - 300) / 2);
	    var wup = openWindow(url, 'Preview', 500, 500, x, y, 'yes', 'no', 'no', 'yes', 'no', 'no', 'no');
	    if (wup.opener == null) wup.opener = self;
		wup.focus();
	}
}

function openPopup(index,page) {

		url  = CONTEXT_PATH + page;
		if (url){
			var x = 100;
			var y = Math.floor((screen.height - 300) / 2);
		    var wup = openWindow(url, 'Preview', 500, 500, x, y, 'yes', 'no', 'no', 'yes', 'no', 'no', 'no');
		    if (wup.opener == null) wup.opener = self;
			wup.focus();
		}

}

function openUploadWindow(url, path, formName, property, indexForm,w, h, xi, yi) {
    var url = url;

    url = url + '?parent=' + getOpenerUrl();
    url = url + '&path=' + path;
    url = url + '&form=' + formName;
    url = url + '&property=' + property;
    url = url + '&index=' + indexForm;

	if (w != null && h != null){
	    var wup = openWindow(url, 'Upload', w, h, xi, yi, 'yes', 'no', 'no', 'yes', 'no', 'no', 'no');
	}else{
		var x = Math.floor((screen.width - 600) / 2);
		var y = Math.floor((screen.height - 400) / 2);
    
	    var wup = openWindow(url, 'Upload', 590, 380, x, y, 'yes', 'no', 'no', 'yes', 'no', 'no', 'no');
	}

    if (wup.opener == null) wup.opener = self;
	wup.focus();
}
function clean(frm, objTargetName, objLabelName, txtDesc){
	if (typeof txtDesc=='undefined')
		txtDesc="";
	frm.elements[objTargetName].value = "";
	frm.elements[objLabelName].value = txtDesc;
}

function openSearchWindow(pUrl,objForm) {	
	var newUrsl = pUrl ;
	openCenterWindow(newUrsl, 'searchContent', 650, 500, 'yes');
}
function openPreviewContentWindow(pUrl,codeLayout) {	
	var url = pUrl ;
	if (codeLayout!=''){
		window.open(url+'&codeTemplate='+codeLayout, 'previewContentWindow');
	}
}


/**Functio to Open a PopUp Search Form */
function openSearch(url, f, propList, labelList, index) {
    var url = CONTEXT_PATH + url;
    
	if (url.indexOf('?')>=0)
		url = url + '&';
	else 
		url = url + '?';
    url = url + 'parent=' + getOpenerUrl();
    url = url + '&form=' + f;
    
	for (var i=0; i<propList.length; i++) {
		url = url + '&properties='+propList[i];
	}
	for (var i=0; i<labelList.length; i++) {
		url = url + '&labels='+labelList[i];
	}
	url = url + '&index=' + index;
	
	var x = Math.floor((screen.width - 600) / 2);
	var y = Math.floor((screen.height - 400) / 2);
    
    var wup = openWindow(url, 'Search', 590, 380, x, y, 'yes', 'no', 'no', 'yes', 'no', 'no', 'no');
    
    if (wup.opener == null) wup.opener = self;
	wup.focus();
}


function clearControls(index, controls){
	var controlCollection = controls.split('|'); 
	for (var i=0; i<controlCollection.length; i++){
		document.forms[index].elements[controlCollection[i]].value = '';
	}
}

function setFocus() {
	if (document.forms[0]!='undefined'){
		var len = document.forms[0].elements.length;

		for (i = 0; i < len; i++) {
			if (document.forms[0].elements[i].type == 'text'&&(document.forms[0].elements[i].disabled=='true' || document.forms[0].elements[i].disabled=='')){
				document.forms[0].elements[i].focus();
				break;
			}
		}
	}
}

/**
* Fomrs Functions
*/
function setCheckBoxValue(objForm, checkName, value) {
	var len = objForm.elements.length;

	for (i = 0; i < len; i++) {
		if (objForm.elements[i].name == checkName)
			objForm.elements[i].checked = value;
	}
}




function getSelectedCheckBoxValues(objForm, checkName){
	var len = objForm.elements.length;
	var cont = 0;
	var value = 0;
	var flagtab= 0;
	for (i=0; i < len; i++) {
		if (objForm.elements[i].name == checkName && objForm.elements[i].checked) {
			 value = (value==0)?objForm.elements[i].value:value;
			 cont = cont + 1;
		}
		if(objForm.elements[i].name == 'tab') {
			flagtab = 1;
		}
	}
	if (flagtab ==0){
		if ( objForm.code != undefined )
			objForm.code.value = value;
	}
	return cont;
}
function doAction(objForm, property, action) {
	if (action == 'delete') {
		input_box=confirm("Si elimina esta seccion, sera irrecuperable. No ira a la Papelera de Reciclaje. Desea eliminarla?  ");
		if (input_box==true) {
			objForm.submit();
		}
	} else {
		objForm.submit();
	}
}
function doActionN(objForm, property, action, checkName) {  //para los que tienen checkBoxes
	var nRegs = 0;
	if (action == 'read'){			
		nRegs = getSelectedCheckBoxValues(objForm, checkName);
		if( nRegs == 1){
			objForm.submit();
		}
		else if(nRegs > 1){
			alert('Por favor seleccione solo un item para su edicion');
		}else
			window.alert('Debe seleccionar un elemento para poder editarlo');
	}	
	else if (action == 'delete'){
		nRegs = getSelectedCheckBoxValues(objForm, checkName);
		if( nRegs > 0){
			if( Delete_Click(action, nRegs) == 1 ){
				objForm.submit();
			}
		}else
			window.alert('Debe seleccionar al menos un elemento para poder realizar una eliminacion');
	}else {

		objForm.submit();
	}

}


function Delete_Click(action, nRegs)
{
	var sMessage = "Si elimina esta informacion, sera irrecuperable. No ira a la Papelera de Reciclaje. Desea eliminar " + nRegs +  " registro(s)?  " ;
	
	if(action == 'delete')
	{		
		if(!confirm(sMessage)) {
			return 0;
		} else {
			return 1;
		}
	}else {
		window.alert('Ha ocurrido un error, por favor comuniquese con el administrador.');
	}	
}

function confirmDelete(urlParam,message,collection,errorMessage) {
	var theAnswer;
	while (collection.indexOf('[') >= 0)
		collection = collection.replace('[','');
		
	while (collection.indexOf(']') >= 0)
		collection = collection.replace(']','');
		
	if (collection.length > 0)
		alert('Esta seccion no puede ser eleminada porque tiene secciones dependientes');
	else {
		theAnswer = confirm(message);
		if (theAnswer)
			window.location = urlParam;
	}
}

function disableButtons(objForm) {
	var len = objForm.elements.length;
	
	for (i = 0; i < len; i++) {
		var tempObj = objForm.elements[i];
		var temp = tempObj.type.toLowerCase();
		if ( temp == 'submit' || temp == 'reset' || temp == 'button')
			tempObj.disabled = true;
	}
}

function syncSelect(selObj, value, adata) {
	var sdata = new Array();
	var sindx = 0;	
	var code = value*1;	
	for (var i=0; i<adata.length; i++) {
		if (adata[i][0] == code) {	
			var row = new Array();
			row[0] = adata[i][1];
			row[1] = adata[i][2];
			sdata[sindx] = row;
			sindx ++;
		}
	}
	selObj.length = sindx;
	for (var i=0; i<sindx; i++) {
		selObj.options[i].value = sdata[i][0];
		selObj.options[i].text = sdata[i][1];
	}
	selObj.selectedIndex = 0;
}



function populateSelect(selectObj, selectTarget, arrayList, search) {
	var val = selectObj.value;
	var frm = selectObj.form;
	var trg = frm.elements[selectTarget];
	
	var optionList = new Array();
	var index = 0;
	
	if (search == true) {
		var opt = new Array();
		opt[0] = 0;
		opt[1] = 'All';
		optionList[index] = opt;
		index ++;
	}
	for (var i=0; i<arrayList.length; i++) {
		if (arrayList[i][0] == val) {
			var opt = new Array();
			opt[0] = arrayList[i][1];
			opt[1] = arrayList[i][2];
			optionList[index] = opt;
			index ++;
		}
	}
	
	trg.length = index;
	for (var i=0; i<index; i++) {
		trg.options[i].value = optionList[i][0];
		trg.options[i].text = optionList[i][1];
	}
	trg.selectedIndex = 0;
}


function submitForm(form) {
	var theForm = form;
	/*
	var theElements = theForm.elements;
	if (theElements!= null){
		for (i=0;i<theElements.length;i++) {
			var theElement = theElements[i];
			if (theElement.disabled) {
				theElement.disabled = false;
			}
		}
	}
	*/
	theForm.submit();
}

function submitOrder(obj) {
	frm = obj.form;
	frm.submit();
}

/**
* Style Functions
*/
function swapClass(obj, curClass, newClass) {
	if (obj.className.toLowerCase() == curClass.toLowerCase() || obj.className == '') {
		obj.className = newClass;
	} else {
		if (obj.className.toLowerCase() == newClass.toLowerCase() || obj.className == '') {
			obj.className = curClass;
		}
	}
}

/*
 * Funciones Macromedia
 */

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function openView(url) {
	var x = 100;
	var y = Math.floor((screen.height - 300) / 2);
    
    var wup = openWindow(url, 'View', 670, 480, x, y, 'yes', 'no', 'no', 'yes', 'no', 'no', 'no');
    
    if (wup.opener == null) wup.opener = self;
	wup.focus();
}

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


// This function gets called when the end-user clicks on some date.
function selected(cal, date) {
  cal.sel.value = date; // just update the date in the input field.
  if (cal.dateClicked && (cal.sel.id == "sel1" || cal.sel.id == "sel3"))
    // if we add this call we close the calendar on single-click.
    // just to exemplify both cases, we are using this only for the 1st
    // and the 3rd field, while 2nd and 4th will still require double-click.
    cal.callCloseHandler();
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
  cal.hide();                        // hide the calendar
//  cal.destroy();
  _dynarch_popupCalendar = null;
}


// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id, format, showsTime, showsOtherMonths) {
  var el = document.getElementById(id);
  if (_dynarch_popupCalendar != null) {
    // we already have some calendar created
    _dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(1, null, selected, closeHandler);
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      cal.showsOtherMonths = true;
    }
    _dynarch_popupCalendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
  _dynarch_popupCalendar.parseDate(el.value);      // try to parse the text in field
  _dynarch_popupCalendar.sel = el;                 // inform it what input field we use

  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.

  _dynarch_popupCalendar.showAtElement(el, "BR");        // show the calendar

  return false;
}