/*
 * Copyright © Yage. All Rights Reserved.
 *
 * YAGE evolucion digital
 * Av. Brasil 951 y Mariano Echeverría
 * 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;
}

// 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 openCalendarWindow(url, field) {
	url = CONTEXT_PATH + url;
	return modalDialog(url, field, 270, 240, 0, 0, 'yes', 'no', 'raised', 'no', 'no', 'no', 'no');
}

function openPreview(url, photo) {
    var url = CONTEXT_PATH + url;

    url = url + '?parent=' + getOpenerUrl();
    url = url + '&value=' + photo;

	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, value, index) {
    var url = CONTEXT_PATH + url;

    url = url + '?parent=' + getOpenerUrl();
    url = url + '&path=' + path;
    url = url + '&form=' + formName;
    url = url + '&property=' + property;
    url = url + '&value=' + value;
	url = url + '&index=' + index;

	var x = Math.floor((screen.width - 600) / 2);
	var y = Math.floor((screen.height - 400) / 2);
    
    var wup = openWindow(url, 'Upload', 600, 400, x, y, 'yes', 'no', 'no', 'yes', 'no', 'no', 'no');
    
    if (wup.opener == null) wup.opener = self;
	wup.focus();
}

function closeUpload(btn, srcPath, imgPath) {
    var frm = btn.form;
    var frmTarget = frm.form.value;
    var objTarget = frm.property.value;
    
	if (opener.location.href != frm.parent.value) {
		alert("This window is out of context!");
		return;
	}
    
    opener.document.forms[frmTarget].elements[objTarget].value = srcPath;
	imgPhoto = opener.document.getElementById('imgPhoto');
	if (imgPhoto != null && imgPath != null && imgPath.length > 0 ){
		imgPhoto.src = imgPath;
	}
    close();
}


/**
* 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 doAction(objForm, property, action) {
	var obj = objForm.elements[property];
	
	if ( obj != undefined ) {
	
		if (action == 'delete'){
			if( Delete_Click(action) == 1 ){
				obj.value = action;
				disableButtons(objForm);
				objForm.submit();
			}
			else{
				return false;
			}
		}else {
			obj.value = action;
			disableButtons(objForm);
			objForm.submit();
		}
	} else {
		window.alert('The property is not a valid object');
	}
}

function Delete_Click(action)
{
	var sMessage = "Si elimina esta sección, será irrecuperable. No irá a la Papelera de Reciclaje. ¿Desea eliminarla?  " ;
	
	if(action == 'delete')
	{
		if(!confirm(sMessage)) {
			return 0;
		} else {
			return 1;
		}
	}else {
		window.alert('Ha ocurrido un error, por favor comuníquese 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 sección 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 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;
}


/**
* 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;
		}
	}
}

function launchUrl(urlString,paramNames,paramValues) {
	var theParams = new String("");
	var theConstructor = paramNames.constructor.toString();
	var thePosition = theConstructor.indexOf('Array');
	if (thePosition>0) {
		var theSize = paramNames.length;
		for (i=0; i < theSize; i++) {
			theParams = theParams+paramNames[i]+'='+paramValues[i];
			if (i<theSize-1) {
				theParams = theParams+'&';
			}
		}
	} else {
		theParams = paramNames+'='+paramValues;
	}
	window.location = urlString+'?'+theParams;
}


function submitForm(formName) {
	var theForm = document.forms[formName];
	var theElements = theForm.elements;
	for (i=0;i<theElements.length;i++) {
		var theElement = theElements[i];
		if (theElement.disabled) {
			theElement.disabled = false;
		}
	}

	theForm.formAction.value = "load";
	theForm.submit();
}

function showPensumPdf(url) {
	var theWidth = screen.availWidth;
	var theHeight = screen.availHeight;
	var thePercentage = 85;
	theWidth = parseInt(theWidth * thePercentage / 100);
	theHeight = parseInt(theHeight * thePercentage /100);
	var theHelpWindow = window.open(url,'PensumWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width='+theWidth+',height='+theHeight+',left=5,top=5');
	theHelpWindow.focus();
}

