/*
 * Copyright ©2003 MINDSOFT. All Rights Reserved. 
 *
 * MINDSOFT Cia. Ltda.
 * Pasaje Giron Oe3-30 y America
 * Quito-Ecuador
 * www.mindsoft.biz
 */
 
var da = (document.all) ? 1 : 0;
var pr = (window.print) ? 1 : 0;
var mac = (navigator.userAgent.indexOf("Mac") != -1); 

function printPage() {
  if (pr) // NS4, IE5
    window.print()
  else if (da && !mac) // IE4 (Windows)
    vbPrintPage()
  else // other browsers
    alert("Sorry, your browser doesn't support this feature.");
  return false;
}
 
 function winStatus(msg) {
	window.status = msg;
}

function linkTo(sectionCode) {
	var url = CONTEXT_PATH + '/jsp/user/go.do?sectionCode=' + sectionCode;
	setLocation(url);
}

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 openPreviewDesc(url, photo, desc) {
    var url = CONTEXT_PATH + url;

    url = url + '?parent=' + getOpenerUrl();
    url = url + '&value=' + photo;
	url = url + '&desc=' + desc;

	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, sequence, index, w, h, xi, yi) {
    var url = CONTEXT_PATH + url;
    url = url + '?parent=' + getOpenerUrl();
    url = url + '&path=' + path;
    url = url + '&form=' + formName;
    url = url + '&property=' + property;
    url = url + '&prefix=' + value;
	url = url + '&sequence=' + sequence;
	url = url + '&index=' + index;

	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 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;
	}
}


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 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;
}

/**Functio to Open a PopUp Search Form */
function openSearch(url, f, propList, labelList, index) {
    var url = CONTEXT_PATH + 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', 750, 400, x, y, 'yes', 'no', 'no', 'yes', 'no', 'no', 'no');
    
    if (wup.opener == null) wup.opener = self;
	wup.focus();
}

function openSearch2(url, f, p, l, i) {
    var url = CONTEXT_PATH + url;

    url = url + '&parent=' + getOpenerUrl();
    url = url + '&form=' + f;
    url = url + '&property=' + p;
    url = url + '&label=' + l;
	url = url + '&index=' + i;

	var x = Math.floor((screen.width - 600) / 2);
	var y = Math.floor((screen.height - 400) / 2);

	var wup = openWindow(url, 'Search', 600, 400, x, y, 'yes', 'no', 'no', 'yes', 'no', 'no', 'yes');
    
    if (wup.opener == null) wup.opener = self;
	wup.focus();
}

function openParamSearch(url, f, propList, labelList, index) {
    var url = CONTEXT_PATH + 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', 750, 400, x, y, 'yes', 'no', 'no', 'yes', 'no', 'no', 'no');
    
    if (wup.opener == null) wup.opener = self;
	wup.focus();
}

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 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 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;
}

/*
*  Verifica si el campo esta  o no vacio
*   Si esta vacio devuelve TRUE caso contrario FALSE
*/
function isEmpty (vtext) {
	var ok = true;
	if ( vtext != '' ) {
		for (var i=0; i<vtext.length; i++)
			if (vtext.charAt(i) != ' ')
				ok = false;
	}
	return(ok);
}

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

function submitOrder(obj) {
	frm = obj.form;
	frm.submit();
}