function Browser() {

  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isOP    = false;  // Opera
  this.isNS    = false;  // Netscape
  this.version = null;

  ua = navigator.userAgent;

  s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isOP = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as Netscape 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }

  s = "MSIE";
  if ((i = ua.indexOf(s))) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
}

var browser = new Browser();


function getPageOffsetLeft(el) {

  var x;

  // Return the x coordinate of an element relative to the page.

  x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getPageOffsetLeft(el.offsetParent);

  return x;
}

function getPageOffsetTop(el) {

  var y;

  // Return the y coordinate of an element relative to the page.

  y = el.offsetTop;
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);

  return y;
}
//----------------------------------------------------------------------------
// Code for handling the menu bar and active menu.
//----------------------------------------------------------------------------

var activeMenu = null;

function hideActiveMenu() {

  if (activeMenu == null)
    return;

  activeMenu.style.visibility = "hidden";
  activeMenu = null;
}

function showMenu( el, menuId ){
  var x, y;
  var menu = document.getElementById( menuId );
  // Position the associated drop down menu under the button and
  // show it.

  x = getPageOffsetLeft(el);
  y = getPageOffsetTop(el) + el.offsetHeight;

  // For IE, adjust position.
  if (browser.isIE) {
    x += el.offsetParent.clientLeft;
    y += el.offsetParent.clientTop;
  }
  if( activeMenu != null && activeMenu != menu )
  	activeMenu.style.visibility = "hidden";
  menu.style.left = x + "px";
  menu.style.top  = y + "px";
  menu.style.visibility = "visible";
  activeMenu = menu;
}

//----------------------------------------------------------------------------
// Functions for validation
//----------------------------------------------------------------------------
var whitespace = " \t\n\r";

function isEmpty(s) { 
	return ((s == null) || (s.length == 0)) 
}

// Check if the string is just composed by whitespaces
function isWhitespace (s) {
	var i;
    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
	}
	return true;
}

// Force a entry from user for the field
function ForceEntry(val, str) {
	var strInput = new String(val.value);
	if (isWhitespace(strInput)) {
		alert(str);
		val.focus();
		return false;
	} 
	else
		return true;
}

// Check if is a valid email
function isEmail(val, str){
	var strInput = new String(val.value);
	if ( !/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(strInput) ){
 		alert(str);
		val.focus();
		return false;
	}
	else
		return true
}

// Check if is a word without numbers
function isWord(val, str){
	var strInput = new String(val.value);
	if ( !/^\w+$/.test(strInput) ){
 		alert(str);
		val.focus();
		return false;
	}
	else
		return true
}

// Check if is Entire
function IsEntire(s,str) {
	var ValidChars = "0123456789";
   	var IsEntire=true;
   	var theChar;
	for (i = 0; i < s.value.length && IsEntire == true; i++)  { 
    	theChar = s.value.charAt(i); 
     	if (ValidChars.indexOf(theChar) == -1) {
	       	IsEntire = false;
        }
    }
	if (!IsEntire) {
		alert(str);
		s.focus();
	}
   	return IsEntire;
}

// Validate if at least one checbox from fields is selected 
function isSomeChecked(fields, str){
	var isChecked = false;
	for( i = 0; i < fields.length; i++ ){		
		if( fields[i].checked ){	
			return true;
		}
	}
	fields[0].focus();
	alert(str);
	return isChecked;
}

// Check two fields for coincidence
function areEqual(val1, val2, str){
	var strInput1 = val1.value;
	var strInput2 = val2.value;
	if( strInput1 != strInput2 ){
		alert(str);
		val2.focus();
		return false;
	}
	else
		return true;
}

// Check if a select field was used, only works if select field has
// an option with value="" to indicate no selection
function isSelected(val, str){
	var strInput = new String(val.options[ val.selectedIndex ].value );
	if (isWhitespace(strInput)) {
		alert(str);
		val.focus();
		return false;
	} 
	else
		return true;
}

// Check for a minimun lenght of a field
function minLength(val, minl, str){
	var strInput = new String(val.value);
	if( strInput.length < minl ){
		alert(str);
		val.focus();
		return false;
	}
	else
		return true;
}

// Check for maximun lenght, usefull on textarea fields
function maxLength(val, maxl, str){
	var strInput = new String(val.value);
	if( strInput.length > maxl ){
		alert(str);
		val.focus();
		return false;
	}
	else
		return true;
}

/*Validate customer Login*/
function validateCustomerLogin(form){
	var messageA = "All fields are mandatory";
	var messageB = "Please write a valid email";
	var CanSubmit = false;
    CanSubmit = ForceEntry(form.authenticateCustomerLogin,messageA);
	if (CanSubmit) CanSubmit = isEmail(form.authenticateCustomerLogin,messageB);;	
	if (CanSubmit) CanSubmit = ForceEntry(form.authenticatePassword,messageA);;
	return CanSubmit;
}

/*Validate MailList Form*/
function validateMailListForm(form){
	var messageA = "All fields are mandatory";
	var messageB = "Please write a valid email";
	var CanSubmit = false;
    CanSubmit = ForceEntry(form.email,messageA);
	if (CanSubmit) CanSubmit = isEmail(form.email,messageB);;	
	if (CanSubmit) CanSubmit = ForceEntry(form.name,messageA);;
	return CanSubmit;
}


function modifyHiddenNSubmit( hiddenId, nuVal ){
	hField = document.getElementById( hiddenId );
	hField.value = nuVal;	
}

/*-----------Show and Hide Divs-------------------*/
function showHideDiv( divId, theLink ){
    var theDiv = document.getElementById( divId );
	var theImg = document.getElementById( divId+"img" );
	if( theDiv.style.display != "block" ){
		theDiv.style.display = "block";
		theImg.src = "images/i_hideinfo.gif?10";
	}
	else{
		theDiv.style.display = "none";
		theImg.src = "images/i_moreinfo.gif?10";
	}
	return false;
}