//  wepa.js - JavaScript library for WEPA! (sm)
//
//  (c) 1995-2007 Puerto Rico Internet Corp.
//
//  All Rights Reserved.

var isIE = false; // MSIE 4.0+
var isMO = false; // Mozilla, Netscape 6.0, Firefox 1.5x
var isOP = false; // Opera 8.5x+
var isDOM1 = false; // DOM1 compliant

var ua = window.navigator.userAgent;

if (ua.indexOf("Opera") >= 0)
	isOP = true;

if (ua.indexOf("Gecko") >= 0) {
	isMO = true;
	isDOM1 = true;
}

if (ua.indexOf("MSIE") >= 0) {
  isIE = true;
} else {
	// legacy browser, including Netscape 4.x
}

var hTimeout = 0;	//  used to control timeout for display of menus
var hMenu = 0;		//  used to control menu system

var AmazonSearchUrl = 'http://www.amazon.com/exec/obidos/external-search';

var AmazonCategories = new Object();
	AmazonCategories["AZ1"] = 'books';
	AmazonCategories["AZ2"] = 'music';
	AmazonCategories["AZ3"] = 'video';
	AmazonCategories["AZ4"] = 'classical-music';
	AmazonCategories["AZ5"] = 'toys';
	AmazonCategories["AZ6"] = 'electronics';

  var rxRemove = new Array();
	rxRemove.push( /\s+\w{2} puerto rico$/i );
	rxRemove.push( /\s+\w{2} p\.?r\.?/i );
	rxRemove.push( /^informaci[o\u00F3]n sobre\s+/i );
	rxRemove.push( /^information of\s+/i );
	rxRemove.push( /^puerto rico\s+/i );
	rxRemove.push( /\s+puerto rico$/i );
	rxRemove.push( /\s+p\.?r\.?$/i );

function validQuery(query, lang)
{
  //  Removed automatic word translations
  //

  for(var i=0; i<rxRemove.length; ++i) {
	//
	//  Find bad patterns and remove them from query
	//
	query.value = query.value.replace(rxRemove[i], "");
  }

  //
  //  Note: I have removed the old query parser. We need to build a new query
  //  pre-processor based on the server grammar.
  //
  //  I have also removed the stop word screener. Both the server and the
  //  indexer need to automatically ignore some words.
  //
  //  I have removed the upper-case word screener, mostly because I removed the
  //  split words function in the above steps.
  //
  //  I am no longer warning people if their queries have too many search words.
  //

  query.value = query.value.replace(/^\s+/, "");	// trim() final spaces from the query
  query.value = query.value.replace(/\s+$/, "");

  return true; ////
}



function onMouseOver(obj) {}
function onMouseOverMenu(obj) {}
// experimental
function onMouseOverSpecial(obj) {}
function onMouseOut(obj) {}


function getAmazonSearchLink(sMode, sKeyword)
{
  return AmazonSearchUrl +
	'?tag=wepreg&mode=' + sMode + '&keyword=' + sKeyword;
}


// This one is called whenever a different collection is selected
// using the select box on the search form.
   
function handleOnChangeSearch(th)
{
  var i = th.selectedIndex;     // some integer
  var v = th.options[i].text;   // Country Name
  var c = th.options[i].value;  // vi

  if (th.name == "cCountry") {
    if (th.form.lang.value == 'es') {
      window.status = "Buscando ahora en "+v;
      if (c == 'do') {
        window.alert('Pronto estará lista!');
      }
    } else {
      window.status = "Searching now in "+v;
      if (c == 'do') {
        window.alert('Coming Soon!');
      }
    }

    // adjust Form Action

    if ((c == 'pr') || (c == 'vi')) {
      th.form.action = '/index-' + th.form.lang.value + '.html';
      return true;
    }
    if (c.substring(0,2) == 'AZ') {
      th.form.action = AmazonSearchUrl;
      return true;
    }
    th.form.action = '/' + c + '/index-' + th.form.lang.value + '.html'; 

    return true;
  } // cCountry

  return true;
}


// This one is called just after clicking the submit button
// for a search query.

function handleOnSubmitSearch(th)
{
  var i = th.cCountry.selectedIndex;     // some integer
  var v = th.cCountry.options[i].text;   // Country Name
  var c = th.cCountry.options[i].value;  // vi

  if ((th.query.value == null) || 
      (th.query.value == ""))
  {
    if (th.lang.value == 'en') {
      window.alert('Need to enter a query!');
    } else {
      window.alert('Ponga una palabra o frase en el encasillado!');
    }
    return false;
  }

  if (c == 'do') {
    window.alert('Coming Soon!');
    return false;
  }

  var c2 = c.substring(0,2);

  if (c2 == 'AZ') {
    window.open( 
      getAmazonSearchLink(AmazonCategories[c], th.query.value), 'amazon');
    return false;
  }
	
  return validQuery(th.query, th.lang.value);
}


//  The following four functions are used by search results.
//
function clk(th,qt,cDB,lang,i)
{
  var q = 'http://' + th.hostname + '/index-' + lang 
	+ '.html?query=' + qt + '&cCountry=' + cDB + '&lang=' + lang + '&i=' +i;

  th.href = q;

  return true;
}

function clk2(th,qt,cDB,lang,i,g)
{
  var q = 'http://' + th.hostname + '/index-' + lang 
	+ '.html?query=' + qt + '&cCountry=' + cDB + '&lang=' + lang + '&i=' +i;

  if ( g > 0 ) {
	q = q + '&g=' + g;
  }

  th.href = q;

  return true;
}

function tease(th,qt,cDB,lang,i)
{
  var link = 'http://' + th.hostname + '/index-' + lang 
	+ '.html?query=' + qt + '&cCountry=' + cDB + '&lang=' + lang + '&i=' +i;
  window.status = link;

  return true;
}

function tease2(th,qt,cDB,lang,i,g)
{
  var link = 'http://' + th.hostname + '/index-' + lang 
	+ '.html?query=' + qt + '&cCountry=' + cDB + '&lang=' + lang + '&i=' +i;

  if ( g > 0 ) {
	link = link + '&g=' + g;
  }

  window.status = link;

  return true;
}


// Code to handle bookmarks
//
function bmGlobal(th)
{
 if (isIE) {
	document.all.mywepa1.style.visibility = 'hidden';
	document.all.mywepa2.style.visibility = 'visible';
	document.all.mywepa3.style.visibility = 'hidden';
 }
 if (isDOM1) {
	document.getElementById("id_mywepa1").style.visibility = 'hidden';
	document.getElementById("id_mywepa2").style.visibility = 'visible';
	document.getElementById("id_mywepa3").style.visibility = 'hidden';
 }

 return false;
}

function bmLocal(th)
{
 if (isIE) {
	document.all.mywepa3.style.visibility = 'hidden';
	document.all.mywepa2.style.visibility = 'hidden';
	document.all.mywepa1.style.visibility = 'visible';
 }
 if (isDOM1) {
	document.getElementById("id_mywepa3").style.visibility = 'hidden';
	document.getElementById("id_mywepa2").style.visibility = 'hidden';
	document.getElementById("id_mywepa1").style.visibility = 'visible';
 }
 return false;
}

function bmPersonal(th)
{
 if (isIE) {
	document.all.mywepa1.style.visibility = 'hidden';
	document.all.mywepa2.style.visibility = 'hidden';
	document.all.mywepa3.style.visibility = 'visible';
 }
 if (isDOM1) {
	document.getElementById("id_mywepa1").style.visibility = 'hidden';
	document.getElementById("id_mywepa2").style.visibility = 'hidden';
	document.getElementById("id_mywepa3").style.visibility = 'visible';
 }
 return false;
}


function getCookieVar(myCookie, key)
{
  var pos = myCookie.indexOf(key+"=");

  if (pos != -1) {
    var start = pos + key.length + 1;
    var end = myCookie.indexOf(";", start);
    if (end == -1) end = myCookie.length;
    var value = unescape(myCookie.substring(start, end));
    return value;
  }

  return null;
}

function bmEditMozilla(e)
{
  if (e.shiftKey) { bmEdit0(e, e.currentTarget); return false; }

  if (e.ctrlKey) { alert('ctrl Key'); return false; }

  return true;
}

function bmEdit(th)
{
  var e = th.document.parentWindow.event;

  if (e.shiftKey) { bmEdit0(e, th); return false; }

  if (e.ctrlKey) {
//    var bm = new Array("p1|http://www.wepa.com/|Test #1");
//    bmSet(bm);
    return false;
  }

  return true;
}

function bmEdit0(e, th)
{
  var cUrl = prompt('New URL for this link: ', th.href);

  if (cUrl != null)
	th.href = cUrl;

  var cLabel = prompt('New Label for this link: ', th.innerHTML);

  if (cLabel != null)
	th.innerHTML = cLabel;

  var bm = new Array();
  var pLinks;
  if (isIE) pLinks = th.document.links;
  if (isMO) pLinks = window.frames.mywepa3.document.links;
  var j = 0;

  for (var i=0; i<pLinks.length; i++) {
    var myLink = pLinks[i];
    if (myLink.id != "") {
      if (myLink.innerHTML.indexOf("Link #") != 0)
        bm[j++] = myLink.id + "|" + myLink.href + "|" + myLink.innerHTML;
    }
  } // for i

  if (j>0) bmSet(bm);
}


function bmOnLoad()
{
  var vBm = getCookieVar(document.cookie, 'bookmarks');

  if ((vBm != null) && (vBm != "")) {
    var llist = vBm.split("\n");

    for(var i=0; i<llist.length; i++) {
      var params = llist[i].split("|"); // link id, URL, URL name

      var cmd1; var cmd2;

      if (isIE) {
        cmd1 = "document.frames.mywepa3.document.all." + params[0]
	  + ".href = '" + params[1] +"'";
        cmd2 = "document.frames.mywepa3.document.all." + params[0]
	  + ".innerHTML = '" + params[2] +"'";
        eval(cmd1); eval(cmd2);
      } else { // isMO
        var ctl = window.frames.mywepa3.document.getElementById(params[0]);
	    ctl.href = params[1];
	    ctl.innerHTML = params[2];
      } // if IE
    } // for i
  } // if cookie not empty

  // Add Mozilla event listeners

  if (isMO) {
    for(var i=0; i<25; i++) {
      var ctlId = 'p' + i;
      var ctl = window.frames.mywepa3.document.getElementById(ctlId);
          ctl.addEventListener("click", bmEditMozilla, false);
    } // for i
  } // if Mozilla

  return true;
}


function bmSet(myBM)
{
 var expDate = new Date();
     expDate.setFullYear(expDate.getFullYear() + 10);
 var expires = "; expires=" + expDate.toGMTString();

 var path = "";		if (isIE) path = "; path=/";

 var domain = "";	if (isIE) domain = "; domain=wepa.com";

 document.cookie = "bookmarks=" + escape(myBM.join("\n")) 
	+ expires + path + domain;
}


// The functions below implement the menu system... 2003/12/26

function getObjById(vId)
{
  if ( isDOM1 ) {
	return document.getElementById(vId);
  } else {
	return document.all[vId];
  }
}


function setVisibilityById(myId, kVisibleOrHidden)
{
  getObjById(myId).style.visibility = kVisibleOrHidden;
}

function setVisibility(myObj, kVisibleOrHidden)
{
  myObj.style.visibility = kVisibleOrHidden;
}

function onMouseOverMenu(menuId) {
  //
  //  Responds to a MouseOver over a Menu by displaying the full Menu.
  //
  if (menuId != hMenu)
        hideMenu();

  var ctl0 = getObjById("id_menubar1");
  var top0 = ctl0.offsetTop; // 16
  var left0 = ctl0.offsetLeft; // 0

//    alert("("+ left0 + ","+ top0 +")");

  var ctl1;

  switch( menuId) {
	case 1:
		ctl1 = getObjById("id_menu1");
		break;
	case 2:
		ctl1 = getObjById("id_menu2");
		break;
	case 3:
		ctl1 = getObjById("id_menu3");
		break;
	case 4:
		ctl1 = getObjById("id_menu4");
		break;
  }

  var top1 = ctl1.offsetTop; // 0
  var left1 = ctl1.offsetLeft; // 408

//    alert("("+ left1 + ","+ top1 +")");

  var top = top0 + 18;
  var left = left0 + left1;

//    alert("("+ left + ","+ top +")");

  var pMenu;

  switch( menuId ) {
	case 1:
		pMenu = getObjById("id_menu101");
		setVisibility(pMenu, 'visible');
		pMenu.style.left = left;
		pMenu.style.top  = top;
		break;
	case 2:
		pMenu = getObjById("id_menu202");
		setVisibility(pMenu, 'visible');
		pMenu.style.left = left;
		pMenu.style.top  = top;
		break;
	case 3:
		pMenu = getObjById("id_menu303");
		setVisibility(pMenu, 'visible');
		pMenu.style.left = left;
		pMenu.style.top  = top;
		break;
	case 4:
		pMenu = getObjById("id_menu404");
		setVisibility(pMenu, 'visible');
		pMenu.style.left = left;
		pMenu.style.top  = top;
		break;
  }

  hMenu = menuId;

  return false;
}

function onMouseOutMenu()
{
  clearTimer();
  setTimer();
}


function clearTimer()
{
  if (hTimeout != 0)
        window.clearTimeout(hTimeout);
  hTimeout = 0;
}

function setTimer()
{
  hTimeout = window.setTimeout('hideMenu()', 2000);
}

function resetTimer()
{
  clearTimer();
  setTimer();
}

function onMouseUpBody()
{
  hideMenu();

  return false;
}


function hideMenu() {
  //
  //  Hide the menu being displayed
  //
  switch(hMenu) {
	case 1:
		setVisibilityById("id_menu101", 'hidden');
		break;
	case 2:
		setVisibilityById("id_menu202", 'hidden');
		break;
	case 3:
		setVisibilityById("id_menu303", 'hidden');
		break;
	case 4:
		setVisibilityById("id_menu404", 'hidden');
		break;
  }

  hMenu = 0;
}

function onMouseOverMenuItems()
{
  clearTimer();
}

function onMouseOutMenuItems()
{
  resetTimer();
}


function launchSRW1 (lang) {
  //
  //  Launches the Site Registration Wizard on a separate, small window
  //
  var url = lang == 'en' ? '/srw1/index-en.html' : '/srw1/index-es.html';

  window.open(url,'wizard', 'height=300,width=370,scrollbars=no,resizable=yes');
}


