function doLoad() {
  // does nothing
}

function addEvent( obj, type, fn ){  // the add event function
  if (obj.addEventListener) obj.addEventListener( type, fn, false );
  else if (obj.attachEvent) {
    obj["e"+type+fn] = fn;
    obj[type+fn] = function() {
      obj["e"+type+fn]( window.event );
    };
    obj.attachEvent( "on"+type, obj[type+fn] );
  }
}

var firstEl;

function validateForm(frm) {
  errs = "";
  firstEl = null;
  els = frm.elements;
  for(i=0; i<els.length; i++) {
    if(els[i].id == "req")
      errs += checkVal(els[i]);
  }
  if(errs != "") {
    alert("Please make the following changes:\n\n" + errs);
    if(firstEl)
      firstEl.focus();
    return false;
  }
  return true;
}

function checkVal(el) {
  if(el) {
    if(((el.tagName == "INPUT" || el.tagName == "TEXTAREA") && el.value == "")
       || (el.tagName == "SELECT" && el.options[el.selectedIndex].value == "")) {
      if(firstEl == null)
        firstEl = el;
      return "- " + el.title + "\n";
    }
  }
  return "";
}

var tabOver = function(el) {
  cls = el.className;
  if(cls.indexOf('xed') != -1)
    el.className = 'tab xedover';
  else if(cls.indexOf('on') == -1)
    el.className = 'tab over';
}

var tabOut = function(el) {
  cls = el.className;
  if(cls.indexOf('xedover') != -1)
    el.className = 'tab xed';
  else if(cls.indexOf('on') == -1)
    el.className = 'tab';
}
  


/** MOUSE OVER EFFECTS **/
function preloadImgs() {
  imgs = document.images;
  for(i=0; i<imgs.length; i++) {
    src = imgs[i].src;
    if(imgs[i].className != 'no-hover' && src.indexOf('_off') != -1) {
      document.onImg = new Image();
      document.onImg.src = src.replace('_off', '_on');
      
      addEvent(imgs[i], "mouseover", toggleImg);
      addEvent(imgs[i], "mouseout", toggleImg);
    }
  }
  imgs = document.getElementsByTagName("input");
  for(i=0; i<imgs.length; i++) {
    if(imgs[i].type.toLowerCase() == 'image') {
      src = imgs[i].src;
      if(imgs[i].className != 'no-hover' && src.indexOf('_off') != -1) {
        document.onImg = new Image();
        document.onImg.src = src.replace('_off', '_on');
        
        addEvent(imgs[i], "mouseover", toggleImg);
        addEvent(imgs[i], "mouseout", toggleImg);
      }
    }
  }
}

addEvent(window, "load", preloadImgs);

function toggleImg() {
  src = this.src;
  if(src.indexOf('_off') != -1)
    this.src = src.replace('_off', '_on');
  else if(src.indexOf('_on') != -1)
    this.src = src.replace('_on', '_off');
}


function fixIE() {
  if(document.all) {
    theObjects = document.getElementsByTagName("object");
    for (var i = 0; i < theObjects.length; i++) {
      theObjects[i].outerHTML = theObjects[i].outerHTML;
    }
  }
}
//addEvent(window, "load", fixIE);


function goTo(u) {
  window.location = u;
}

function openPopupWindow(url, width, height, scrolling, name) {
  if (!width) width = 350;
  if (!height) height = 350;
  if (!scrolling) scrolling = "no"; 
  if (!name) name = "win"+Math.round(Math.random()*1000); 
  
  features = "width="+width+","
           + "height="+height+","
           + "toolbar=no,"
           + "location=no,"
           + "status=no,"
           + "menubar=no,"
           + "scrollbars="+scrolling+","
           + "top=50,"//+(window.screen.height-height)/2+","
           + "left=50";//+(window.screen.width-width)/2;
  win = window.open(url,name,features);
  if(win)
    win.focus();
  return win;
}

function popup(u) {
  openPopupWindow('http://www.synccreation.com/popup'+u, 600, 600, "yes");
  return false;
}

function viewImg(u) {
  openPopupWindow("/popup.jsp?url="+u);
}

function watchVid(u) {
  openPopupWindow("/video.jsp?url="+u, 680, 416, "no", "vidWin");
}

/**
 * Search Functions
 */
function sFocus(el){
  t = el.title;
  if(el.value == t)
    el.value = '';
}
function sBlur(el) {
  t = el.title;
  if(el.value == '')
    el.value = t;
}
function sSubmit(frm) {
  s = frm.s.value;
  t = frm.s.title;
  if(s == t || s == '') {
    alert('Please enter something to search for');
    return false;
  }
  return true;
}

function trackTrans() {
	// override on thanks page
}

function _clear(el) {
  t = el.title;
  if(el.value == t)
    el.value = '';
}

function _blur(el) {
  t = el.title;
  if(el.value == '')
    el.value = t;
}

function _validate(frm) {
  eml = frm.email;
  at = eml.value.indexOf('@');
  if(eml.value == eml.title || eml.value == '') {
    alert('Please enter your phone number or email address');
    return false;
  } else if(at == -1 || eml.value.indexOf('.', at) <= at) {
    alert('Please enter a valid email address');
    return false;
  }
  return true;
}

/* Cart Function */
function editItem(s) {
  window.location = s;
}

function updateCart(i) {
  //document.forms[i].action.value = 'update';
  document.forms[i].submit();
}

function removeItem(i) {
  document.forms[i].action.value = 'remove';
  document.forms[i].submit();
}

function changeShipping(el) {
  window.location = window.location.pathname+"?shipping="+el.value;
}

function viewOptions() {
  s = document.getElementById("shipping-options").style;
  if(s.display == 'block')
    s.display = 'none';
  else
    s.display = 'block';

}

function setCookie(c_name,value,expiredays){
	var exdate=false;
	if(expiredays!=null && expiredays){	exdate=new Date(); exdate.setDate(exdate.getDate()+expiredays); extime=exdate.toGMTString(); }
	document.cookie=c_name+"="+escape(value)+(exdate ? ";expires="+extime : '')+'; path=/';
}

function getCookie(c_name){
	var cookies=document.cookie.split(';');
	for(i in cookies){
		tmp_cookie=cookies[i].split('=');
		cookie_name=tmp_cookie[0].replace(/^\s+|\s+$/g, '');
		if(cookie_name==c_name){
			cookie_value=tmp_cookie.length?unescape(tmp_cookie[1].replace(/^\s+|\s+$/g,'')):1;
			return cookie_value;
			break;
		}
	}
	return null;
}

/* checkout functions */
var checkout = {
  doSubmit: function() {
    frm = document.forms['dForm'];
    if(validateForm(frm))
      frm.submit();
  },

  validateForm: function(frm) {
    errs = "";
    els = frm.elements;
    if(frm.password_confirm && frm.password_confirm.value != frm.password.value) {
      errs += "- Your passwords didn't match. Please re-enter your password.\n";
    }

    checkShipping = false;
    if(frm.shipto_is_same)
      checkShipping = frm.shipto_is_same.checked;

    for(i=0; i<els.length; i++) {
      if(els[i].className == "req")
        errs += checkVal(els[i]);
      else if(els[i].className == "bill-req" && checkShipping)
        errs += checkVal(els[i]);
    }
    if(errs != "") {
      alert("Please make the following changes:\n\n" + errs);
      return false;
    }
    if(frm.shipto_address1) {
      sTo = frm.shipto_address1.value.toLowerCase();
      if(sTo.indexOf("p.o. box") != -1 ||
         sTo.indexOf("po box") != -1) {
        alert("We're sorry, we cannot ship to P.O. Boxes.\nPlease provide a street address for us to ship to.");
        return false;
      }
    }
    return true;
  },

  autoTab: function(el, next) {
    if(el.getAttribute && el.value.length == el.getAttribute("maxlength"))
      document.getElementById(next).focus()
  }
};
/* hs function */
var viewTitles = function(pid, pgid, pg) {
  $("#app-block-" + pid).load("/ajax/title-list.jsp?pid=" + pid + "&pgid=" + pgid + "&pg=" + pg);
}

$(document).ready(function() {
  $h1 = $("h1.headline");
  if($h1.css('background-image') != '')
    $h1.addClass("h1-img");

  // keep the session alive by making an ajax call every 10 minutes
  setInterval('keepAlive', 600000);

});

function keepAlive() {
  $.ajax("https://www.synccreation.com/secure/refresh.jsp");
}

