/*
 * 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 preUrl='';
if(document.location.href.indexOf('www.transelectric.com.ec')>0){
	preUrl = 'http://www.transelectric.com.ec:8080';
}
if(document.location.href.indexOf('srvweb')>0){
	preUrl = 'http://srvweb:8080';
}
if(document.location.href.indexOf('208.19.64.4')>0){
	preUrl = 'http://208.19.64.4:8080';
}
    var url = preUrl + 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 ) {
		obj.value = action;
		disableButtons(objForm);
		objForm.submit();
	} else {
		window.alert('The property is not a valid object');
	}
}

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;
	}
}

/**
* 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 showHide(name, imgId, io, ic) {
	var obj = document.getElementById(name);
	var display = obj.style.display;
	
	if (display.toLowerCase() == 'none' || obj.style.display == '') {
		obj.style.display = 'block';
	} else {
		if (display.toLowerCase() == 'block' || obj.style.display == '') {
			obj.style.display = 'none';
		}
	}
	
	if (imgId != undefined) {
		var img = document.getElementById(imgId);
		if (img != undefined) {
			if (img.src.indexOf(io) != -1) {
				img.src = ic;
			} else {
				img.src = io;
			}
		}
	}
}


/*
 * Custom Functions
 */
 
function redirectContact(form) {
	form.elements['sectionCode'].value = 21; //Ayudanos a Crecer
	form.elements['action'].value = '';
	form.submit();
}

function showProductMain() {
	var a = document.getElementById('prodMain');
	var b = document.getElementById('prodCommerce');
	
	a.style.display = 'block';
	b.style.display = 'none';
	return false;
}

function showProductCommerce() {
	var a = document.getElementById('prodMain');
	var b = document.getElementById('prodCommerce');
	
	a.style.display = 'none';
	b.style.display = 'block';
	return false;
}

function jobApply() {
	var a = document.getElementById('jobApply');
	a.style.display = 'block';
	return false;
}

function popupGame(url) {
	url = CONTEXT_PATH + '/jsp/common/games.jsp?file='  + url;
	return openWindow(url, 'Ferrero', 300, 300, 100, 100, 'yes', 'no', 'no', 'yes', 'no', 'no', 'yes');
}


/**********************************************************
 * Image Functions
 *********************************************************/

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];}
}
