//*****************************************************
// http://js.photoshelter.com/js/APP/20100215/div.js
function getDivObj(divName)
{
	if (document.all && document.all[divName])
		return document.all[divName];
	else if (document.getElementById)
		return document.getElementById(divName);

}

function divStatus(divName)
{
	if (!(divObj = getDivObj(divName)))
		return null;

	if (divObj.style.display == '' || divObj.style.display == 'block')
		return true;
	else
		return false;
	
}

function divSet(divName, flag)
{
	if (typeof(divName) == 'object')
		divObj = divName;
	else if (!(divObj = getDivObj(divName)))
		return;
	
	var			style = 'block';

	if (typeof(flag) == 'string')
		style = flag;

	if (flag)
		divObj.style.display = (document.getElementById) ? style : '';
	else
		divObj.style.display = 'none';
}

function divToggle(divName) 
{
	if (divStatus(divName)) {
		divSet(divName, false)
		return false;
	} else {
		divSet(divName, true);
		return true;
	}
}

function divSwap(div1, div2)
{
	divToggle(div1);
	divToggle(div2);
}

// ########## "prompt" stuff ##########

var		PROMPT_DIVOBJ = null;
var		PROMPT_OFFSET_Y = 0;

function promptActive()
{
	return PROMPT_DIVOBJ;
}

// NOTE: offset argument can be boolean (true/false), number (pixel offset),
// or HTML object (offset will be determined based on position on page)
function promptOpen(name, offset, force)
{
	if (!force && PROMPT_DIVOBJ)
		return false;

	if (!(divObj = getDivObj(name))) {
		//alert('Please wait until the page has completed loading.');
		return false;
	}

	PROMPT_DIVOBJ = divObj;
	if (offset) {
		switch (typeof(offset)) {
		case 'boolean':
			divObj.style.top = PROMPT_OFFSET_Y + 'px'; break;
		case 'number':
			divObj.style.top = offset + 'px'; break;
		case 'object':
			try { ToolMan && ToolMan.coordinates(); } 
			catch (e) { alert(e); return; }
			var		TM = ToolMan.coordinates();
			var		pos = TM.topLeftOffset(offset);
			divObj.style.top = pos.y + 'px'; break;
		}
	}
	divObj.style.visibility = 'visible';
	return true;
}

function promptForce(name, offset)
{
	if (!offset) offset = false;
	promptClose();
	promptOpen(name, offset);
}

function promptClose()
{
	if (!PROMPT_DIVOBJ) return;
	PROMPT_DIVOBJ.style.visibility = 'hidden';
	PROMPT_DIVOBJ = null;
	return;
}

function promptOffset(elem, offset)
{
	try { ToolMan && ToolMan.coordinates(); } 
	catch (e) { alert(e); return; }

	var		TM = ToolMan.coordinates();
	var		pos = TM.topLeftOffset(elem);

	if (offset) pos.y += offset;
 
	PROMPT_OFFSET_Y = pos.y
}


//*****************************************************
// http://js.photoshelter.com/js/APP/20100215/util.js

// ##########################################
// primitives
// ##########################################

function isArray(variable)
{
	 return Boolean( typeof variable == 'object' && variable.length >= 0 );
}

// a "proper" implementation of the escape() routine; 
function httpEscape(str)
{

	var		i, c;
	var		output = "";

	if (!str || (typeof str != "string"))
		return str;

	for (i = 0; i < str.length; i++) {
		c = str.charAt(i);
		if (c.match(/[a-z0-9\/-]/i)) {
			output += c;
			continue;
		}
		c = str.charCodeAt(i);
		output += "%" + c.toString(16);
	}

	return output;
}

// returns number of elements in a formvar multiple
function getElementLength(fVar)
{
	if (!fVar)
		return 0;
	else
		return (fVar.length) ? fVar.length : 1;
}

// checks if a formvar is blank or whitespace;
function checkBlank(fVar, message)
{
	if (fVar.value.search(/^ *$/) == -1)
		return true;
	
	if (message) alert("Please enter " + message + ".");
	return false;
}

// returns number of items that are checked
function checkCount(fVar)
{

	if (!fVar)
		return false;

	var count = 0;
	var n = getElementLength(fVar);

	switch (n) {
		case 0:
			return 0;
		case 1:
			return (fVar.checked) ? 1 : 0;
		default:
			for (var i = 0; i < n; i++) {
				count += ((fVar[i].checked) ? 1 : 0);
			}
			return count;
	}
}

function checkSize(fVar, maxsize, name)
{
	if (!fVar) return true;

	var		n = fVar.value.length;
	if (n > maxsize) {
		var diff = n - maxsize;
		alert("The maximum size of the field \"" + name + "\" is " + maxsize +
		      " characters; you have entered " + n + " characters.\n" +
		      "Please reduce your entry by at least " + diff + " characters.");
		fVar.focus();
		return false;
	} else return true;
}

// sets all checkboxes from element[0] to the specified value
function cbSet(fVar, value)
{
	var n = getElementLength(fVar);

	switch (n) {
		case null:
		case false:
		case 0:
			return;
		case true:
		case 1:	
			fVar.checked = value;
			break;
		default:
			for (var i = 0; i < n; i++) {
				fVar[i].checked = value;
			}
	}
}


// returns value of the specified fVar
// NOTE: it returns an ARRAY, not a single value
// skipHidden determines if getVal retrieves hidden variables or not
function getVal(fVar, skipHidden)
{
	var		val = new Array();
	var		type = null;

	if (!fVar) return null;

	if (!skipHidden) skipHidden = false;
	
	if (fVar.type) type = fVar.type;
	else if (getElementLength(fVar)) type = fVar[0].type;

	switch (type) {
	case "select-one":
		val.push(fVar.options[fVar.selectedIndex].value);
		break;
	case "select-multiple":
		for (var i = 0; i < fVar.length; i++) {
			if (fVar.options[i].selected)
				val.push(fVar.options[i].value);
		} 
		break;
	case "checkbox":
	case "radio":
		if (getElementLength(fVar) > 1) {
			for (var i = 0; i < fVar.length; i++) {
				if (fVar[i].checked)
					val.push(fVar[i].value);
			}
		} else 
			val.push((fVar.checked) ? fVar.value : null);
		break;
	case "text":
	case "textarea":
	case "password":
		val.push(fVar.value);
		break;
	case "hidden":
		val.push((skipHidden) ? null : fVar.value);
		break;
	default:
		val.push(null);
	}

	return val;
}


// single variable wrapper for getVal
function getValSingle(fVar, skipHidden)
{
	if (!skipHidden) skipHidden = false;

	var		val = getVal(fVar, skipHidden);
	
	if (val) return val[0];
	else return null;
}

function setVal(fVar, val)
{
// attempts to set a fVar s value to the value specified by "val";
// NOTE: val must be an array!!! for non-multiple formvars, pass the
// desired value in val[0].

	var		i, j;

	if (!fVar) return;
	
	if ((typeof val) != 'object')
		val = [val];

	switch (fVar.type) {
		case "select-one":
		case "select-multiple":
			if (!val || !val.length) {
				fVar.selectedIndex = 0;
				break;
			}
			for (i = 0; i < val.length; i++) {
				for (j = 0; j < fVar.length; j++) {
					if (fVar.options[j].value == val[i]) {
						fVar.options[j].selected = true;
						break;
					}
				}
			}
			break;
		case "checkbox":
		case "radio":
			if (!val || !val.length) {
				fVar.checked = false;
			} else {
				for (i = 0; i < val.length; i++) {
					if (fVar.value == val[i]) {
						fVar.checked = true;
						break;
					} else {
						fVar.checked = false;
					}
				}
			}
			break;
		case "text":
		case "textarea":
		case "password":
			fVar.value = (val && val.length) ? val[0] : "";
			break;
	}
	return;
}


function getCookieData(labelName)
{
	 var labelLen = labelName.length;
	 var cookieData = document.cookie;
	 var cLen = cookieData.length;
	 var i = 0;
	 var cEnd;
	 while (i < cLen) {
		  var j = i + labelLen;
		  if (cookieData.substring(i,j+1) == labelName + '=') {
			   cEnd = cookieData.indexOf(";",j);
			   if (cEnd == -1) {
				    cEnd = cookieData.length;
			   }
			   return unescape(cookieData.substring(j+1, cEnd))
		  }
		  i++;
	 }
	 return ""
}


// ##########################################
// more specific form handling functions...
// ##########################################

function detectSelect(formName, varName) 
{

	var objCheckbox = document.forms[formName].elements[varName];

	var objArray = new Array();

	if (isArray(objCheckbox)) {
		for (i=0; i < objCheckbox.length; i++) {
			if (objCheckbox[i].checked == true) {
				objArray.push(objCheckbox[i].value);
			}
		}
	} else {
		objArray.push(objCheckbox.value);
	}
	return objArray;
}

function toggle(divname)
{
	if (document.all) {
		if (document.all[divname].style.display==''||document.all[divname].style.display=='block'){
			document.all[divname].style.display='none';
		} else {
			document.all[divname].style.display='';
		}
	} else if (document.getElementById) {
		if (document.getElementById(divname).style.display=='block'||document.getElementById(divname).style.display=='') {
			document.getElementById(divname).style.display='none';
		} else {
			document.getElementById(divname).style.display='block';
		}
 	}
}

function checkSel(fVar) {
	if (!fVar)
		return false;
		
	var count = 0;
	var n = getElementLength(fVar);
	
	switch(n) {
		case 0:
			return false;
			break;
		case 1: 
			return (fVar.checked) ? true : false;
			break;
		default: 
			for (var i = 0; i < n; i++) {
				count += ((fVar[i].checked) ? 1 : 0);
			}
			if (count > 0) return true; else return false;
			break;
	}
}

var SUBMIT_ONCE_FLAG = false;

function submitOnce(fObj, f_nosubmit)
{
    if (!SUBMIT_ONCE_FLAG) {
	SUBMIT_ONCE_FLAG = true;
	try {
		fObj.submitButton.value = " Please wait... ";
		fObj.submitButton.disabled = true;
	} catch (e) {}
	if (!f_nosubmit) fObj.submit();
    }

    return false;
}

function isCanProvince (s)
{
	switch (s) {
	case 'ON':
	case 'QC':
	case 'NS':
	case 'NB':
	case 'MB':
	case 'BC':
	case 'PE':
	case 'SK':
	case 'AB':
	case 'NL':
		return true;
	default: return false;
	}
}

function selectState(state, country)
{
	var s = _bsForm.getValue(state);

	if (isCanProvince(s))
		_bsForm.setValue(country, 'CAN');
	else if (!empty(s))
		_bsForm.setValue(country, 'USA');
}

function selectCountry(country, state)
{
	if (!state) return;
	var s = _bsForm.getValue(state);

	switch (_bsForm.getValue(country)) {
	case 'USA':
		if (isCanProvince(s))
			_bsForm.setValue(state, '');
		break;
	case 'CAN':
		if (!isCanProvince(s))
			_bsForm.setValue(state, '');
	default:
		_bsForm.setValue(state, '');
	}
}

function clearDateVal(fVar)
{
	 if (!fVar.value.match(/[0-9]{1,}/))
		  fVar.value = '';
}

function catBillDate(fObj, alias)
{
	if (!alias) alias = 'BI';

	// skip for invoice type - kind of a hack for BI_METHOD radio
	if (fObj.BI_METHOD && fObj.BI_METHOD.length > 1 && fObj.BI_METHOD[1].checked)
		return true;

	// set CC_EXP
	if (!fObj.elements[alias + '_CC_EXP__MM'].selectedIndex ||
	    !fObj.elements[alias + '_CC_EXP__YY'].selectedIndex) {
		alert('Please select your credit card expiration date.');
		return false;
	} else
		fObj.elements[alias + '_CC_EXP'].value = getValSingle(fObj.elements[alias + '_CC_EXP__MM']) + getValSingle(fObj.elements[alias + '_CC_EXP__YY']);

	return true;
}


// ##########################################
// misc. front-end stuff
// ##########################################

var             ACT_SRC_DIR = "/img/act/";

function actImgOver(imgName,imgSrc,mode) {
	if (mode == 1) {
    		document[imgName].src = ACT_SRC_DIR + imgSrc + "-over.gif";
	} else {
    		document[imgName].src = ACT_SRC_DIR + imgSrc + ".gif"; 
	}
}

var splashMe = null;
function splash()
{
	 var w = 500;
	 var h = 100;
	 var win1 = ((screen.width - w) / 2);
	 var win2 = ((screen.height - h) / 2);
	 var url = '/mem/upload_splash.html';
	 var settings = 'height='+h+',';
	     settings += 'width='+w+',';
	     settings += 'top='+win2+',';
	     settings += 'left='+win1+','
	     settings += 'resizable=no,scrollbars=no';
	 splashMe = window.open(url,"",settings);
	 return true;	      
}

function closeSplash()
{
	if(window.splashMe) {
	 	splashMe.close();
	}
}

function popupWin(url, width, height, name)
{
	if (!name) name = "popup";

	var foo = window.open(url, name, "resizable=yes,scrollbars=yes,location=no,menubar=no,width=" + width + ",height=" + height);
	if (foo) foo.focus();
}

function helpPop(help, width, height)
{
	if (!width) width = 850;
	if (!height) height = 600;

	popupWin('/help/pop/' + help, width, height);
}

function convertBytes(bytes)
{
	var			unit;
	var			n;
	
	if (bytes/(1024) < 1) {
		unit = 'bytes';
		n = Math.floor(bytes);
	} else if (bytes/(1024*1024) < 1) {
		unit = 'KB';
		n = Math.floor(10*bytes/(1024))/10;
	} else if (bytes/(1024*1024*1024) < 1) {
		unit = 'MB';
		n = Math.floor(10*bytes/(1024*1024))/10;
	} else {
		unit = 'GB';
		n = Math.floor(10*bytes/(1024*1024*1024))/10;
	}
	
	n = n.toString();
	if (n.indexOf('.') < 0) n += '.0';

	return n + unit;
}

function blockToggle (obj, cn)
{
	if (!isset(cn)) cn = 'open';

	var div = obj.parentNode;
	var f = _bsDom.hasClass(div, cn);
	if (!f) _bsDom.addClass(div, cn);
	else _bsDom.delClass(div, cn);
};

function _seoIcon (obj)
{
	var str = "This field can affect your search engine optimization (SEO).<br><b>&raquo;</b> <a href=\"/" + PS.app.pvGet('area') + "/home/help/tut/seo\" class=\"bold\" target=\"_blank\">Learn More</a>";
	_balloon(obj, '<h3>SEO</h3>', str)
}

function seoIcon (f_write, style)
{
	var str = '<a href="javascript:void(0);" onClick="_seoIcon(this);"';
	if (style) str += ' style="' + style + '"';
	str += ' class="seoIcon">';
	str += '<img src="/img/icon/seo.gif" width="24" height="11" alt="SEO" border="0">';
	str += '</a>';

	if (f_write) document.write(str);
	else return str;
}


//*****************************************************
// http://js.photoshelter.com/js/APP/20100215/events.js

var		WINDOW_ONLOAD_QUEUE = new Array();	// onLoad event queue

function addWindowOnLoadEvent(str)
{
	WINDOW_ONLOAD_QUEUE.push(str);
}

function execWindowOnloadQueue()
{
	while (WINDOW_ONLOAD_QUEUE.length) {
		eval(WINDOW_ONLOAD_QUEUE.shift());
	}
}

// prevents events from bubbling (triggering events in parent DOM objects)
function noEventBubble(e)
{
	if (!e)
		window.event.cancelBubble = true;
	else if (e.stopPropagation) {
		e.stopPropagation();
	}
}

// cross-browser event attachment
function addEvent(obj, event, func)
{
	if (obj.addEventListener)
		obj.addEventListener(event, func, false);
	else if (obj.attachEvent)
		obj.attachEvent('on' + event, func);
}