var d;
var df;
var sideBarContent;
var navs = new Array('navProducts','navArtwork','navMyCart','navContact');
var navTabs = [];
var navArtwork = [];
navArtwork[0] = ('requirements,Requirements');
navArtwork[1] = ('clipart,Clipart');
navArtwork[2] = ('fonts,Fonts');
var navSpecials = [];
var navMyCart = [];
navMyCart[0] = ('mycart,Shopping Cart');
var navContact = [];
navContact[0] = ('contact,Contact');
navContact[1] = ('club,Savers Club');
navContact[2] = ('faq,FAQ');
var navLnksAr = [];
var interval;
var okayToClose = true;
// cats array...
var navProducts = [];
navProducts[0] = ('ceramic-coffee-mugs,Ceramic Mugs');
navProducts[1] = ('custom-door-hangers,Door Hangers');
navProducts[2] = ('promotional-frisbees-flying-discs,Flying Discs');
navProducts[3] = ('custom-foam-hands,Foam Hands');
navProducts[4] = ('custom-ice-scrapers,Ice Scrapers');
navProducts[5] = ('key-chains-tags,Key Chains');
navProducts[6] = ('custom-koozies,Koozies');
navProducts[7] = ('promotional-letter-openers,Letter Openers');
navProducts[8] = ('custom-magnets,Magnets');
navProducts[9] = ('promotional-pens,Pens');
navProducts[10] = ('custom-rubber-ducks,Rubber Ducks');
navProducts[11] = ('sports-water-bottles,Sports Bottles');
navProducts[12] = ('plastic-stadium-cups,Stadium Cups');
navProducts[13] = ('note-pads-sticky-notes,Sticky Notes');
navProducts[14] = ('stress-balls-relievers,Stress Balls');
navProducts[15] = ('custom-travel-mugs,Travel Mugs');
navProducts[16] = ('promotional-wall-calendars,Wall Calendars');
var menuShowing;
// for qMarks...
var iframe;
var scrollHeight;
// keep the iframe open...
var holdTight = false;
var timer;
var db_selected = 'main';
//////////////////////////////////////////////////////////
function $(e) {
  return document.getElementById(e);
}
function hidePopup() {
	$('popupFrame').src = '';
	$('popup').style.display = 'none';
	document.getElementById('bodyContainer').className = '';
}
function popUp(which) {
  var titleTxt = '';
  var popSize = 405;
  var scrolling = 'no';
	var frameHeight = 0;
	var queryString = '';
	var frameSrc = '';
	var pop = $('popup');
	var popFrame = $('popupFrame');
	var showLoading = false;
	var id = 0;
	var scrollAdd = 0;
	var production_value;
	var notice = 'circle';
	if(which.match(/,/)) {
	  var parts = which.split(/,/);
	  which = parts[0];
	  var details = parts[1];
	}
	switch(which) {
		case 'fonts' :
			popSize = 800;
			frameHeight = 540;
			frameSrc = 'http://www.discountfavors.com/fonts/';
			titleTxt = 'Fonts';
			break;
		case 'clipart' :
			popSize = 943;
			frameHeight = 493;
			frameSrc = 'http://www.discountfavors.com/clipart/';
			titleTxt = 'Clipart';
			break;
		case 'reset' :
			frameHeight = 1;
			frameSrc = '';
		  break;
	  case 'details' :
		  popSize = 750;
      frameHeight = 485;
      frameSrc = 'http://www.discountfavors.com/?p=sdtls';
      titleTxt = 'Sale Details';
      scrolling = 'no';
	    break;
	}
	if(frameSrc == '') return false;
	if(showLoading) {
		loading(notice, 1, popSize, frameHeight);
	}
	else {
		loading('', 0, 0, 0);
	}
	if(document.documentElement) {
		scrollAdd = document.documentElement.scrollTop;
	}
	$('bodyContainer').className = 'overlay';
	popFrame.src = frameSrc + queryString;
	popFrame.scrolling = scrolling;
	document.body.clientHeight ? win = innerWidth('ie') : win = innerWidth('');
	pop.style.left = win['w']/2 - popSize/2 + 'px';
	pop.style.top = scrollAdd + win['h']/2 - frameHeight/2 + 'px';
	pop.style.width = popSize + 'px';
	popFrame.style.height = frameHeight + 'px';
	popFrame.style.width = popSize + 'px';
	$('titleText').innerHTML = titleTxt;
	pop.style.display = 'block';
}
function innerWidth(which) {
  var dim = new Array();
  switch(which) {
	  case 'ie' :
			dim['w'] = document.documentElement.clientWidth;
			dim['h'] = document.documentElement.clientHeight;
		  break;
		default :
			dim['w'] = window.innerWidth;
			dim['h'] = window.innerHeight;
		  break;
	}
	return dim;
}
function loading(notice, toggle, width, height) {
  var loading = $('loading');
  var innerHtml = '';
  if(!toggle) {
	  loading.style.visibility = 'hidden';
	  return false;
  }
  switch(notice) {
    case 'pleaseWait' :
      innerHtml = '<img id="progress" src="http://www.discountfavors.com/img/progress/flasher[wait].gif" alt="Loading Page..." />';
      break;
    default :
      innerHtml = '<img id="progress" src="http://www.discountfavors.com/img/progress/progress.gif" alt="Loading Page..." /><br />Loading...';
      break;
  }
  loading.innerHTML = innerHtml;
  loading.style.left = width/2-50 + 'px';
  loading.style.top = height/2 + 'px';
  loading.style.visibility = 'visible';
}
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,'');
}
function clearField(which) {
	switch(which) {
			case 'e-news' :
			var val = document.form.EmailAddress;
			if(val.value == 'email@domain.com') val.value = '';
			break;
		case 'search' :
			var val = document.form.search_txt;
			if(val.value == 'Search for Giveaways') val.value = '';
			val.style.color = '#000000';
			val.style.textAlign='left';
			val.style.fontWeight='bold';
			break;
	}
}
function submitForm(which) {
	submit = false;
  switch(which) {
		case 'search' :
			if(submit = validate('alphaNumeric', which)) {
				document.form.action = 'http://www.discountfavors.com/search.html';
			}
			break;
		case 'newsletter' :
			if(submit = validate('email', which)) {
			  document.form['flag'].value = which;
			}
			break;
		}
		if(submit) {
		  document.form.submit();
		}
}
function validate(type, field) {
  switch(type) {
		case 'email' :
			var email = trim(document.form[field].value);
			var pattern = /^[a-zA-Z0-9\-\._]+@[a-zA-Z0-9\-_]+(\.?[a-zA-Z0-9\-_]*)\.[a-zA-Z]{2,3}$/;
			if(!pattern.test(email)) {
				alert('The email address provided is invalid!    ');
				return false;
			}
			return true;
		case 'alphaNumeric' :
			var search = trim(document.form[field].value);
			if(search.match(/^search/i)) return false;
			if(search == '') {
			  alert('You must enter a search parameter!    ');
			  return false;
			}
			var pattern = /^[a-zA-Z0-9\-\._\s]+$/;
			if(!pattern.test(search)) {
				alert('The parameter provided is invalid!    ');
				df['search'].value = '';
				return false;
			}
			return true;
  }
}
function redirect(which) {
	switch(which) {
		case 'home' :
			window.location = 'http://www.discountfavors.com/?p=';
			break;
		case 'contact' :
			window.location = 'http://www.discountfavors.com/?p=cntct';
			break;
	}
}
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = '0';
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	cents = num % 100;
	num = Math.floor(num/100).toString();
	if(cents < 10) cents = '0' + cents;
	for (var i = 0; i < Math.floor((num.length - (1+i))/3); i++) {	
		num = num.substring(0,num.length-(4 * i + 3))+','+ num.substring(num.length - (4 * i + 3));
	}
	return (((sign) ? '' : '-') + num + '.' + cents);
}
function findPos(obj) {
	var curleft = curtop = 0;
	if(obj.offsetParent) {
		do{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} 
		while (obj = obj.offsetParent);
	}
	if(curleft + curtop > 0) return [curleft,curtop];
	return false;
}
/* util */
function mathRound(value) {
  // 10000 is percision...
  return Math.round((value)*10000)/10000;
}
//////////////////////////////////////////////////////////
function showSideBar(toggle) {
	if(!toggle) {
		hideSideBar();
		return;
	}
  try {
  	$('sideBar').style.display = 'table-cell';
  }
  catch(e) {
    // ie method...
  	$('sideBar').style.display = 'block';
  }
  if(sideBarContent == 'categories') {
    $('actionIs').style.height = 650 + 'px';
  }
}
function hideSideBar() {
  $('sideBar').style.display = 'none';
  $('actionIs').style.height = 100 + '%';
}
function setNavHandlers() {
  $('menu').onclick = function() {
    toggleMenu('menuHide');
  }
  $('menu').onmouseover = function() {
	  okayToClose = false;
	}
  $('menu').onmouseout = function(e) {
    var e = e || window.event;
	  if(checkMouseLeave(this, e)) {
	  	okayToClose = true;
			toggleMenu('menuHide');
			setTabs(0);
		}
	}
  for(t in navTabs) {
    // make sure the img has an id...
    if(!navTabs[t].id) continue;
    navTabs[t].onmouseover = function() {
      if(populateMenu(this.id)) {
        switch(this.id) {
          case 'navArtwork' :
            $('menu').style.left = 173 + 'px';
            $('menuPanel').style.width = 160 + 'px';
            break;
          case 'navMyCart' :
            $('menu').style.left = 654 + 'px';
            $('menuPanel').style.width = 160 + 'px';
            break;
          case 'navContact' :
            $('menu').style.left = 826 + 'px';
            $('menuPanel').style.width = 160 + 'px';
            break;
          // products...
          default :
            $('menu').style.left = 0 + 'px';
            $('menuPanel').style.width = 160 + 'px';
            break;
        }
	      toggleMenu('menuShow');
				setTabs(this);
      }
			this.src = this.src.replace(/_off/,'_on');      
    }
		navTabs[t].onmouseout = function(e) {
			this.src = this.src.replace(/_on/,'_off');			
			interval = setInterval('toggleMenu(\'menuHide\')', 2500);
		}
  }
}
function setTabs(tab) {
  for(t = 0;t < navTabs.length;t++) {
    var ts = navTabs[t].src;
    if(navTabs[t].id != tab.id) {
		  navTabs[t].src = ts.replace(/On/,'Off');
		}
  }
}
function toggleMenu(className) {
	if(okayToClose) $('menu').className = className;  
	interval = clearInterval(interval);
}
function populateMenu(id) {
  try {
  	navLnksAr = eval(id);
  }
  catch(e) {
    return false;
  } 
  len = navLnksAr.length;
  if(len > 0) {
	  $('menuPanel').innerHTML = '';
	  var ul = document.createElement('ul');
	  ul.className = 'ulLnkGrp';
	  cntr = column = 0;
	  for(n in navLnksAr) {
	    var parts = navLnksAr[n].split(',');
	    var li = document.createElement('li');
	    li.id = parts[0];
	    li.innerHTML = parts[1];
	    li.className = 'tLinks';
	    li.onclick = function() {
	      switch(this.id) {
	        case 'art__' :
	        case 'cart_' :
	        case 'conta' :
	        case 'club_' :
	        case 'faq__' :
	        	navGoTo('page', this.id);
	          break;
	        case 'clipart' :
	        case 'fonts' :
	          popUp(this.id);
	          break;
	        default :
	          navGoTo('id', this.id);
	          break;
	      }
	    }
			ul.appendChild(li);
			column++; 
	    cntr++; 
	    if(column == 12 || (column < 12 && cntr == len)) {  		
				$('menuPanel').appendChild(ul); 
	  		ul = document.createElement('ul');
	  		ul.className = 'ulLnkGrp';
	  		column = 0;
	    } 
	  }
	  return $('menuPanel').innerHTML != '' ? 1 : 0;
  }
  return false
}
function checkMouseLeave (element, evt) {
  if (element.contains && evt.toElement) {
    return !element.contains(evt.toElement);
  }
  else if (evt.relatedTarget) {
    return !containsDOM(element, evt.relatedTarget);
  }
}
function containsDOM (container, containee) {
  var isParent = false;
  do {
    if ((isParent = container == containee)) break;
    containee = containee.parentNode;
  }
  while (containee != null);
  return isParent;
}
function navGoTo(which, data) {
  window.location = 'http://www.discountfavors.com/' + data.replace(/ /,'-').toLowerCase() + '.html';
}
/*
function navGoTo(which, data) {
  var url = '';
  switch(which) {
    case 'id' :
      url = 'items&c=';
      break;
  }
  window.location = 'http://www.discountfavors.com/?p=' + url + data;
}
*/
function showBanner(toggle) {
	if(!toggle) {
		$('banner').style.display = 'none';
		return;
	}
	$('banner').style.display = 'block';
}
function init() {
	showBanner(true);
}
window.onload = function(e) {
  // globals...
  d = document;
  df = document.form;
	try {
		//df['search'].focus();
	}
	catch(e) {}
	try {
		df.I_quantity.focus();
	}
	catch(e) {}
	try {
	  // if no hidden flag input, create one...
	  df['flag'].type;
	} 
	catch(e) {
	  var _input = d.createElement('input');
	  _input.name = 'flag';
	  _input.type = 'hidden';
	  df.appendChild(_input);
	}
	navTabs = $('navBtGrp').getElementsByTagName('img');
	setNavHandlers();
	// for search enter...
	d.onkeydown = function(e) {
	  if(!e) e = event;
	  if(e.keyCode == 13) {
			submitForm('search');
			// kill single input submit...
			return false;
		}
	}
	// last...
	try {
	  // a.k.a "onload event" for templates...
	  // --> only one per page cycle!
		init();
	}
	catch(e) {
	  //alert('no init found!');
	  // no init...
	}
}