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 'upload' :
		  frameHeight = 225;
			frameSrc = 'upload?side=' + document.form.side.value + '&key=' + document.form.A_key.value;
			titleTxt = 'File Upload';
			break;
		case 'calendar' :
      popSize = 450;
      frameHeight = 445;
      try {
        production_flag = document.form.production_flag.value;        
      }
      catch(e) {
        production_flag = 0;
			}
      id = document.form.I_item_number.value;
			production = '?id=' + id + '&prdtn=' + production_flag;
      frameSrc = 'calendar' + production;
      titleTxt = 'Production Schedule';
      scrolling = 'no';
			break;
		case 'example' :
			popSize = 900;
			frameHeight = 453;
			frameSrc = 'static/example.html';
			titleTxt = 'Artwork Example';
			break;
		case 'ccv' :
			popSize = 600;
			frameHeight = 453;
			frameSrc = 'static/ccv.html';
			titleTxt = 'Card Verification Number Details';
			break;
		case 'fonts' :
			popSize = 800;
			frameHeight = 540;
			frameSrc = 'fonts/';
			titleTxt = 'Fonts';
			break;
		case 'clipart' :
			popSize = 943;
			frameHeight = 493;
			frameSrc = 'clipart/';
			titleTxt = 'Clipart';
			break;
		case 'caps' :
			popSize = 700;
			frameHeight = 453;
			titleTxt = 'Caps';
		  break;
		case 'modify' :
			popSize = 700;
			frameHeight = 500;
			frameSrc = 'modify.html?key=' + document.form.key.value;
			titleTxt = 'Modify Item';
			break;
		case 'reorder' :
			popSize = 350;
			frameHeight = 153;
			frameSrc = 'reorder.html';
			titleTxt = 'Reorder Item';
			break;
		case 'sales_receipt' :
			popSize = 950;
			frameHeight = 500;
			frameSrc = '?p=slsrt&zip=' + document.form.I_zip_code.value + '&tax=' + document.form.tax.value;
			titleTxt = 'Sales Receipt';
			break;
		case 'reset' :
			frameHeight = 1;
			frameSrc = '';
		  break;
	  case 'details' :
		  popSize = 750;
      frameHeight = 485;
      frameSrc = '?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="img/progress/flasher[wait].gif" alt="Loading Page..." />';
      break;
    default :
      innerHtml = '<img id="progress" src="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 = '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 = '?p=';
			break;
		case 'contact' :
			window.location = '?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;
}
function browserType() {
  browser = navigator.userAgent;
  type = 'Unknown';
  if(browser.match(/Apple|Safari/)) {
    return 'Safari';
  }
  if(browser.match(/Firefox/)) {
    return 'Firefox';
  }
  if(browser.match(/MSIE/)) {
    return 'Internet Explorer';
  }
  return type;
}