/*
********************************************************************
* General JavaScript Functions
********************************************************************
File Name: /Scripts/app_GeneralJSFunctions.js
Author: Phil Derksen - VisionOne, Inc.
Completion Date: 
Date of Last Revision: 11/19/2001
Last Revisor: Phil Derksen
Function: General JavaScript functions.
OS: N/A
Requires: Internet Explorer 5.0 or higher browser.
********************************************************************
*/

/*
********************************************************************
* Function: AddKeyword
* Objective: Adds a keyword to the keywords list box.
* Input: objKeyword - The keyword text box object
    objListBox - The list box of the current keywords.
* Output: N/A
********************************************************************
*/
function AddKeyword(objKeyword,objListBox) 
{
    var blnFoundMatch = false;
	var strSearchCriteria = /,/gi;
	var strKeyword;
	
    if(event.keyCode == 13 || event.keyCode == 0) 
    {
		strKeyword = objKeyword.value;
		if(strKeyword.search(strSearchCriteria) != -1)
		{
			alert('You cannot use commas when specifying keywords');
			return;
		}
	
        for (var i = (objListBox.options.length - 1); i >= 0; i--) 
        {
            if(objListBox.options[i].value == objKeyword.value) 
            {
                var blnFoundMatch = true;
            }
        }
        
	
        if(!blnFoundMatch) 
        {
            if(objKeyword.value != '') 
            {
                AddOption(objListBox, objKeyword.value, objKeyword.value);
				objKeyword.value='';
            } 
        } 
        else 
        {
            alert('You can only define a Keyword once');
        }
		
		objKeyword.focus();
    }
}
/*
********************************************************************
* Function: ConfirmLogoff
* Objective: Popup confirm to proceed to logoff current user
* Input: N/A
* Output: N/A
********************************************************************
*/
function ConfirmLogoff() {
if (confirm("Are you sure you want to logoff?")==1) {
	window.location.href = "/Login.asp?Logoff=True";
	}
}
/*
********************************************************************
* Function: RedirectToURL
* Objective: Redirects to another page
* Input: strURL - The URL to redirect to
* Output: N/A
********************************************************************
*/
function RedirectToURL(strURL)
{
	location.href = strURL;
}
/*
********************************************************************
* Function: OpenPopup
* Objective: Opens a centered popup window.
* Input: strURL - The URL to load inside the popup window
* 	intWidth - The width of the popup window (optional)
* 	intHeight - The height of the popup window (optional)
*	blnScrollbars - "no" to disable scrollbars (optional)
*	blnResizable - "yes" to enable resizing (optional)
*	blnToolbar - "yes" to show toolbar (optional)
* Output: N/A
********************************************************************
*/
function OpenPopUp(strURL, intWidth, intHeight, blnScrollbars, blnResizable, blnToolbar)
{
	//Set default parameters (if no values passed in for them)
	if(!intWidth)			
		intWidth=500;
	if(!intHeight)
		intHeight=300;
	if(!blnScrollbars)			
		blnScrollbars="yes";
	if(!blnToolbar)			
		blnToolbar="no";
		
	blnResizable="yes";

	//Test for IE 4+ / Netscape 6+
	if (screen.width)
	{
		var intWidthMax = screen.width;
		var intHeightMax = screen.height;
	}
	//Test for Netscape 4
	else if (window.outerWidth)
	{
		var intWidthMax = window.outerWidth;
		var intHeightMax = window.outerHeight;
	}
	//Use default resolution
	else
	{
		var intWidthMax = 640;
		var intHeightMax = 480;
	}

	var intWidthOffset = (intWidthMax - intWidth)/2;
	var intHeightOffset = (intHeightMax - intHeight)/2;
	
	var strSettings = "width=" + intWidth + ",height=" + intHeight + ",scrollbars=" + blnScrollbars + 
		",resizable=" + blnResizable + ",toolbar=" + blnToolbar + 
		",screenX=" + intWidthOffset + ",screenY=" + intHeightOffset + ",left=" + intWidthOffset +",top=" + intHeightOffset +
		",directories=no,location=no,menubar=no,status=no";
	window.open(strURL, '', strSettings);
}
/*
********************************************************************
* Function: PopulateHiddenForKeywords
* Objective: Populate the hidden tag with the current keywords.
* Input: objListBox - The list box of the current keywords.
    objToHiddenBox - The hidden text box where keywords will go.
* Output: String
********************************************************************
*/
function PopulateHiddenForKeywords(objFromListBox, objToHiddenBox)
{
    var strOutput = '';
    for (var i=0, l=objFromListBox.options.length;i<l;i++) 
    {
        strOutput += objFromListBox.options[i].value;
        if(i != objFromListBox.options.length-1)
        {
            strOutput += ',';
        }
    }
    objToHiddenBox.value = strOutput;
}

/*
********************************************************************
* Function: RemoveKeywords
* Objective: Removes a keyword(s) from the list box.
* Input: objListBox - The list box of the current keywords.
* Output: N/A
********************************************************************
*/
function RemoveKeywords(objListBox) 
{
    var intLastSelected;
    
    for (var i = (objListBox.options.length - 1); i >= 0; i--) {
        if (objListBox.options[i].selected)
		{
            DeleteOption(objListBox, i);
			if (i >= objListBox.options.length)
			{
				intLastSelected = objListBox.options.length - 1;
			}
			else
			{
				intLastSelected = i;	
			}
		}
    }
}
/*
********************************************************************
* Function: SetInitialElements
* Objective: Sets elements values for the keywords, when the window
    is first opened.
* Input: objTextBox - The text box to enter a keyword.
    objListBox - The list box of the current keywords.
    strHiddenField - The hidden field to hold the current keywords.
* Output: String
********************************************************************
*/
function SetInitialElements(objTextBox, objListBox, strHiddenField)
{
    var objValArray;
    objValArray = objTextBox.value.split(',');
    
    if(objTextBox.value != '') 
    {
        for(i=0;i<objValArray.length;i++) 
        { 
            AddOption(objListBox, objValArray[i], objValArray[i]);
        }
    }
    
    strHiddenField.value = objTextBox.value;
}

/*
********************************************************************
* Function: SetOpenerValue
* Objective: Sets a value from a popup back to an opener.
* Input: objPopup - The form object of the opener
* 	objOpener - The form object of the popup
* Output: N/A
********************************************************************
*/
function SetOpenerValue(objPopup, objOpener)
{
	for(i=0; i<objPopup.length; i++) {
		if(objPopup[i].checked) {
			objOpener.value = objPopup[i].value
		}
	}
}
/*
********************************************************************
* Function: SetOptionClicked
* Objective: Sets a radio option checked.
* Input: objElement - The element of the radio button
* 	intIndex - The index to select of the element
* Output: N/A
********************************************************************
*/
function SetOptionClicked(objElement, intIndex)
{
	objElement[intIndex-1].checked = true;
}
/*
********************************************************************
* Function: SetVisibility
* Objective: Dynamically sets an element hidden or visible.
* Input: varElementID - the ID of the element being accessed
*	blnVisible - If true, shows element. If false, hides element.
* Output: N/A
********************************************************************
*/
function SetVisibility(varElementID, blnVisible)
{
	document.getElementById(varElementID).style.display = (blnVisible ? 'block' : 'none');
}

/*
********************************************************************
* Function: FieldIncrementer
* Objective: Incremements a specific field by 1.
* Input: objElement - The object of the element to incrememnt
* Output: N/A
********************************************************************
*/
function FieldIncrementer(objElement)
{
	var intCount = Number(objElement.value);
	intCount++;
	objElement.value = intCount;
}
/*
********************************************************************
* Function: FieldDecrementer
* Objective: Decrements a specific field by 1.
* Input: objElement - The object of the element to decrement
* Output: N/A
********************************************************************
*/
function FieldDecrement(objElement)
{
	var intCount = Number(objElement.value);
	intCount--;
	objElement.value = intCount;
}
/*
********************************************************************
* Function: SubmitGenericForm
* Objective: Submits a form.
* Input: objForm - The object of the form to submit
* Output: N/A
********************************************************************
*/
function SubmitGenericForm(objForm)
{
	objForm.submit();
}
/*
********************************************************************
* Begin JavaScriptListBoxes Functions
********************************************************************

********************************************************************
* Function: AddOption
* Objective: Adds option to list box
* Input: objListBox - list box to add option to
	strText - text to display for option (between OPTION tags)
	varValue - value to save to VALUE attribute for option
* Output: N/A
********************************************************************
*/
function AddOption(objListBox, strText, varValue)
{
    var blnDefaultSelected = true;
    var blnSelected = true;
    var objOption = new Option(strText, varValue, blnDefaultSelected, blnSelected);
	
    objListBox.options[objListBox.length] = objOption;
	objListBox.selectedIndex = -1;
}
/*
********************************************************************
* Function: DeleteOption
* Objective: Deletes option in list box
* Input: objListBox - list box to delete option from
	intIndex - index of option to delete
* Output: N/A
********************************************************************
*/
function DeleteOption(objListBox, intIndex)
{
    objListBox.options[intIndex] = null;
}
/*
********************************************************************
* Function: MoveSelected
* Objective: Moves selected options from one list box to another
* Input: objFromListBox - list box to move option from
	objToListBox - list box to move option to
* Output: N/A
********************************************************************
*/
function MoveSelected(objFromListBox, objToListBox)
{
	var intLastSelected;
	
    for (var i = 0; i <= (objFromListBox.options.length - 1); i++) {
        if (objFromListBox.options[i].selected)
		{
            AddOption(objToListBox, objFromListBox.options[i].text, objFromListBox.options[i].value);
		}
    }
    for (var i = (objFromListBox.options.length - 1); i >= 0; i--) {
        if (objFromListBox.options[i].selected)
		{
            DeleteOption(objFromListBox, i);
			if (i >= objFromListBox.options.length)
			{
				intLastSelected = objFromListBox.options.length - 1;
			}
			else
			{
				intLastSelected = i;	
			}
		}
    }
	//Select next option
	objFromListBox.selectedIndex = intLastSelected;
}
/*
********************************************************************
* Function: PopulateHidden
* Objective: Populates hidden text box with comma-separated list of values
* Input: objFromListBox - list box to copy option values from
	objToHiddenBox - hidden text box to copy options to
* Output: N/A
********************************************************************
*/
function PopulateHidden(objFromListBox, objToHiddenBox)
{
    var strOutput = '';
    for (var i=0, l=objFromListBox.options.length;i<l;i++) {
            strOutput += escape(objFromListBox.options[i].value) + ',';
    }
    objToHiddenBox.value = strOutput;
}

/*
********************************************************************
* Function: PreviewFileToUpload
* Objective: For /Catalog/ResourceManage.asp.
	Shows preview of image in File to Upload browse box
	(image residing on client's machine).
* Input: N/A
* Output: N/A
********************************************************************
*/
function PreviewFileToUpload()
{
	var frm = document.frmMain;
	
	if ((frm.IsImage[0].checked) && ((frm.FileToUpload.value.length > 0)))
	{
		OpenPopUp(frm.FileToUpload.value, 640, 480, 'yes', 'yes', 'no');
	}
	else
	{
		alert('To preview file before uploading, File to Upload cannot be blank, and File Type must be Image.');
	}
}
/*
********************************************************************
* Function: DatePickerPopup
* Objective: Shows DatePicker.asp in popop window to select date.
* Input: strFormName, strTextBoxName
* Output: N/A
********************************************************************
*/
function DatePickerPopup(strFormName, strTextBoxName)
{
	var winDatePicker = window.open("/DatePicker.asp?FormName=" + strFormName + "&TextBoxName=" + strTextBoxName, "DatePickerWindow", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=185,height=165,left=375,top=200");
	winDatePicker.focus();
	//Name return window for reference
	self.name="ReturnWindow";
}

/*
********************************************************************
* Function: FrameBuster
* Objective: If in a frame, breaks out of the frame.
* Input: N/A
* Output: N/A
********************************************************************
*/
function FrameBuster()
{
	if (parent.frames.length > 0) {
		parent.location.href = self.document.location
	}
}

/*
********************************************************************
* Function: DeletePressRelease
* Objective: Prompts to delete a press release.  If OK is clicked,
*	it does so.
* Input: intPageID - PageID to delete
* Output: N/A
********************************************************************
*/
function DeletePressRelease(intPageID)
{
	var strRequestWarning = "You have requested to delete a Press Release. Click OK to proceed.";
	var blnConfirmDel;
	
	blnConfirmDel = confirm(strRequestWarning);
	if(blnConfirmDel)
	{
		document.forms[0].DelPageID.value = intPageID;
		document.forms[0].submit();
	}
}

/*
********************************************************************
* Function: AC_AddExtension
* Objective: Works with the extension.
* Input: src - The source string.
*	ext - The extension.
* Output: String
********************************************************************
*/
function AC_AddExtension(src, ext)
{
  if (src.indexOf('?') != -1)
    return src.replace(/\?/, ext+'?'); 
  else
    return src + ext;
}

/*
********************************************************************
* Function: AC_Generateobj
* Objective: Generates the neccesary object and embed tags.
* Input: objAttrs - The object attributes.
*	params - The object parameters.
*	embedAttrs - The embedded attributes.
* Output: N/A
********************************************************************
*/
function AC_Generateobj(objAttrs, params, embedAttrs) 
{ 
  var str = '<object ';
  for (var i in objAttrs)
    str += i + '="' + objAttrs[i] + '" ';
  str += '>';
  for (var i in params)
    str += '<param name="' + i + '" value="' + params[i] + '" /> ';
  str += '<embed ';
  for (var i in embedAttrs)
    str += i + '="' + embedAttrs[i] + '" ';
  str += ' ></embed></object>';

  document.write(str);
}

/*
********************************************************************
* Function: AC_FL_RunContent
* Objective: Runs the content for SWF flash movies.
* Input: N/A
* Output: N/A
********************************************************************
*/
function AC_FL_RunContent(){
  var ret = 
    AC_GetArgs
    (  arguments, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
     , "application/x-shockwave-flash"
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

/*
********************************************************************
* Function: AC_SW_RunContent
* Objective: Runs the content for DCR files.
* Input: N/A
* Output: N/A
********************************************************************
*/
function AC_SW_RunContent(){
  var ret = 
    AC_GetArgs
    (  arguments, ".dcr", "src", "clsid:166B1BCA-3F9C-11CF-8075-444553540000"
     , null
    );
  AC_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

/*
********************************************************************
* Function: AC_GetArgs
* Objective: Gets arguements from array.
* Input: args
*	ext - The extension.
*	srcParamName - The source parameter name.
*	classid - The specific classid to use.
*	mimeType - The mime type of the file.
* Output: Object
********************************************************************
*/
function AC_GetArgs(args, ext, srcParamName, classid, mimeType){
  var ret = new Object();
  ret.embedAttrs = new Object();
  ret.params = new Object();
  ret.objAttrs = new Object();
  for (var i=0; i < args.length; i=i+2){
    var currArg = args[i].toLowerCase();    

    switch (currArg){	
      case "classid":
        break;
      case "pluginspage":
        ret.embedAttrs[args[i]] = args[i+1];
        break;
      case "src":
      case "movie":	
        args[i+1] = AC_AddExtension(args[i+1], ext);
        ret.embedAttrs["src"] = args[i+1];
        ret.params[srcParamName] = args[i+1];
        break;
      case "onafterupdate":
      case "onbeforeupdate":
      case "onblur":
      case "oncellchange":
      case "onclick":
      case "ondblClick":
      case "ondrag":
      case "ondragend":
      case "ondragenter":
      case "ondragleave":
      case "ondragover":
      case "ondrop":
      case "onfinish":
      case "onfocus":
      case "onhelp":
      case "onmousedown":
      case "onmouseup":
      case "onmouseover":
      case "onmousemove":
      case "onmouseout":
      case "onkeypress":
      case "onkeydown":
      case "onkeyup":
      case "onload":
      case "onlosecapture":
      case "onpropertychange":
      case "onreadystatechange":
      case "onrowsdelete":
      case "onrowenter":
      case "onrowexit":
      case "onrowsinserted":
      case "onstart":
      case "onscroll":
      case "onbeforeeditfocus":
      case "onactivate":
      case "onbeforedeactivate":
      case "ondeactivate":
      case "type":
      case "codebase":
        ret.objAttrs[args[i]] = args[i+1];
        break;
      case "width":
      case "height":
      case "align":
      case "vspace": 
      case "hspace":
      case "class":
      case "title":
      case "accesskey":
      case "name":
      case "id":
      case "tabindex":
        ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
        break;
      default:
        ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
    }
  }
  ret.objAttrs["classid"] = classid;
  if (mimeType) ret.embedAttrs["type"] = mimeType;
  return ret;
}
/*
********************************************************************
* Function: SwapPromotionalImage
* Objective: Swaps an image for the promotional image mouseover/off.
* Input: 
* Output: N/A
********************************************************************
*/
function SwapPromotionalImage(objImage, targetImage)
{
	objImage.src = targetImage;
}