//site specific functions
function openFileManager()
{
	return openPopup('/admin/content/file_manager.asp','AdminFileManager','scrollbars,resizable,status,toolbar=false',640,480);
}

function openOrderPrint(OrderId)
{
	return openPopup('print.asp?id='+OrderId,'OrderPrint','scrollbars,resizable,status,menubar,toolbar=false',560,480);
}

function openOrderPrintC(OrderId)
{
	return openPopup('print_c.asp?id='+OrderId,'OrderPrintC','scrollbars,resizable,status,menubar,toolbar=false',560,165);
}

function openImgPreview(url)
{
	return openPopup(url,'AdminImgPreview','scrollbars,resizable,status,toolbar=false',540,405);
}

function openGalleryViewer(ImageId)
{
	return openPopup('viewer.asp?ImageId='+ImageId,'ImageGallery','status,toolbar=false',522,419);
}

//window functions
var blnRefreshWindow = false;

function windowIsOpen(objWindow)
{
	var blnIsOpen = false;
	
	if (typeof(objWindow)=='object') {
		if (!objWindow.closed) {
			blnIsOpen = true;
		}
	}
	return blnIsOpen;
}

function setOpenerRefresh()
{
	if (windowIsOpen(opener)) {
		opener.blnRefreshWindow = true;
	}
}

function focusOpener()
{
	if (windowIsOpen(opener)) {
		opener.focus();
	}
}

function refreshOpener(strDefaultURL,blnCloseMe)
{
	var blnExists = false;
	
	if (windowIsOpen(opener)) {
	 	if (opener.blnRefreshWindow==true) {
			opener.location.reload();
			opener.focus();
		} else {
			blnCloseMe = false;
		}
		blnExists = true;
	}
	
	if (blnExists==false) {
		window.open(strDefaultURL);
		blnCloseMe = true;
	}
	
	if (blnCloseMe==true) window.close(self);
}

function openerLocation(strLocation)
{
	if (windowIsOpen(opener)) {
		opener.location.href=strLocation;
		opener.focus();
	} else {
		window.open(strLocation);
		window.close(self);
	}
}

function clickOpenerButton(buttonId,openerUrl)
{
	var openerForm;
	var buttonToClick;
	
	if(opener)
	{
		openerForm = opener.document.forms[0];
		if(openerForm) buttonToClick = eval('openerForm.'+buttonId);
	}
	if(buttonToClick)
	{
		buttonToClick.click();
	}
	else
	{
		openerLocation(openerUrl);
	}
	closePopup();
}

function focusPopup(objPopup,theURL,winName,features,width,height)
{
	if (!windowIsOpen(objPopup)) {
		objPopup = returnPopup(theURL,winName,features,width,height);
	}
	 objPopup.focus();
	 return objPopup;
}

function openPopup(theURL,winName,features,width,height)
{
	var objPopup = returnPopup(theURL,winName,features,width,height);
	objPopup.focus();
	return false;
}

function returnPopup(theURL,winName,features,width,height)
{
	var winWidth	= width;
	var winHeight	= height;
	var strWinSize	= ",width=" + winWidth + ",height=" + winHeight;

	if (window.screen) {
		var winPosL = (screen.availWidth - winWidth) / 2;
		var winPosT = (screen.availHeight - winHeight) / 2;
		strWinSize += ",left=" + winPosL + ",screenX=" + winPosL + ",top=" + winPosT + ",screenY=" + winPosT;
	}	
	
	return window.open(theURL,winName,features + strWinSize);
}

function closePopup()
{
	window.close(self);
	return false;
}

function raiseAlert(id, message)
{
	if((document.cookie)&&(document.cookie.toString().indexOf(id)<0))
	{
		document.cookie = 'AlertID='+id;
		alert(message);
	}
}

function setWinStatus(value)
{
	window.status=value;
	return true;
}

//date functions
function ClearDayList(obj_selectDay)
{
	var i;
	var intLength;
	intLength = obj_selectDay.options.length = 1;
}
	
function IsLeapYear(int_year)
{
	var blnIsLeapYear = false;
	
	if ((((int_year % 4) == 0) && ((int_year % 100) != 0)) || ((int_year % 400) == 0))
	{
		blnIsLeapYear = true;
	}
	
	return(blnIsLeapYear);
}

function PopulateDays(obj_selectMonth, obj_selectDay, obj_selectYear, int_defaultDay)
{
	ClearDayList(obj_selectDay);
	
	var intYear = obj_selectYear.options[obj_selectYear.selectedIndex].value
	
	if (IsLeapYear(intYear) == true)
	{
		arrDaysInMonth[2] = 29;
	}
	else
	{
		arrDaysInMonth[2] = 28;
	}
	
	var intDaysInMonth	= arrDaysInMonth[obj_selectMonth.selectedIndex];
	var intDay			= '';
	
	for (i = 1; i <= intDaysInMonth; i++)
	{
		intDay = '' + i + ''
		if (intDay.length == 1)
		{
			intDay = '0' + intDay;
		}
		obj_selectDay.options[i] = new Option(intDay, intDay, false, false);
	}
	
	var intSelectedIndex = 0;
	
	if (int_defaultDay != '')
	{
		intSelectedIndex = int_defaultDay;
	}
	else
	{
		intSelectedIndex = 0;
	}
	
	obj_selectDay.selectedIndex = intSelectedIndex;
}

function ConstructDate(txt_hiddenField, int_monthSelected, int_daySelected, int_yearSelected)
{
	var txt_hiddenTemp;
	txt_hiddenTemp = int_monthSelected + '/' + int_daySelected + '/' + int_yearSelected;
	
	if(txt_hiddenTemp == '//')
	{
		txt_hiddenTemp = ''
	}
	
	txt_hiddenField.value = txt_hiddenTemp
}
	
//form functions
function MakeFormReadOnly(formName)
{
	if(document.forms[formName])
	{
		var f = document.forms[formName];
		var strType = '';
		
		for(var i=0; i<f.length; i++)
		{
			strType = f.elements[i].type.toLowerCase();
			if(strType.indexOf('text',0)==0)
			{
				f.elements[i].readOnly = "true";
			}
			else if(strType.indexOf('hidden',0)<0)
			{
				f.elements[i].onmousedown = DisableSrcElement;
			}
		}
	}
}

function DisableSrcElement(e)
{
	if(window.event) {
		window.event.srcElement.disabled = "true";
	} else if(e) {
		e.target.disabled = "true";
	}
}

function ClickOnCrKeyPress(e,button)
{
	if(getkey(e)==13)
	{
		button.click();
		return false;
	}
	else return true;
}

function VoidOnCrKeyPress(e)
{
	return (getkey(e)!=13);
}

function getkey(e)
{	//	example usage: if(getkey(event)==13) { *carriage return was pressed* } 
	if (window.event)
	   return window.event.keyCode;
	else if (e)
	   return e.which;
	else
	   return null;
}

function InlineDelete_Submit(sender,keyfield,id)
{
	var row = document.getElementById(id.toString());
	var strClassName = '';
	if(row)
	{
		strClassName = row.className;
		row.className = 'delitem';
	}
	
	if(confirm('Are you sure you want to delete\t\nthe highlighted record?\t'))
	{
		eval('sender.form.'+keyfield).value = id;
		return true;
	}
	else
	{
		if(row) row.className = strClassName;
		return false;
	}
}

function formFocus(strFormname, strElement) 
{
	var objE = eval('document.forms.' + strFormname + '.' + strElement);
	if (objE) objE.focus();
}

function CheckALL(objCheckbox)
{
	var len = objCheckbox.length;
	var i=0;
	for (i=0 ; i<len ; i++) {
		objCheckbox[i].checked=true;
	}
}

function UnCheckALL(objCheckbox)
{
	var len = objCheckbox.length;
	var i=0;
	for (i=0 ; i<len ; i++) {
		objCheckbox[i].checked=false;
	}
}

function formLengthChecker(objForm)
{
	var blnLengthOK = true;
	var len = objForm.length;
	var i=0;
	for (i=0 ; i<len ; i++) {
		if (objForm[i].value) {
			if (objForm[i].value.length > 102300) {
				blnLengthOK = false;
			}
		}
	}
	return blnLengthOK;
}

function textCounter(field,cntfield,maxlimit)
{
	if (field.value.length > maxlimit) {
		// if too long trim it!
  		field.value = field.value.substring(0, maxlimit);
	} else {
		// otherwise, update 'characters left' counter
  		cntfield.value = maxlimit - field.value.length;
	}
}

function textTrimmer(field,maxlimit)
{
	if (field.value.length > maxlimit) {
		// if too long trim it
  		field.value = field.value.substring(0, maxlimit);
	}
}

function submitJUpload(formName)
{
	if(document.JUpload.getFileCount()>0)
	{
		return document.JUpload.startTransfer(formName);
	}
	else
	{
		return true;
	}
}

//DTHML functions
function dhtmlDisplay(domId,value)
{
	document.getElementById(domId).style.display = value;
}

function switchDisplay(domId)
{
	var CssStyle = document.getElementById(domId).style;
	if(CssStyle)
	{
		if(CssStyle.display=='')
		{
			CssStyle.display = 'none';
		}
		else
		{
			CssStyle.display = '';
		}
		setCssDisplayCookie(domId);
	}
	return false;
}

function switchDisplayToggle(parentId,childOn,childOff)
{
	var CssStyle = document.getElementById(parentId).style;
	if((CssStyle)&&(CssStyle.display==''))
	{
		dhtmlDisplay(childOff,'');
		dhtmlDisplay(childOn,'none');
	}
	else
	{
		dhtmlDisplay(childOff,'none');
		dhtmlDisplay(childOn,'');
	}
	return false;
}

function getCssDisplayCookie(domId)
{
	if(document.cookie)
	{
		var CssStyle = document.getElementById(domId).style;
		if((CssStyle)&&(document.cookie.toString().indexOf(domId+'DisplayOn')<0))
		{
			CssStyle.display = 'none';
		}
		else
		{
			CssStyle.display = '';
		}
	}
}

function setCssDisplayCookie(domId)
{
	if(document.cookie)
	{
		var CssStyle = document.getElementById(domId).style;
		if((CssStyle)&&(CssStyle.display==''))
		{
			document.cookie = domId+'Display='+domId+'DisplayOn';
		}
		else
		{
			document.cookie = domId+'Display='+domId+'DisplayOff';
		}
	}
}

function dhtmlDisplay(domID,dVal)
{
	document.getElementById(domID).style.display = dVal;
}

function dhtmlFormEdit(objForm,domLEN,domID)
{
	var styleView;
	var styleEdit;
	var i=1;
	for (i=1 ; i<=domLEN ; i++) {
	
		var styleView = document.getElementById('view'+i.toString()).style;
		var styleEdit = document.getElementById('edit'+i.toString()).style;
		
		if ((i.toString()==domID.toString())&&(styleView.display=="")) {
			styleView.display = "none";
			styleEdit.display = "";
			intLastOpenRow = i.toString();
		} else {
			styleView.display = "";
			styleEdit.display = "none";
		}
	}
	
	objForm.reset();
	return false;
}

function dhtmlFormSubmit(objForm1,objForm2,domID,sysAction)
{
	var i=0;
	for (i=0 ; i<objForm2.length ; i++) {
	
		var objE = eval('objForm1.' + objForm2[i].name + domID);
				
		if (objE) {
			if (objE.type == 'checkbox'|objE.type == 'radio') {
				if (objE.checked) {
					objForm2[i].value = objE.value;
				}
			} else if (objE.type == 'select') {
				var j=0;
				for (j=0 ; j<objE.length ; j++) {
					if (objE.options[i].selected) {
						objForm2[i].value = objE.options[j].value;
					}
				}
			} else {
				objForm2[i].value = objE.value;
			}
		}
	}
	
	objForm2.sys_id.value = domID;
	objForm2.sys_action.value = sysAction;
	
	var blnSubmit;
	if (sysAction == 'delete') {
		blnSubmit = confirm('Click OK to delete this record.');
	} else {
		blnSubmit = true;
	}
	
	if (blnSubmit) objForm2.submit();
	return false;
}

function ul_onclick(jsObj)
{
   var i;
   var style;
  
   for (i = 0; i < jsObj.children.length; i++)
   {
       style = jsObj.children[i].style;
       if (style.display == "none")
       {
           style.display = "";
       }
       else
       {
           style.display = "none";
       }
   } 
}

//misc functions
function verifyMsg(jsStrURL, jsStrMsg) 
{
	if (confirm (jsStrMsg)) 
	{		
		window.location=jsStrURL;
		return true;
	}
}

function buildHumanSQL(objElement, strHeadline)
{
	var inputLocal	= objElement;
	var strSQLHuman	= '<b>' + strHeadline + '</b>\n';
		strSQLHuman	+='<ul type=square style="margin-top:0; margin-left:15; padding:5">\n';
	var blnIsSelected = false;
	
	if (inputLocal) {
		var len = inputLocal.length;
		var i=0;
		for (i=0 ; i<len ; i++) {
		
			if (inputLocal.options[i].selected) {
				blnIsSelected = true;
				strSQLHuman += '<li>' + inputLocal.options[i].text + '<br></li>\n';
			}
		}
	}
	
	strSQLHuman += '</ul>'
	if (blnIsSelected) objElement.form.txt_sqlHuman.value = strSQLHuman;
	return true;
}

// shift all the characters in the inval by shiftval characters from the charset
// example:  "cat", 2, "abcdefghijklmnopqrstuvwxyz" would become "ecv"
// if a character is not found in charset, it is untouched
// if a shift operation goes out of bounds, it will roll to the other side of charset
function CharShiftDecrypt(strInVal, shiftval, shiftCharSet)
{
	var strInString = new String(strInVal);
	var intInString = strInString.length;
	var strCharSet = new String(shiftCharSet);
	var intCharSetLen = strCharSet.length;
	var strOutVal = new String('');
	
	var nextchar, nextindex, i;
	
	// for each character
	for (i=0 ; i < intInString ; i++)
	{	// grab the next character to decrypt
		nextchar = strInString.substr(i, 1);
		// look it up in charset
		nextindex = strCharSet.indexOf(nextchar, 0);
		if (nextindex >= 0)
		{	// found it, modulo it so we can stay in bounds for next operation
			nextindex = (nextindex  - shiftval) % intCharSetLen;
			// check bounds of nextindex
			if (nextindex < 0)
			{	// wrap around to high end of charset
				nextindex = nextindex + intCharSetLen;
			}
			else if (nextindex >= intCharSetLen)
			{	// this wont happen btw, becuase of modulo, but anyway
				nextindex = nextindex - intCharSetLen;
			}
			strOutVal += strCharSet.charAt(nextindex);
		}	
		else
		{	// char not found in set, so add it as is
			strOutVal += nextchar;
		}
	}
	
	return strOutVal;
}