//alert(a);
function includeJavascript(acJsFile) 
{
    document.write('<script src="' + acJsFile + '">' + '<\/script>');
}

includeJavascript("/v3/utils.js");

function populateFieldList(aField, alDataList, alDispList, acDelimiter)
{
	dataList = alDataList.split(acDelimiter);
	displayList = alDispList.split(acDelimiter);
	dataValue = aField.value;
	bKeepOldValue = false;
	clearFieldList(aField);
	for (i = 0; i < dataList.length; i++)
	{
		newLine = aField.length;	
		aField.options[newLine] =new Option(displayList[i], dataList[i]);
		if (dataList[i] == dataValue)
			bKeepOldValue = true;
	}
	if (bKeepOldValue)
	{
		aField.value = dataValue;
	}
	else
	{
		if (dataList.length > 0)
			aField.value = dataList[0];
	}
}

function clearFieldList(aField)
{
	if (aField != null && aField.length > 0)
	{
		for (i = (aField.length - 1) ; i >= 0; i--)
			aField.options[i] = null;
	}
}

function getLocation(locations)
{
	rExp = /,/g;
	locations = locations.replace(rExp, "|");
	locationIDs = window.xShowModalDialog("/v3/popups/location.cfm?locations=" + locations, "",
																  "status:no;dialogWidth:640px;dialogHeight:500px;resizable:yes;scrollbars:no;center:yes");	
	return locationIDs;
}

function sort1(key)
{
	sort(key);
}

function sort1Action(key, cAction)
{	
	var iFrmOrder = getMainFormOrder();
	
	cActionFld = eval("document." + document.forms[iFrmOrder].name + ".cAction");
	cFrm = eval("document." + document.forms[iFrmOrder].name);

	if (cFrm != null)
	{
		cFrm.target = "";
		cFrm.action = "#";
	}
			
	if (cActionFld != null)
	{
		cActionFld.value = cAction;
	}

	sort(key);
}

function page1(no, cAction)
{
	if ((cAction != null) && (cAction != ""))
	{
		var iFrmOrder = getMainFormOrder();
		cActionFld = eval("document." + document.forms[iFrmOrder].name + ".cAction");
		if (cActionFld != null)
		{
			cActionFld.value = cAction;
		}
	}

	page(no);
}

function markChange()
{
	var iFrmOrder = getMainFormOrder();

	cModifiedFld = eval("document.forms[" + iFrmOrder + "].mgmt_sys_modified");
	//cModifiedFld = eval("document." + document.forms[iFrmOrder].name + ".mgmt_sys_modified");
	if (cModifiedFld != null)
	{
		cModifiedFld.value = 1;
	}
}


function submitForm(aForm, cAction)
{
	if (cAction != null)
	{
		aForm.cAction.value = cAction;
	}
	aForm.submit();
}

function gotoUrl(acUrl)
{
	location.href = acUrl;
}

function listFind(aList, aValueToFind, aSeparator)
{
	arrListValues = aList.split(aSeparator);
	bFound = false;
	for (i = 0; i < arrListValues.length; i++)
	{
		if (arrListValues[i] == aValueToFind)
		{
			bFound = true;
			break;
		}
	}
	return bFound; 
}


function ShowHideSpan(displayValue,spanName,selectedValue)
{
	if (listFind(displayValue, selectedValue, "|"))
	{
		document.getElementById(spanName).style.visibility='visible';
	}
	else
	{
		document.getElementById(spanName).style.visibility='hidden';
	}
}

function ShowHideSpanBlocked(displayValue, spanName, selectedValue, blockValue, bExclusiveDisplay)
{
	var DisplayBlockValue = blockValue || 'block';
	var oSpanName = spanName.split(",");

	for (var i=0; i<oSpanName.length; i++)
	{
		if (bExclusiveDisplay != null && bExclusiveDisplay) 
			ShowHideSpan(displayValue, oSpanName[i], selectedValue);

		if (listFind(displayValue, selectedValue, "|"))
		{
			document.getElementById(oSpanName[i]).style.display=DisplayBlockValue;
		}
		else
		{
			document.getElementById(oSpanName[i]).style.display='none';
		}
	}
}

function countCheckedChkboxes(aCheckBox)
{
	var iCheckedChkBoxes = 0;

	if (aCheckBox != null)	
	{
		var bIsArray = aCheckBox.length != undefined;
	
		if (bIsArray)
		{
			for (var i=0; i <aCheckBox.length; i++)
			{
				if (aCheckBox[i].checked)
					iCheckedChkBoxes ++;
			}
		} else
		{
			if (aCheckBox.checked)
				iCheckedChkBoxes = 1;
		}
	}
	return iCheckedChkBoxes;
}


function getValuesCheckedChkboxes(aCheckBox) {
	var values = new Array();
	var strValues = new String();
	var valuesLength = 0;
	strValues = "";
	if (aCheckBox != null) {
		var bIsArray = aCheckBox.length != undefined;
		if (bIsArray) {
			for (i = 0; i < aCheckBox.length; i++) {
				if (aCheckBox[i].checked) {
					strValues = strValues + aCheckBox[i].value + ",";
				}
			}
			for (i = 0; i < values.length; i++) {
				strValues = strValues + values[i];
			}
			valuesLength = String(strValues).length;
			strValues = String(strValues).substring(0, valuesLength - 1);
		}
		else {
			if (aCheckBox.checked) {
				strValues = aCheckBox.value;
			}
		}
	}
	return strValues;
}


function num_format(x) { // format numbers with two digits
	sgn = (x < 0);
	x = Math.abs(x);
	x = Math.floor((x * 100) + .5);
	i = 3;
	y = "";
	while(((i--) > 0) || (x > 0)) {
		y = (x % 10) + y;
		x = Math.floor(x / 10);
		if(i == 1) {
			y = "." + y;
		}
	}
	if(sgn) {
		y = "-" + y;
	}
	return(y);
}

function formatCurrency(num, aiDecimalPlaces) 
{
	var iMultip = Math.pow(10, aiDecimalPlaces);
	var cFormattedVal = "";		
	num = num.toString();
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*iMultip+0.50000000001);
	cents = num%iMultip;
	num = Math.floor(num/iMultip).toString();

	cents = rightPad(cents, aiDecimalPlaces, "0");

	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));
	if (aiDecimalPlaces > 0)
		cFormattedVal = (((sign)?'':'-') + num + '.' + cents);
	else
		cFormattedVal = (((sign)?'':'-') + num );
	return cFormattedVal;
}

function isNumber(aString)
{
	var bIsNumber = false;
	if (trim(aString) != "")
	{
		var noVal = Number(aString)
		bIsNumber = (!isNaN(noVal));
	}
	return bIsNumber;
}

// makes sure an answer was checked before submitting a vote for a poll
function SubmitPoll(formName, pollID) {
	var sForm = "document." + formName + "_" + pollID; //form string
	var oForm = eval(sForm); //form object
	var oRadio = eval(sForm + ".poll_" + pollID); // get the whole set of radio buttons.
	var itemChecked = false;
	for (var j = 0 ; j < oRadio.length ; ++j) {
		if (oRadio[j].checked) {
			itemChecked = true;
			break;
		}
	}
	if (itemChecked) {
		oForm.submit();
	}
	else {
		alert("Please select an answer for this poll before you can vote!");
	}
}


//open a popup for viewing a 360 tour
function TourPopup(img,licd,picnum,mediaTp) 
{
	var ScreenWidth=window.screen.width;
	var ScreenHeight=window.screen.height;
	var movefromedge=0;
	placementx=(ScreenWidth/2)-((340)/2);
	placementy=(ScreenHeight/2)-((330+50)/2);
	var PopUpUrl = "/nqcontent.cfm?a_name=V3_360_popup&img=" + img+'&li_cd='+licd+'&picnum='+picnum + '&mediaTp=' + mediaTp;
	WinPop = window.open(PopUpUrl,"","width=520,height=550,toolbar=0,location=0,directories=0,status=0,scrollbars=1,menubar=0,resizable=1,left="+placementx+",top="+placementy+",screenX="+placementx+",screenY="+placementy+",");
}



/*function newOpportunity(acOpportunityTpHiddenNm)
{
	var cSourceUrl = encodeURI(location.href);
	location.href ="/nqcontent.cfm?a_name=v3_opportunity_form&opp_tp=" + acOpportunityTpHiddenNm + "&sourceUrl=" + cSourceUrl;
}*/

function newOpportunity(acArticleNm, acAddlUrlParams)
{
	var cSourceUrl = encodeURIComponent(location.href);
	var cUrl = "/nqcontent.cfm?a_name=" + acArticleNm;
	if (acAddlUrlParams != null)
	 	cUrl +="&" + acAddlUrlParams;
	cUrl += "&sourceUrl=" + cSourceUrl;
	location.href =cUrl;
}

function selectLocation(acDataFieldId, acDispFieldId, abUseLongLocTitle)
{
	var oDispId = document.getElementById(acDataFieldId);
	var oValId = document.getElementById(acDispFieldId);
	var cBrowser = getBrowser();
	if ( cBrowser == 'IE')
	{
		var val = window.showModalDialog(
			"nqcontent.cfm?a_name=v3_location_popup&dispid=" + acDataFieldId + "&valid=" + acDispFieldId + "&uselong=" + abUseLongLocTitle, "",
			"dialogHeight: 640px; dialogWidth: 500px; center: yes; edge: Raised; resizable: Yes; status: No;");
		if (val != null)
		{
			var aSplit = val.split("|");
			oDispId.value=aSplit[0];
			oValId.value=aSplit[1];
		}
	}
	else
	{
		window.open(
			"nqcontent.cfm?a_name=v3_location_popup&dispid=" + acDataFieldId + "&valid=" + acDispFieldId + "&uselong=" + abUseLongLocTitle, 
			"", 
			"height=640,width=500,top=300,left=450,status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes");
	}
}

// Open a listing in a new unnamed window
// unnamed = each click will result in a new window to pop up
function OpenLink(listing)
{
	var oPreview = window.open(listing, "_blank");
}

function CompareAndShow(expressions, exec_this, delimeter)
{
	if (ValidateExpressions(expressions,delimeter))
		eval(exec_this);
}

function ValidateExpressions(ExpressionList, delimeter)
{
	var bRetVal=true;
	if (delimeter==null) delimeter="|";
	var exoList = ExpressionList.split(delimeter);
	for (i = 0; i < exoList.length; i++)
	{	
		if (!eval(exoList[i]))
		{
			bRetVal=false;
			break;
		}
	}
	return bRetVal;
}

function ValidateNumbers(min_num, max_num, field)
{
	var bRetVal=true;
	if (parseInt(min_num) > parseInt(max_num))
	{
		bRetVal=false;
		alert("The maximum " + field + " cannot\nbe less than the minimum");
	}
	return bRetVal;
}

function checkUserLogin(userlogin) {
	if (getBrowser() == 'IE'){
		d_id = window.showModalDialog('/v3/popups/check_user.cfm?login=' + userlogin, '',
						'status:no;center:yes;dialogWidth:430px;dialogHeight:170px;resizable:no;scrollbars:no');
		return d_id;
	}
	else
	{
		window.open('/v3/popups/check_user.cfm?login=' + userlogin, '',
						'status=no,center=yes,width=430px,height=170px,resizable=no,scrollbars=no');
	}
}

function showHideDiv(elDiv) {
	var elCurrentState = document.getElementById(elDiv).style.display;
	if (elCurrentState == "block") {
		document.getElementById(elDiv).style.display = "none";
	}
	else {
		document.getElementById(elDiv).style.display = "block";
	}
}
/* Author Athos- Used to popup the picture view popup, Called from bs_listing_section_photo_office and bs_listing_section_media */
function openPhotoPopup(licd, picnum, mediaTp, showAll) 
{
	if (showAll == null) showAll = "0";
	window.open('/nqcontent.cfm?a_name=V3_photo_popup&li_cd='+licd+'&picnum='+picnum + '&mediaTp=' + mediaTp + '&showAll=' + showAll, null,"height=750,width=750,status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes");
}

function openSlideShowPopup(licd) 
{
	window.open('/nqcontent.cfm?a_name=V3_photo_popup&mediaTp=photo&slideshow=true&li_cd='+licd+'&picnum=1', null,"height=770,width=750,status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes");
}

function openVideoShowPopup(licd) 
{
	 window.open('/nqcontent.cfm?a_name=V3_video_popup&li_cd='+licd+'&mediaTp=flashvideo', null,"height=790,width=770,status=no,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes");
}

//popup print window
function v3_Print(what)
{
	window.open('/v3_print.cfm' + what, 'newwindow', 'toolbar=no,status=no,scrollbars=yes,location=yes,menubar=no,directories=no,width=700,height=500');
}

//popup print window
function v3_PrintWithPrintTag(what, printTag)
{
	window.open('/v3_print.cfm' + what + "&printTag=" + printTag, 'newwindow', 'toolbar=no,status=no,scrollbars=yes,location=yes,menubar=no,directories=no,width=700,height=500');
}

function listAppend(list, value, delimiter)
{
	if (delimiter == null)
		delimiter = ",";
		
	if (list == "")
		list = value;
	else
		list = list + delimiter + value;
		
	return list;
}

/*
** Should be used only if one item with the removed value exists in the list
*/
function listRemove(list, value, delimiter)
{
	if (delimiter == null)
		delimiter = ",";
		
	list = listDeleteAt(list, listFind2(list, value, delimiter), delimiter);
	
	return list;
}

function listLen(list, delimiter)
{
	var aList = list.split(delimiter);
	return aList.length;
}

function listGetAt(list, position, delimiter)
{
	var aList = list.split(delimiter);
	var cRetVal = "";
	
	if (aList.length >= position)
		cRetVal = aList[position];

	return cRetVal;
}

/*
** Returns the index of the first occurance of a specific value in a list
** Will return -1 if not found!
*/
function listFind2(list, value, delimiter)
{
	if (delimiter == null)
		delimiter = ",";
		
	var iFound = -1;
	for(var i=0; i<listLen(list,delimiter); i++)
	{
		if (listGetAt(list,i,delimiter) == value)
		{
			iFound = i+1;
			break;
		}
	}
	return iFound;
}

function listDeleteAt(list, position, delimiter)
{
	if (delimiter == null)
		delimiter = ",";
		
	var aList = list.split(delimiter);
	var aNewList = new Array(1);
	
	for (var i=0; i<aList.length; i++)
	{
		if (i+1 != position)
			aNewList.push(aList[i]);
	}
	
	var newList = aNewList.toString(); //Returns the array as a comma-separated string
	newList = newList.replace(/,/, delimiter); //Replace comma with the delimiter
	
	return cleanList(newList, delimiter);
}

function cleanList(list, delimiter)
{
	if (delimiter == null)
		delimiter = ",";
		
	if (list.indexOf(delimiter) == 0)
		return cleanList(list.substr(1), delimiter);
	return list;
}