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;	
}

function isEntireValue(s,str) {
	var val=s.value;
	var ValidChars = "0123456789";
    var IsEntire=true;
   	var theChar;
	if(val != ""){
	   for (i = 0; i < val.length && IsEntire == true; i++)  { 
    	   theChar = val.substring(i, i+1); 
     	   if (ValidChars.indexOf(theChar) == -1) IsEntire = false;
       }
	}
	if (!IsEntire) {
		  alert(str);
		  s.focus();
	}
	return IsEntire;
}

function maxbidprice(id){
	var productId=id;
	if( window.XMLHttpRequest )
		ajaxbidprice = new XMLHttpRequest();
	else
		ajaxbidprice = new ActiveXObject("Microsoft.XMLHTTP");
	ajaxbidprice.onreadystatechange = funcionCallbackbidprice;
	ajaxbidprice.open( "GET", "../bidpriceAjax.php?productId="+productId+"&cache="+Math.random(), true );
	ajaxbidprice.send("");
}

function funcionCallbackbidprice(){
	if( ajaxbidprice.readyState == 4 ){
		if( ajaxbidprice.status == 200 ){	
			ajaxRows_1 = ajaxbidprice.responseText;
			ajaxRows_1 = ajaxRows_1.split("/"); 
			ajaxRows = ajaxRows_1[0];
			var messageA = "Your bid is less than current bid";
			var messageD = "Your bid must be different to current bid";
			var messageC = "Your bid is not valid. Please, don't use commas or points.";
			var messageB = 'Are you sure you want to bid?';			
			var bidprice = document.getElementById("bidprice_"+ajaxRows_1[1]).value;
			var newnumber = Math.round(bidprice*Math.pow(10,2))/Math.pow(10,2);
		  if (ajaxRows>newnumber )
			{
				alert (messageA);
			}
		else if(ajaxRows==newnumber){
				alert (messageD);
			}			
		else if(isNaN(newnumber)==true){
				alert (messageC);
			
			}
			else{
				var cansubmit=confirm (messageB);
				if(cansubmit)
					document.getElementById('fred_'+ajaxRows_1[1]).submit(); 
			}
		 	
		} 
	}
}
/**********************/
hidden = true;
function showhide(divid){

	if (hidden){
		document.getElementById(divid).style.zIndex = "50";
		hidden = false;
	}else{
		document.getElementById(divid).style.zIndex = "-1";
		hidden = true;
	}
}

function runSWF(){
	document.write('<div id="player"><embed style="width:435px; visibility:visible; height:345px;" allowScriptAccess="never" src="http://www.myplaylist.org/mc/mp3player-othersite.swf?config=http://www.myplaylist.org/mc/config/config_black_noautostart_shuffle.xml&mywidth=435&myheight=345&playlist_url=http://www.myplaylist.org/loadplaylist.php?playlist=43092434" menu="false" quality="high" width="435" height="345" name="mp3player" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" border="0"/></div>');
}
/*
gray
<embed style="width:435px; visibility:visible; height:348px;" allowScriptAccess="never" src="http://www.musicplaylist.us/mc/mp3player-othersite.swf?config=http://www.musicplaylist.us/mc/config/config_regular.xml&mywidth=435&myheight=348&playlist_url=http://www.musicplaylist.us/loadplaylist.php?playlist=43092434" menu="false" quality="high" width="435" height="348" name="mp3player" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" border="0"/>

*/

function showHelpDiv(divId, el){
  var x, y;
  var theDiv = document.getElementById( divId );
  var theFrm = document.getElementById( 'upAllFrm' );
  // 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( theDiv.style.display != "block" )
  	theDiv.style.display = "block";
  theDiv.style.left = x + "px";
  theDiv.style.top  = y + "px";
  if (browser.isIE && theFrm) {
	theFrm.style.height = theDiv.offsetHeight
	theFrm.style.width = theDiv.offsetWidth
  	theFrm.style.display = "block";
	theFrm.style.left = x + "px";
	theFrm.style.top  = y + "px";	
  }
}

function hideDiv(divId){
	TheDiv = document.getElementById(divId);
	var theFrm = document.getElementById( 'upAllFrm' );
	TheDiv.style.display='none';
	if (browser.isIE && theFrm) {
		theFrm.style.display = "none";
	}
}
