function setupSectionTabs(){
$('.tabs-header li:not(.ineligible)').click(function(e){
		e.preventDefault();
		$this = $(this);
		
		if ( !$this.attr('follow') || $this.attr('follow') == 'false') {
			e.preventDefault();
			$('.tabs-header li').removeClass('current').addClass('faded');
			$this.removeClass('faded').addClass('current');
			
			$('.tab').removeClass('current').addClass('faded');
			$('#content-'+$this.attr('id')).removeClass('faded').addClass('current');
		}
	});
}

function setupQuickAddGallery(){
	$('.quick-event-add').unbind().click(function(e){
		e.preventDefault();
		createGallery();
	});
}

function setupPageDams(){
	$('.dam').click(function(e){
		e.preventDefault();
		$selector = $(this);
		$content = $('#content-'+$selector.attr('id'));
		if ($selector.hasClass('active')) {
			$content.hide();
			$selector.removeClass('active')
		}else {
			$content.show();
			$selector.addClass('active');
		}
	});
}

function setupPageFilters(){
	$('#filter .action').click(function(e){
		e.preventDefault();
		handleMenu($(this).get(0),'filter-menu','active','inactive');
	});
}

function setupPageHelp(){
	$('.dynHelp').click(function(e){
		e.preventDefault();
		var $this = $(this);
		var $this = $(this);
		var helpData = $this.attr('title').split('::');
		if (helpData.length < 2) return;
		createHelpElm($this, helpData[0], helpData[1]);
	});
}

function formatDateParameter(datestring){
	return unicodeEscape(datestring);
};

function dateJSONToParam(obj){
	var s = formatDateParameter(obj[0]);
	var e = formatDateParameter(obj[1]);
	return {sd: s, ed: e};
};

function eventJSONToParam(obj){
	return {eid: obj};
};

function getGalleryFacade(dateparams, container){
	var ajaxLoading = document.createElement('div');
	ajaxLoading.id = "ajax-loader";
	var pd = dateparams;
	$(container).empty().append(ajaxLoading);
	GalleryWS.getGalleryByStartDate({sd: pd.sd , ed: pd.ed}, function(data){
		updateElement(container, data);
	});
}

function createGallery(){
	// /var body = document.getElementsByTagName('body')[0];
	// /var frame = document.getElementById('col-frame');
	var body = $('body');
	var frame = $('#site-container');
	var bodydims = {size: {x: body.width(), y: body.height()}};
	var framedims = {size: {x: frame.width(), y: frame.height()}};
	var h = (framedims.size.y>bodydims.size.y)?framedims.size.y+150:bodydims.size.y;
	var w = (framedims.size.x>bodydims.size.x)?framedims.size.x:bodydims.size.x;
	
	if (navigator.appVersion.match(/\bMSIE\b/)){
		var shim = document.createElement('iframe');
		shim.id = 'dimmer-shim';
		shim.className = 'dimmer';
		shim.setAttribute('src','');
		shim.setAttribute('frameBorder','0');
		shim.setAttribute('scrolling','no');
		shim.style.height = h + 'px';
		shim.style.width = w + 'px';
		shim.style.zIndex = '1';
		shim.style.position = 'absolute';
		shim.style.top = '0px';
		shim.style.left = '0px';
		body.append(shim);
	}
	
	var dimmer = document.createElement('div');
	dimmer.id = 'dimmer';
	dimmer.className = 'dimmer';	
	dimmer.style.zIndex = '2';
	dimmer.style.height = '100%';
	dimmer.style.width = '100%';	
	body.append(dimmer);
	
	var pos = frame.offset();
	var container = document.createElement('div');
	container.id = 'event-quick';
	container.className = 'popout';
	container.style.zIndex = '3';
	container.style.top = (pos.top+50) +'px';
	container.style.left = (pos.left+200) +'px';
	container.innerHTML = '<div><h3>Add a Gallery</h3><form name="frmEvent" method="post" action="/galleries/eventadd.asp"><p>Name <br /><input id="event-name" type="text" name="txtName"></p><p>Date (mm/dd/yyyy)<br /><input id="event-date" type="text" name="txtDate"></p><p>Password (optional)<br /> <input id="event-password" name="txtPassword" type="text"></p><p><input id="event-inactive" type="radio" name="ddmUpcoming" value="1" checked="true">I\'d like to create this gallery but not turn the gallery "Live".</p><p><input type="hidden" name="ddmActive" value="1"><input id="gallery-active" type="radio" name="ddmUpcoming" value="0">I\'d like to create this gallery and upload images to be shown online immediately (the gallery status will be set to "Live" and a default group of products and prices will be added).</p><p><input id="create-event" type="button" name="cmdAdd" value="Create Gallery" onclick="validateGallery()"> <input type="button" id="cancel" value="Cancel" onclick="cancelGalleryQuick()"><input id="create-event" type="hidden" name="cmdAdd" value="Create Gallery"></form></div>';
	body.append(container);	
}

function validateGallery()
{
	var dt = $('#event-date').val();
	var name = $('#event-name').val();
	var pass = $('#event-password').val();
	errMsg = '';
	
	//validate the event name
	if(name.length < 1){
		errMsg = 'Please enter a valid name for this gallery\n'
	}
	
	//validate the date
	var OK = validateDate($('#event-date').get(0));
	if(OK !='true'){
		errMsg += OK
	}
	
	//check if any errors
	if(errMsg.length == 0){
		document.frmEvent.submit();
	}else{
		alert(errMsg)
	}
}

function validateDate(fld) {
    var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
    var errorMessage = 'Please enter valid date as month, day, and four digit year.\nYou may use a slash, hyphen or period to separate the values.\nThe date must be a real date. 2-30-2000 would not be accepted.\nFormat mm/dd/yyyy.';
    if ((fld.value.match(RegExPattern)) && (fld.value!='')) {
        //alert('Date is OK'); 
        return 'true';
    } else {
        //alert(errorMessage);
        fld.focus();
        return errorMessage
    } 
}

function cancelGalleryQuick(elm)
{
	$('#event-quick,#dimmer,#dimmer-shim,#dim').remove();	
}

function updateElement(container,data){
	$(container).empty().html(data);
};

function unicodeEscape (pstrString) {
	if (pstrString == "") { return "";}
	var iPos = 0;
	var strOut = "";
	var strChar;
	var strString = escape(pstrString);
	while (iPos < strString.length) {
		strChar = strString.substr(iPos, 1);
		if (strChar == "%") {
			strNextChar = strString.substr(iPos + 1, 1);
			if (strNextChar == "u") {
				strOut += strString.substr(iPos, 6);
				iPos += 6;
			}else {
				strOut += "%u00" +
				strString.substr(iPos + 1, 2);
				iPos += 3;
			}
		}else {
			strOut += strChar;
			iPos++;
		}
	}
	return strOut;
}

function handleDam(damelm, overlimitelm, anchoronstate, anchoroffstate) {
	handleMenu(damelm, overlimitelm, anchoronstate, anchoroffstate);
}

function handleMenu(anchorelm, menuelm, anchoronstate, anchoroffstate) {
	arMenus = menuelm.split(',');
	for(i=0;i<=arMenus.length-1;i++){
		if($(arMenus[i])) {
			var m = ( arMenus[i].indexOf('#') >=0 ) ? $(arMenus[i]) : $('#'+arMenus[i]);
			toggleElmDisplay(m);
		}
	}	
	toggleClass(anchorelm, anchoronstate, anchoroffstate);
}

function toggleClass(elm, onstate, offstate, forcestate){
	elm = $(elm);
	if (forcestate) {
		return elm.removeClass(onstate).removeClass(offstate).addClass(forcestate);
	}
	if (elm.hasClass(onstate)) {
		elm.removeClass(onstate).addClass(offstate);
	}else{
		elm.removeClass(offstate).addClass(onstate);
	}
	return elm;
}

function toggleElmDisplay(elm){	
	elm = $(elm);
	if (elm.css('display') == 'block' || elm.css('display') == '') {
		elm.hide();
	}else{
		elm.show();
	}
}

// HELP FUNCTIONS
function getelementposition(which,what)
{
      var temp = document.getElementById(which);
      coords = new Array();
      var x = 0;
      var y = 0;
      var yoff = temp.offsetHeight;
      var xoff = temp.offsetWidth;
      if(temp && typeof temp.offsetParent != 'undefined')
      {
        while(temp && typeof temp.offsetLeft == 'number')
      {
        x += temp.offsetLeft;
        temp = temp.offsetParent;
      }
      var temp = document.getElementById(which);
      while(temp && typeof temp.offsetTop == 'number')
      {
      y += temp.offsetTop;
      temp = temp.offsetParent;
      }
      }
      coords.xoff = xoff;
      coords.yoff = yoff;
      coords.x = x;
      coords.y = y;
      //alert('coords.xoff: ' + coords.xoff + ' - coords.xoff: ' +coords.yoff+ ' - coords.x: '+coords.x+' - coords.y: '+coords.y+' ')
      return coords;
}

function createPopElm(refElm,title,text,offsettop,offsetleft)
{
  //-------------------------
  //in page implementation
  //-------------------------
  //get the coordinates of the reference Element;
  var coords = getelementposition(refElm.id);
  var newDiv = document.createElement('DIV'); 
  newDiv.id = 'div'+refElm.id;
  newDiv.style.position = 'absolute';
  newDiv.style.top = coords.y + offsettop;
  newDiv.style.left = coords.x + offsetleft;
  if(document.height)
    if( (newDiv.style.top.replace('px','') - document.height) < 100 ) newDiv.style.top = coords.y - 100;
    
  newDiv.borderWidth = '5px';
  newDiv.borderColor = '#000000';
  newDiv.style.padding = '5px';
  newDiv.className = 'popelm';
  newDiv.innerHTML = text;
  newDiv.style.display = 'block';
  var docroot = document.getElementsByTagName('BODY').item(0);
  docroot.appendChild(newDiv);  
}

function removePopElm(refElm)
{
  var helpElm = document.getElementById('div'+refElm.id);
  var docroot = document.getElementsByTagName('BODY').item(0);
  docroot.removeChild(helpElm);
}


//------------------------------------------------------------
//help window functions
//------------------------------------------------------------

function createHelpElmPopUp(refElm, title, text)
{
  popup = window.open('http://admin.backprint.com/help.asp?t='+title+'&h='+text, 'Help', 'toolbar=no,scrollbars=yes,location=no,statusbar=no,menubar=no,resizable=no,width=300,height=300');
  popup.focus();
}

function fullScreen(theURL) {
window.open(theURL, '', 'fullscreen=yes, scrollbars=auto');
}

function createHelpElm(refElm, title, text) {
	elm = refElm;	
	var body = $('body').get(0);
	
	var p = elm.offset();
	var t = p.top - 13;	
	var l = p.left -6;
	
//dropshadow
	var ds = document.createElement('div')
	ds.id = 'help'
	ds.className = 'bubble dropshadow'
	ds.style.left = (l+6)+'px';
	ds.style.top = (t+7)+'px';
	
	//top of bubble
	var dsct = document.createElement('span');
	dsct.className = 'ct';
	var dscl = document.createElement('span');
	dscl.className = 'cl';
	ds.appendChild(dscl);
	
	//content of bubble
	var dscntnt = document.createElement('div');
	dscntnt.className = 'cntnt';
	var dst = document.createElement('h1');
	dst.innerHTML = title
	dscntnt.appendChild(dst);
	
	//body of bubble
	var dsp = document.createElement('p')
	dsp.innerHTML = text
	dscntnt.appendChild(dsp)
		
	//bottom of bubble
	var dscb = document.createElement('span');
	dscb.className = 'cb';
	var dscbl = document.createElement('span');
	dscbl.className = 'cl';
	dscb.appendChild(dscbl);
	
	//put together the bubble
	ds.appendChild(dsct);
	ds.appendChild(dscntnt);
	ds.appendChild(dscb)
	
//container
	var c = document.createElement('div');
	c.id = 'help';
	c.className = 'bubble';
	c.style.left = l+'px';
	c.style.top = t+'px';
	
	//close bubble 'X'
	var x = document.createElement('div');
	x.className = 'close-window';
	x.onclick = function(){
		$(c).hide();
		$(ds).hide();
	}
	x.onmouseover = function(){
		x.style.backgroundPosition = '0px -14px;'
	}
	x.onmouseout = function(){
		x.style.backgroundPosition = '0px 0px;'
	}
	
	//top of bubble
	var ct = document.createElement('span');
	ct.className = 'ct';
	var cl = document.createElement('span');
	cl.className = 'cl';
	ct.appendChild(cl);
	
	//content of bubble
	var cntnt = document.createElement('div');
	cntnt.className = 'cntnt';
	var t = document.createElement('h1');
	t.innerHTML = title
	cntnt.appendChild(x);
	cntnt.appendChild(t);
	
	//body of bubble
	var p = document.createElement('p')
	p.innerHTML = text
	cntnt.appendChild(p)
		
	//bottom of bubble
	var cb = document.createElement('span');
	cb.className = 'cb';
	var cbl = document.createElement('span');
	cbl.className = 'cl';
	cb.appendChild(cbl);
	
	//put together the bubble
	c.appendChild(ct);
	c.appendChild(cntnt);
	c.appendChild(cb)

	//add it to the parent
	body.appendChild(ds);
	body.appendChild(c);
}

function setElmDisplay(elm,displayvalue) {
	$(elm).css('display',displayvalue);
}

function displayNone(elmid) {
	$(elmid).hide();
	return false;
}

function displayBlock(elmid) {
	$(elmid).show();
	return false;
}

function openWindowScrollNoQS( url, id, width, height )
{
	//extract the url parameters if any, and pass them to the called html
  var tempvar=document.location.toString(); // fetch the URL string
	popup = window.open( url, id, 'toolbar=no,scrollbars=yes,location=no,statusbar=yes,menubar=no,resizable=no,width=' + width + ',height=' + height + '' );
	popup.focus();
}

function openWindowScroll( url, id, width, height )
{
   //extract the url parameters if any, and pass them to the called html
   var tempvar=document.location.toString(); // fetch the URL string
   var passedparams = tempvar.lastIndexOf("?");
   if(passedparams > -1)
      url += tempvar.substring(passedparams);
      
  popup = window.open( url, id, 'toolbar=no,scrollbars=yes,location=no,statusbar=yes,menubar=no,resizable=no,width=' + width + ',height=' + height + '' );
  popup.focus();
}

function clearField(fld)
{
  fld.parentNode.style.backgroundColor = '';
  fld.parentNode.style.color = '';
  $.each(fld.parentNode.childNodes,function(){
		var elm = this;
		if(elm && elm.style) elm.style.color = '';
  });
}

function markField(fld)
{
  fld.parentNode.style.backgroundColor = '#CF2626';
  fld.parentNode.style.color = '#ffffff';
  $.each(fld.parentNode.childNodes,function(){
		var elm = this;
		if(elm && elm.style) elm.style.color = '#ffffff';
  });
}

function submitSearch(){
		window.location.href = '/search-results/?type='+$('#Gsearch-type').get(0).selectedIndex+'&value='+$('#Gsearch-text').val() 
}

function submitViaReturnKey(e){
		var keycode;
		keycode = (window.event)?window.event.keyCode:e.which;
		if (keycode == 13){
			window.location.href = '/search-results/?type='+$('#Gsearch-type').get(0).selectedIndex+'&value='+$('#Gsearch-text').val() 
			return false;
		}else{
			return true;
		}
}

// CONFIGURATION FUNCTIONS --------------------------------------------------------------------

function changeOfficePreference(name,value,onComplete){
	ConfigurationWS.setOfficePreference({pn:name,pv:value},onComplete);
	return false;
}

function changeOfficeSessionPreference(name,value,onComplete){
	SessionConfigurationWS.setOfficeSessionPreference({pn:name,pv:value},onComplete);
	return false;
}

/* --- IsNumeric(sText) ----------------------------------------
 * Determines if the supplied text value is numeric.
 * ------------------------------------------------------------- */
function IsNumeric(sText) {
  var ValidChars = "0123456789.";
  var IsNumber=true;
  var Char;

 
  for (var i = 0; i < sText.length && IsNumber == true; i++) { 
    Char = sText.charAt(i); 
    if (ValidChars.indexOf(Char) == -1) {
      IsNumber = false;
    }
  }
  return IsNumber;
}
