/*
 * $Id: utils.js,v 1.11 2008/06/03 13:28:36 savuso Exp $
 *
 * Description: useful javascript constants, variables & functions
 * Author:      Vlad Cochina
 * Start date:  2005/06/10
 *
 */

function trim_string(value) {
    var re = /(^\s+|\s+$)/g;
    return value.replace(re,'');
}

function html_escape(str) {
    str = str.replace(/&/g, '&amp;');
    str = str.replace(/\"/g, '&quot;');
    str = str.replace(/'/g, '&#039;');
    str = str.replace(/</g, '&lt;');
    str = str.replace(/>/g, '&gt;');
    return str;
}
function html_unescape(str) {
    str = str.replace(/&amp;/g, '&');
    str = str.replace(/&quot;/g, '"');
    str = str.replace(/&#039;/g, '\'');
    str = str.replace(/&lt;/g, '<');
    str = str.replace(/&gt;/g, '>');
    return str;
}

function is_valid_email(email) {
    return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/.test(email);
}

function cancel_bubble(evt) {
    var e = (window.event) ? window.event : evt;
    e.cancelBubble = true;
}

/* FUNCTIONS FOR STATIC TOUR */
function bulletShow(og_trigger, text) {
        trigger = document.getElementById(og_trigger);
        target = document.getElementById('bullet_panel');
        center = trigger.offsetParent.offsetWidth/2;
        equator = trigger.offsetParent.offsetHeight/2;
        target.innerHTML = text;
        
        if (parseFloat(trigger.style.left) > center) {
                x = parseFloat(trigger.style.left) - 270;
        } else {
                x = parseFloat(trigger.style.left) + 20;
        }
        y = trigger.offsetTop + 20;

        trigger.style.zIndex = 10;
        target.style.left = x+"px";
        
        
        if (parseFloat(trigger.style.top) > equator) {
                target.style.zIndex = -1;
                target.style.display = "block";
                setTimeout("getHeight('"+og_trigger+"')", 1);
        } else {
                y = parseFloat(trigger.style.top) + 20;
                target.style.top = y+"px";
                target.style.zIndex = 1;
                target.style.display = "block";
        }
}
function getHeight(og_trigger) {
        target = document.getElementById('bullet_panel');
        height = target.offsetHeight;
        y = parseFloat(trigger.style.top) - (height - 20);
        target.style.top = y+"px";
        target.style.zIndex = 1;
}
function bulletHide(trigger) {
        document.getElementById(trigger).style.zIndex = 0;
        document.getElementById('bullet_panel').style.display = "none";
}

function tabs(whichtab) {
	tabarray = new Array('marketplace', 'auctions');
	if (whichtab == 'stores') {
		document.getElementById('invisiTabs').style.backgroundImage = 'none';
	} else {
		for (i=0; i<2; i++) {
			if (whichtab == tabarray[i]) {
				document.getElementById('invisiTabs').style.backgroundImage = 'url(/images/marketing/tour_automated_' + tabarray[i] + '.png)';
			}
		}
	}
}

function panelRotate(type) {
  bg_images = new Array('none', 'url(/images/marketing/tour_' + type + '_marketplace.png)', 'url(/images/marketing/tour_' + type + '_auctions.png)');
  target = document.getElementById('invisiTabs');

  for (i=0; i<3; i++) {
    if (target.style.backgroundImage == bg_images[i]) {
      currentPos = i;
      break;
    }
    currentPos = 0;
  }

  rotator = setInterval(rotateInterval, 2000);
}

function rotateInterval() {
  if (currentPos == 2) {
    nextPanel = 0;
  } else {
    nextPanel = currentPos + 1;
  }

  target.style.backgroundImage = bg_images[nextPanel];
  currentPos = nextPanel;
}

function stopRotate() {
  clearInterval(rotator);
}

function validate_usab() {
    var email = document.getElementById('email_usab').value;
    if (is_valid_email(email)) {
        return true;
    }
    else {
        alert('Please enter a valid email address.');
        return false;
    }
}


function set_cookie(name, value, exp_days, path) {
    var cookie = name + '=' + escape(value);
    if (exp_days) {
        var exp_date = new Date();
        exp_date.setDate(exp_date.getDate() + exp_days);
        cookie += '; expires=' + exp_date.toGMTString();
    }
    if (path && path.length) {
        cookie += '; path=' + path;
    }
    document.cookie = cookie;
}

var hideTimer;
function start_dialog_timer(func, dur) {
    hideTimer = setTimeout(func, dur);
}

function reset_dialog_timer() {
    hideTimer = clearTimeout(hideTimer);
}

function isOverflown(o) {
    if(o.clientHeight<o.scrollHeight) {
        return true;
    } else {
        return false;
    }
}
