// This file is automatically included by javascript_include_tag :defaults
// ----------------------------------------------
// StyleSwitcher functions written by Paul Sowden
// www.idontsmoke.co.uk/ss/
// - - - - - - - - - - - - - - - - - - - - - - -/
// For the details, visit ALA:
// www.alistapart.com/stories/alternate/
// ----------------------------------------------

function setActiveStyleSheet(title, reset) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  if (reset == 1) {
  createCookie("wstyle", title, 365);
  }
}

function setStyle() {
var style = readCookie("wstyle");
if (style != null) {
setActiveStyleSheet(style, 0);
}
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+escape(value)+expires+"; path=/;";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return unescape(c.substring(nameEQ.length,c.length));
  }
  return null;
}

//show hide checkbox on customer info form
  function showhidefield()
  {
    if (document.frmOINF.chk_billingcolumn.checked)
    {
      document.getElementById("billingcolumn").style.display = "block";
    }
    else
    {
      document.getElementById("billingcolumn").style.display = "none";
    }
  }
  
 

function IsChecked(theform) {
   return theform.agreeservice.checked;
 }

// This function build a url for the affiliate id from the url entered in the form 
function buildurl(){
   document.form01.result.value = document.form01.url.value + '?kbid=' + document.form01.kbid.value;
   document.form01.result.style.visibility = 'visible';
}

// This two functions set and read cookies... 
function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}

function ReadCookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return ""; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

// This function generate a popup window from the url 
function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=300,height=300,left = 490,top = 362');");
}

//from referdetect.js
// 2005-9-17 gaba: javascript for myaffiliateprogram updated by Gaba 3/1/2007 

	var scriptLoc = 'http<%= request.ssl? ? "s" : nil %>://www.babelandaffiliates.com/rep.asp';
	
	var kbId = 0;
	var queryString = window.location.search.substring(1);
	var imgdata = '';
	var subdata = '';
	var overwrite = 0;

	if (queryString.length > 0)
	{
		var pairs = queryString.split('&');
								
		for(var i = 0; i < pairs.length; i++)
		{
			var pairs2 = pairs[i].split('=');
			
			switch(pairs2[0].toLowerCase())
			{
				case 'kbid':
					kbId = pairs2[1];
					break;
				case 'clearcookie':
					if (pairs2[1] == 1)
					{
						DeleteCookie("kbid");
					}
					break;
				case 'img':
					imgdata = pairs2[1];
					break;
				case 'sub':
					subdata = pairs2[1];
					break;
			}
		} //end for
	}//end if

	if (GetCookie("kbid") == null || overwrite == 1)
	{
		if (kbId != 0)
		{
			var img = new Image();
			var url = scriptLoc + '?id=' + kbId;
			if (imgdata != '')
			{
				url += '&img=' + imgdata
			}

			if (subdata != '')
			{
				url += '&sub=' + subdata
			}

			img.src = url;
			
			SetCookie("kbid", kbId, 1);
		}
	}//end if
	
	function getCookieVal (offset) 
	{
		var endstr = document.cookie.indexOf (";", offset);
		if (endstr == -1)
			endstr = document.cookie.length;

		return unescape(document.cookie.substring(offset, endstr));
	}

	function GetCookie (name) 
	{
		var arg = name + "=";
		var alen = arg.length;
		var clen = document.cookie.length;
		var i = 0;
		
		while (i < clen) 
		{
			var j = i + alen;
			if (document.cookie.substring(i, j) == arg)
				return getCookieVal (j);
			
			i = document.cookie.indexOf(" ", i) + 1;

			if (i == 0) break; 
		}
		
		return null;
	}

	function WriteCookie (name, value, expires) 
	{
		var argv = SetCookie.arguments;
		var argc = SetCookie.arguments.length;
		var path = (argc > 3) ? argv[3] : null;
		var domain = (argc > 4) ? argv[4] : null;
		var secure = (argc > 5) ? argv[5] : false;
		
		var cookie = name + "=" + escape(value) +
			((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
			((path == null) ? "" : ("; path=" + path)) +
			((domain == null) ? "" : ("; domain=" + domain)) +
			((secure == true) ? "; secure" : "");
		
		document.cookie = cookie;
	}

	function DeleteCookie (name) 
	{
		var exp = new Date();
		exp.setTime (exp.getTime() - 1000000000);  // This cookie is history (changed -1 to make it previous time)
		var cval = GetCookie (name);
		document.cookie =name + "=" + cval + "; expires=" + exp.toGMTString();
	}

	function SetCookie(name, value, expiredays)
	{
		var expdate = new Date(); 
		expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * expiredays)); 
		WriteCookie(name, value, expdate);  
	}

//image switcher for product detail view

function prepImageswitcher() {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("prod_special_icons")) return false;
	var imageswitcher = document.getElementById("prod_special_icons");
	var links = imageswitcher.getElementsByTagName("a");
	for ( var i=0; i< links.length; i++) {
		links[i].onclick = function() {
			return showAltimage(this);
			
		}
	}
}

function showAltimage(whichpic) {
	if (!document.getElementById("placeholder")) return true;
	
	var source = whichpic.getAttribute("href");
	var placeholder = document.getElementById("placeholder");
	placeholder.setAttribute("src",source);
	return false;
}

/*search form*/
function submitSearch() {
	if (document.forms["portlet-babeland-search"].searchlocation[1].checked) {
		window.location = "/search?q="+escape(document.forms["portlet-babeland-search"].q.value);
		return false;
	}
}

// funtion to display a popup window (generally used for promotions)

function promoPopup(mylink, windowname){
	if (! window.focus)return true;
	  var href;
	if (typeof(mylink) == 'string')
	  href=mylink;
	else
	  href=mylink.href;
	  window.open(href, windowname, 'width=615,height=415,scrollbars=no,location=0');
	return false;
}

// ref: simonwillison.net/2004/May/26/addLoadEvent/
// functions that need to be loaded on window.onload can be added here

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(prepImageswitcher);