// JavaScript Document
// ----------------------------------------------------------------------------
//   browser / feature detection
// ----------------------------------------------------------------------------

isIE = window.clientInformation ? true : false
isIEDTD = ((document.doctype && document.doctype.name.indexOf(".dtd")>-1) || document.compatMode == "CSS1Compat") ? true : false;
isN4 = document.layers ? true : false
isN6  = navigator.appName == "Netscape" && parseInt(navigator.appVersion) >= 5
isO5 = navigator.userAgent.indexOf("Opera") != -1 && parseInt(navigator.appVersion) >= 4

ie = document.all != null && !isO5;
safari = navigator.userAgent.indexOf("Safari/125") != -1;
moz = !ie && !safari && document.getElementById != null && document.layers == null && navigator.userAgent.indexOf("Netscape/7.02") == -1 && (navigator.userAgent.indexOf("Gecko") || navigator.userAgent.indexOf("Firefox"));
mac = (navigator.userAgent.indexOf("Macintosh") != -1 || navigator.userAgent.indexOf("Mac_PowerPC") != -1);

// ----------------------------------------------------------------------------
//   form submission / effects processing
// ----------------------------------------------------------------------------
function emptyvalidation(entered, alertbox){
	with (entered){
		if (value==null || value==""){
			if (alertbox!=""){
				alert(alertbox);
			}
				return false;
			}else{
				return true;
		}
	}
}

function radiovalidation(entered, alertbox){
	if (entered.type == null){
		var blnchecked = false;
		for (var j = 0; j < entered.length; j++){
					if (entered[j].checked){
						blnchecked = true;
					}
				}
		if (!blnchecked){
			(alertbox)
				{alert(alertbox);} 
			return false;
		}
	}
}

function emailvalidation(entered, alertbox){
	with (entered){
		apos=value.indexOf("@"); 
		dotpos=value.lastIndexOf(".");
		lastpos=value.length-1;
		if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2){
			if (alertbox) {alert(alertbox);} return false;
		}else{
			return true;
		}
	}
}

function limitNumbers() {
	if (event.keyCode < 44 || event.keyCode > 57) event.returnValue = false;
}

// cuenta y limita los campos de textarea.
function limit(what,chars,counter) {
	var d=document;
	if (what.value.length > chars) {
		what.value=what.value.substr(0,chars);
		alert('Solamente está permitido '+chars+' carácteres en este campo');
	}
	counting = (chars - what.value.length);
	c = document.getElementById(counter);
	c.innerHTML = counting;
}

function statusifyElements(root) {
  var as = root.getElementsByTagName("a");
  for (var i = 0; i < as.length; i++) {
    var anchor = as[i];
    if (anchor.title) {
      anchor.onmouseover = function() { window.status = this.title; return true; }
      anchor.onmouseout = function() { window.status = ''; return true; }
    }
  }
  var as = root.getElementsByTagName("img");
  for (var i = 0; i < as.length; i++) {
    var img = as[i];
    if (img.title) {
      img.onmouseover = function() { window.status = this.title; return true; }
      img.onmouseout = function() { window.status = ''; return true; }
    }
  }
}

function urlAttributeAdjust(url, name, val) {

  if (url.match(new RegExp("(\\?|\\&)" + name + "=([^&]*)", "g"))) {
    url= url.replace(new RegExp("(\\?|\\&)" + name + "=([^&]*)", "g"), "$1" + name + "=" + val);
  } else {
    if (url.indexOf("?") == -1) {
      url = url + "?" + name + "=" + val;
    } else {
      url = url + "&" + name + "=" + val;
    }
  }
  return( url );

}

function disableButtonForAction(buttonObj) {
  buttonObj.disabled = 'true';
//  buttonObj.value = 'Enviando..';
}

function smartSubmit(buttonObj, formName) {
  //return formvalidation();
  disableButtonForAction(buttonObj);
  document.forms[formName].submit();
  return true;
}

function smartLocation(buttonObj, loc) {
  disableButtonForAction( buttonObj );
  document.location = loc;
  return true;
}

function smartGoBack(buttonObj) {
  disableButtonForAction( buttonObj );
  history.go(-1);
  return true;
}

function smartCancel() {
  window.close();
}

function scrollToElement(name) {
  window.scrollTo(0, getYCoord(document.getElementById(name)) - 100 );
}

functionMap = new Array();
oldButtonColors = new Array();
numericChars = "0123456789";


function inputContainsErrors(targetId) {

  // method 1: fine errorText in it's standard title

  var titleObj = document.getElementById(targetId + '_title');
  if (titleObj) {
    var scanDivs = titleObj.getElementsByTagName("div");
    for (var i = 0; i < scanDivs.length; ++i) {
      if (scanDivs[i].className == "errorText") { return( true ); }
    }
  }
  return( false );

}

function autoFocus(formName, defaultFocus) {

  var f = document.forms[formName];
  if (!f) return;

  var elCount = f.elements.length;
  var errorsSeen = false;
  var focusEl = false;

  for (var i = 0; i < elCount; ++i) {
    el = f.elements[i];
    if (el.type == 'text' || el.type == 'edit' || el.type == 'password' || el.type == 'textarea' || el.type == 'checkbox' || el.type == 'radio' || el.type == 'select-one') {
      if (inputContainsErrors( el.id ) && !focusEl) {
        errorsSeen = true;
        focusEl = el;
      } else if (defaultFocus && !focusEl) {
        focusEl = el;
      }
    }
  }

  if (focusEl) focusEl.focus();

}

function initFormEffects(formName) {

  var f = document.forms[formName];
  if (!f) return;

  var elCount = f.elements.length;

  for (var i = 0; i < elCount; i++) {

    el = f.elements[i];

    if (el.type == 'text' || el.type == 'edit' || el.type == 'password' || el.type == 'textarea' || el.type == 'checkbox' || el.type == 'select-one' || el.type == 'input') {

      var inSequence = false;
      var elNum, elName;
      var n = el.name;
      for (j=0; j < n.length; j++) {
        if (numericChars.indexOf( n.charAt(j) ) == -1) continue; // if we found a number in the string
        elName = n.substring(0, j);
        elNum  = n.substring(j);
        // if this is not the first item, and it doesnt have its own title, its part of a sequence
        if (elNum != "1" && document.getElementById(elName + '_title')) { inSequence = true; }
        break;
      }

      if (inSequence) {
        focusObjName = elName + '1_title';
      } else {
        focusObjName = el.name + '_title';
      }

      o = document.getElementById(focusObjName);
      if (o != null) {
        functionMap[el.name] = focusObjName;
        if (el.type == 'checkbox') {
          functionMap[o.id] = focusObjName;
        }
        el.onfocus = function() { document.getElementById(functionMap[this.name]).style.fontWeight = 'bold'; };
        el.onblur  = function() { document.getElementById(functionMap[this.name]).style.fontWeight = 'normal'; };
      }

    } else if (el.type == 'button' || el.type == 'submit') {

       //el.onfocus = function() { oldButtonColors[this.name] = this.style.backgroundColor; this.style.backgroundColor = '#BBBBBB'; };
       //el.onblur  = function() { this.style.backgroundColor = oldButtonColors[this.name]; };

    }

  }

}

// ----------------------------------------------------------------------------
// popup image
// ----------------------------------------------------------------------------

function openImage(size, orientation, idFoto) {
  var w = 0, h = 0, root = "";
  if (size == 'small') {
	  	root = "../_comun/foto_p.asp";
	  	if (orientation == 'horizontal') {
			w = 107;
			h = 80;
		} else if (orientation = 'vertical') {
			w = 80;
			h = 107;
		}
  } else if (size == 'medium') {
	  	root = "../_comun/foto_m.asp";
	  	if (orientation == 'horizontal') {
			w = 200;
			h = 150;
		} else if (orientation = 'vertical') {
			w = 150;
			h = 200;
		}
  } else if (size == 'large') {
	  	root = "../_comun/foto_g.asp";
	  	if (orientation == 'horizontal') {
			w = 300;
			h = 225;
		} else if (orientation == 'vertical') {
			w = 225;
			h = 300;
		}
  } else if (size == 'superlarge') {
	  	root = "../_comun/foto_ar.asp";
	  	if (orientation == 'horizontal') {
			w = 800;
			h = 600;
		} else if (orientation = 'vertical') {
			w = 600;
			h = 800;
		}
  }
  window.open(root + '?id=' + idFoto + '&FotoOrient=' + orientation, size,'width=' + w + ',height=' + h + ',scrollbars=no,resizable=no,titlebar=no,menubar=no,toolbar=no,top=100,left=10');
}

// ----------------------------------------------------------------------------
//   popup Contenido Html
// ----------------------------------------------------------------------------

function verContHtml(idCont) {
	var w = 0, h = 0, root = "";
	root = "../1/1_1_verCont.asp";
	w = 650;
	h = 500;
	window.open(root +'?idCont=' + idCont,'verCont','width=' + w + ',height=' + h + ',scrollbars=yes,resizable=no,titlebar=no,menubar=no,toolbar=no,top=100,left=10');
}

function editContHtml(idCont) {
	var w = 0, h = 0, root = "";
	root = "../1/1_1_editCont.asp";
	w = 650;
	h = 600;
	window.open(root +'?idCont=' + idCont,'verCont','width=' + w + ',height=' + h + ',scrollbars=yes,resizable=no,titlebar=no,menubar=no,toolbar=no,top=100,left=10');
}
// ----------------------------------------------------------------------------
//   popup Print Function
// ----------------------------------------------------------------------------

function PrintPage(Page) {
	window.open(Page,'PrintPage','width=710,height=700,scrollbars=yes,resizable=no,titlebar=no,menubar=no,toolbar=no,top=100,left=10');
}

function prePrint(){
	document.all.printWindow.style.visibility = "hidden";
}


// ----------------------------------------------------------------------------
//   add string helpers
// ----------------------------------------------------------------------------

Date.prototype.getFullYear =  function () { return( window.moz || window.safari ? this.getYear() + 1900 : this.getYear() ); }

String.prototype.trim =  function () { return this.replace(/^\s+/,'').replace(/\s+$/,''); }

function escapeForJS(str) {
  return( str.replace(/"\'"/g, "\\\'").replace(/"\""/g, "\\\"") );
}

function capitalizeFirst(s) {
  return( s.substring(0,1).toUpperCase() + s.substring(1).toLowerCase() );
}

String.prototype.endsWith = function(sEnd) {
  return (this.substr(this.length-sEnd.length)==sEnd);
}

String.prototype.startsWith = function(sStart) {
  return (this.substr(0,sStart.length)==sStart);
}

// ----------------------------------------------------------------------------
//   global functions
// ----------------------------------------------------------------------------

function copyToClipboard(resourceName, str, resourceIsText) {
  if (!resourceIsText) { resourceName = "\'" + resourceName + "\'"; }
  if (window.ie) {
    window.clipboardData.setData('text', str);
    showHelpTip(window.event, '<strong>URL Copied to Clipboard</strong><br/>The URL for ' + resourceName + ' has been copied to your clipboard.  To give this file to someone, simply paste this URL to them.' );
  } else {
    showObject( document.getElementById("clipboardTextContainer") );
    document.getElementById("clipboardTextFilename").innerHTML = resourceName + " ";
    document.getElementById("clipboardTextContent").innerHTML = str;
  }
}

function noop() { }

function getMouseRelativeX() {
  return( parseInt(window.event.clientX) );
}
function getMouseRelativeY() {
  return( parseInt(window.event.clientY) );
}

function getMouseX(e)
{
  if (!e) var e = window.event;
  if (e.pageX) return e.pageX;
  return e.clientX + getViewportScrollX();
}
function getMouseY(e)
{
  if (!e) var e = window.event;
  if (e.pageY) return e.pageY;
  return e.clientY + getViewportScrollY();
}

function getWidth(obj) {
  return( parseInt(obj.offsetWidth) );
}
function getHeight(obj) {
  return( parseInt(obj.offsetHeight) );
}
function getLeft(obj) {
  return( parseInt(obj.offsetLeft) );
}

function getTop(obj) {
  return( parseInt(obj.offsetTop) );
}

function setLeft(obj, distance) {
  obj.style.left = distance + "px";
}

function setTop(obj, distance) {
  obj.style.top = distance + "px";
}

function getXCoord(obj) {
  x = getLeft(obj);
  while (obj.offsetParent.tagName != "BODY" && obj.offsetParent.tagName != "HTML") {
    obj = obj.offsetParent;
    x += getLeft(obj);
  }
  return( x );
}
function getYCoord(obj) {

  y = getTop(obj);
  while (obj.offsetParent.tagName != "BODY" && obj.offsetParent.tagName != "HTML") {
    obj = obj.offsetParent;
    y += getTop(obj);
  }
  return( y );
}

function isHidden(obj) {
  return( obj.style.display == "none" );
}

function isVisible(obj) {
  return( !isHidden(obj) );
}

function hideObject(obj) {
  if (obj) obj.style.display = "none";
}
function showObject(obj) {
  if (obj) obj.style.display = "";
}

function toggleVisibility(obj) {
  if (isHidden(obj)) {
    showObject(obj);
  } else {
    hideObject(obj);
  }
}

function trim(str) {
  var idx_start = 0;
  var idx_end = str.length - 1;
  while (str.charAt(idx_start) == ' ') { idx_start++; }
  while (str.charAt(idx_end) == ' ') { idx_end--; }
  return( str.substr(idx_start, (idx_end - idx_start + 1)) );
}

function trimNums(str) {
  var idx_start = 0;
  var idx_end = str.length - 1;
  while (str.charAt(idx_start) == ' ' || !isNaN(str.charAt(idx_start)) || str.charAt(idx_start) == '_') { idx_start++; }
  while (str.charAt(idx_end) == ' ' || !isNaN(str.charAt(idx_end)) || str.charAt(idx_end) == '_') { idx_end--; }
  return( str.substr(idx_start, (idx_end - idx_start + 1)) );
}

function getWindowHeight() {
  if (ie) {
    return( document.body.offsetHeight );
  } else {
    return( window.innerHeight );
  }
}

function getWindowWidth() {
  if (ie) {
    return( document.body.offsetWidth );
  } else {
    return( window.innerWidth );
  }
}

function expandWindowHeight(factor) {
  if (ie) {
    window.resizeBy( 0, factor );
  } else {
    window.innerHeight = window.innerHeight + factor;
  }
}

function expandWindowWidth(factor) {
  if (ie) {
    window.resizeBy( factor, 0 );
  } else {
    window.innerWidth = window.innerWidth + factor;
  }
}

function debug(x) {
  if( window.status.length > 0 )
    window.status = window.status + ' | ' + x;
  else
    window.status = x;
}


function getWindowScroll() {
  if (isIEDTD) {
    return document.documentElement.scrollTop;
  }
  else if (isIE)  return document.body.scrollTop;
  else          return window.pageYOffset
}

function getIFrameDocument(fName) {
  if (window.moz) {
    return( document.getElementById(fName).contentWindow.document );
  } else {
    return( window.frames[fName].document );
  }

}




// ----------------------------------------------------------------------------
//   viewport functions
// ----------------------------------------------------------------------------

function getViewportHeight() {
  if (self.innerHeight) // all except Explorer
  {
    y = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight)
    // Explorer 6 Strict Mode
  {
    y = document.documentElement.clientHeight;
  }
  else if (document.body) // other Explorers
  {
    y = document.body.clientHeight;
  }
  return( y );
}

function getViewportWidth() {
  if (self.innerHeight) // all except Explorer
  {
    x = self.innerWidth;
  }
  else if (document.documentElement && document.documentElement.clientHeight)
    // Explorer 6 Strict Mode
  {
    x = document.documentElement.clientWidth;
  }
  else if (document.body) // other Explorers
  {
    x = document.body.clientWidth;
  }
  return( x );
}


function getViewportScrollX() {
  if (self.pageYOffset) // all except Explorer
  {
    x = self.pageXOffset;
  }
  else if (document.documentElement && document.documentElement.scrollTop)
    // Explorer 6 Strict
  {
    x = document.documentElement.scrollLeft;
  }
  else if (document.body) // all other Explorers
  {
    x = document.body.scrollLeft;
  }
  return( x );
}

function getViewportScrollY() {
  if (self.pageYOffset) // all except Explorer
  {
    y = self.pageYOffset;
  }
  else if (document.documentElement && document.documentElement.scrollTop)
    // Explorer 6 Strict
  {
    y = document.documentElement.scrollTop;
  }
  else if (document.body) // all other Explorers
  {
    y = document.body.scrollTop;
  }
  return( y );
}


// ----------------------------------------------------------------------------
//   cookie functions
// ----------------------------------------------------------------------------

function setCookie(sName, sValue, expires, path, domain) {
  document.cookie = sName + "=" + escape(sValue) + (expires == null ? "" : "; expires=" + expires) + ((path == null)   ? "" : "; path=" + path) + ((domain == null)  ? "" : "; domain=" + domain);
}

function delCookie(sName, path, domain) {
  document.cookie = sName + "=" + escape(getCookie(sName)) + ";expires=" + new Date("December 31, 1975").toGMTString() + ((path == null) ? "" : "; path=" + path) + ((domain == null)  ? "" : "; domain=" + domain);
}

function getCookie(sCookie) {
  var aCookie = document.cookie.split(";");
  for (var i = 0; i < aCookie.length; i++)
  {
    var aCrumb = aCookie[i].split("=");
    if (sCookie == trim(unescape(aCrumb[0]))) {
      return( unescape(aCrumb[1]) );
    }
  }
  return( null );
}

function getCookieDomain(fullhost) {
  var hs = fullhost.split(".");
  if (hs.length - 2 < 0) {
    return( "" );
  } else {
    return( "." + hs[hs.length - 2] + "." + hs[hs.length - 1] );
  }
}

function toggleContentElement(srcCheckbox, tgtElement) {
  if ( srcCheckbox.checked ) {
    showObject(tgtElement);
  } else {
    hideObject(tgtElement);
  }
}

function toggleContentElementByValue(val, tgtElement) {
  if ( val ) {
    showObject(tgtElement);
  } else {
    hideObject(tgtElement);
  }
}


// ----------------------------------------------------------------------------
//   ie emulation (erik arvidson)
// ----------------------------------------------------------------------------

if (!safari && /Mozilla\/5\.0/.test(navigator.userAgent)) {

  HTMLElement.prototype.insertAdjacentHTML = function (sWhere, sText) {

     var r = document.createRange();
     switch (sWhere) {
      case "beforeBegin":
       r.setStartBefore(this);
       this.parentNode.insertBefore(r.createContextualFragment(sText), this);
       break;

      case "afterBegin":
       r.setStartBefore(this.firstChild);
       this.insertBefore(r.createContextualFragment(sText), this.firstChild);
       break;

      case "beforeEnd":
       r.setStartAfter(this.lastChild);
       this.appendChild(r.createContextualFragment(sText));
       break;

      case "afterEnd":
       r.setStartAfter(this);
       this.parentNode.insertBefore(r.createContextualFragment(sText), this.nextSibling);

       break;
     }

  }


}


// ----------------------------------------------------------------------------
//   form radio button helpers
// ----------------------------------------------------------------------------

function setRadioValue(formname, radioname, val) {

  obj = document.forms[formname][radioname];
  for(i=0;i<obj.length;i++) {
    if (obj[i].value == val) obj[i].checked = true;
  }

}

function getRadioValue(formname, radioname) {

  obj = document.forms[formname][radioname];
  for(i=0;i<obj.length;i++) {
    if (obj[i].checked) return( obj[i].value );
  }

}

function getRadio(formname, radioname, val) {

  obj = document.forms[formname][radioname];

  for(i=0;i<obj.length;i++) {
    if (obj[i].value == val) return( obj[i] );
  }

}

function getSelectOptionTitleByValue(formname, fieldName, val) {

  obj = document.forms[formname][fieldName];
  for (var i = 0; i < obj.length; ++i) {
    if (obj[i].value == val) { return( obj[i].text ); }
  }
  return( null );

}

function setFieldValue(formname, fieldName, val) {

  obj = document.forms[formname][fieldName];

  if (obj.tagName.toLowerCase() == "select") {

    for (var i = 0; i < obj.length; ++i) {
      if (obj[i].value == val) { obj.selectedIndex = i; break; }
    }

  } else {

    obj.value = val;

  }

}

function setFieldDisabled(formname, fieldName) {

  obj = document.forms[formname][fieldName];
  obj.style.color = '#727272';
  obj.style.backgroundColor = '#F9F9F9';
  obj.readOnly = 1;

}

function setFieldEnabled(formname, fieldName) {

  obj = document.forms[formname][fieldName];
  obj.style.color = '#000000';
  obj.style.backgroundColor = '#FFFFFF';
  obj.readOnly = 0;

}

function addEvent(obj, et, f) {
  if (obj.addEventListener) {
    obj.addEventListener(et, f, false);
    return( true );
  } else if (obj.attachEvent) {
    var r = obj.attachEvent('on' + ret, f);
    return( r );
  }
  return( false );
}

function cleanForHTML(s) {

  s = s.replace(new RegExp("(“|”)", "g"), "\"");
  s = s.replace(new RegExp("(–|—)", "g"), "--");
  s = s.replace(new RegExp("(’|’)", "g"), "'");
  s = s.replace(new RegExp("…", "g"), "...");

  return( s );

}
