// Peak JavaScript Functions Version 1.0 - 09/16/2007
// g_rootpath, g_PeakToolsPath and g_PeakToolsImagesPath are autogenerated

// Peak_OpenPage - This is used for opening pages in different modes - Tab, Popup or Redirect
// Function: Peak_OpenPage(gridid, page, id, mid, title, height, width, top, left, showscroll, showmenu, resizeable)
// Description: Used for all Peak Framework Popup Pages
// Parameters:  (string) gridid - ID of the calling grid (use null for non-grid parents)
//              (string) page - URL of the page to be displayed
//              (int) id - Item ID passed to the page (use -1 to get Item ID from grid)
//              (int) mid - Module ID passed to the page
//              (string) title - Title for the popup window or Field Name to use for Tab Label (no spaces) 
//              (int) height - Height in pixels of the popup window
//              (int) width - Width in pixels of the popup window
//              (string) mode - The selected mode in which to open the page (optional - default='REDIR')
//                              'WIN' - New Popup Window
//                              'TAB' - New Tab Window
//                              'REDIR' - New Redirect Window  
//              (int) top - Popup window offset from the top of the screen in pixels (optional - default=10)
//              (int) left - Popup window offset from the left of the screen in pixels (optional - default=10)
//              (bool) showscroll - Show scrollbar for the popup window (optional - default=false)
//              (bool) showmenu - Show menu for the popup window (optional - default=false)
//              (bool) resizeable - Allow resizing of popup window (optional - default=false)
function Peak_OpenPage(gridid, page, id, mid, title, img, height, width, mode, top, left, showscroll, showmenu, resizeable)
{
    if (gridid != null && (id == null || id == undefined || id < 0))
    {
        id = PeakGrid_GetKeyValue(gridid);
        if (id==null)
            return;
        title = PeakGrid_GetCellValue(gridid, title);
    }
    //Set the mode based on condition
    mode = (mode==null) ? mode="REDIR" : mode.toUpperCase();
    //Determine the page open type based on the mode
    switch(mode)
    {
        case "TAB":
            var tabid = title.substr(0,3) + "_" + id;
            parent.PeakTab_Add(tabid, title, img, page, id, mid, gridid);
            break;
        case "WINDOW":
        case "WIN":
            //Peak_PopupPage(gridid, page, id, mid, title, height, width, top, left, showscroll, showmenu, resizeable)
            Peak_PopupPage(gridid, page, id, mid, title, height, width, top, left, showscroll, showmenu, resizeable);
            break;
        default:
            Peak_RedirectPage(page, id, mid);
            break;
    }
}

// Function: Peak_PopupPage(gridid, page, id, mid, title, height, width, top, left, showscroll, showmenu, resizeable)
// Description: Used for all Peak Framework Popup Pages
// Parameters:  (string) gridid - ID of the calling grid (use null for non-grid parents)
//              (string) page - URL of the page to be displayed
//              (int) id - Item ID passed to the page
//              (int) mid - Module ID passed to the page
//              (string) title - Title of the popup window (no spaces)
//              (int) height - Height in pixels of the popup window
//              (int) width - Width in pixels of the popup window
//              (int) top - Popup window offset from the top of the screen in pixels (optional - default=10)
//              (int) left - Popup window offset from the left of the screen in pixels (optional - default=10)
//              (bool) showscroll - Show scrollbar for the popup window (optional - default=false)
//              (bool) showmenu - Show menu for the popup window (optional - default=false)
//              (bool) resizeable - Allow resizing of popup window (optional - default=false)
function Peak_PopupPage(gridid, page, id, mid, title, height, width, top, left, showscroll, showmenu, resizeable)
{
    if (gridid != null && (id == null || id == undefined || id < 0))
    {
        id = PeakGrid_GetKeyValue(gridid);
        if (id==null)
            return;
    }

	var url = page + ((page.indexOf("?")>0)?"&":"?") + "itemid=" + id + "&mid=" + mid + "&opentype=win";
    if (gridid != null)
        url += "&exit=window.opener.PeakGrid_Refresh('"+gridid+"');";
    if (!top) top = 10;        
    if (!left) left = 10;        
	var opt = "height=" + height + ",width=" + width + ",top=" + top + ",left=" + left;
	if (showscroll)
		opt += ",scrollbars=yes";
	if (showmenu)
		opt += ",menubar=yes";
	if (resizeable)
		opt += ",resizable=yes";
    title = title.replace(/ /g, "_");  //Replace blanks with _
    title = title.replace(/-/g, "_");  //Replace dashes with _
    title = title.replace(/,/g, "_");  //Replace commas with _
    title = title.replace(/./g, "_");  //Replace period with _
    title = title.replace(/;/g, "_");  //Replace semi colons with _
    title = title.replace(/:/g, "_");  //Replace colons with _
    var newwin = window.open(url, title, opt);
    if (window.focus) 
        newwin.focus();
}

// Function: Peak_RedirectPage(page, id, mid)
// Description: Used for all Peak Framework Redirect Pages
// Parameters:  (string) page - URL of the page to be displayed
//              (int) id - Item ID passed to the page (optional)
//              (int) mid - Module ID passed to the page (optional)
function Peak_RedirectPage(page, id, mid)
{
	if (id==null) id = 0;
	if (mid==null) mid = 0;
	var url = page + ((page.indexOf("?")>0)?"&":"?") + "itemid=" + id + "&mid=" + mid + "&opentype=redir";
	window.parent.location.href = url;
}

// Function: Peak_ContentPage(page, mid)
// Description: Used for viewing Peak Framework Content Pages
// Parameters:  (string) page - URL of the page to be displayed
//              (int) mid - Module ID passed to the page (optional) if not provided, then default to pageid
function Peak_ContentPage(page, mid)
{
//	var page = g_rootpath + "/PeakContent.aspx?url=" + page;
//	var url = page + ((page.indexOf("?")>0)?"&":"?") + "itemid=0&pageid=" + g_PeakPageID + "&opentype=redir";
	var url = page + ((page.indexOf("?")>0)?"&":"?") + ((mid==null) ? "pageid=" + g_PeakPageID : "mid=" + mid);
	window.parent.location.href = url;
}

// Function: Peak_ContentEdit(id, mid)
// Description: Used for editing Peak Framework Content Pages
// Parameters:  (int) id - item id of content
//              (int) mid - Module ID passed to the page
function Peak_ContentEdit(page, mid)
{
	var url = g_rootpath + "/DM/Content/ContentEdit.aspx?url=" + page;
	Peak_PopupPage(null, url, 0, mid, "ContentEdit", 400, 400, 10, 10, true, false, true);
}

function Peak_LoadMenu()
{
    if(!document.body.currentStyle) return; // code for IE
    var subs = document.getElementsByName('daddy');
    for(var i=0; i<subs.length; i++) 
    {
        var li = subs[i].parentNode;
        if(li && li.lastChild.style) 
        {
            li.onmouseover = function() {this.lastChild.style.visibility = 'visible';}
            li.onmouseout = function() {this.lastChild.style.visibility = 'hidden';}
        }
    }
}

// Function: Peak_ValidateName(id, mid)
// Description: Used for validating names and addresses
// Parameters:  (object) frmObj - Form object (TextBox) to be validated
function Peak_ValidateName(frmObj)
{
	var testString = frmObj.value;
	var tmpString;
	var strMsg;
	var errorFlag = false;

	// Regular expression patterns
	var BadChars = /(\w*)([^a-zA-Z_\s]+)(\w*)/gi; //Non-word characters before words
	var RepeatSpace = /\s{2,}/gi; //Two or more blanks
	var LeadSpace = /^\s+/; //Leading blank spaces
	var PeriodCheck = /\w([\.]{1})$/gi; //Check for period after words
//	var DashCheck = /\w[-]{1}\w/i; //Check for a hyphen between words
	var AposCheck = /\w[']{1}\w/i; //Check for an apostrophe between words
	var CommaCheck = /\w([,]{1})$/gi; //Check for comma after words

	//Get rid of leading blanks if they exist
	tmpString = testString.replace(LeadSpace, "");
	//Get rid of extra blanks
	tmpString = tmpString.replace(RepeatSpace, " ");
	frmObj.value = tmpString;
	//Split the name into an array to check each word individually.
	var tmpStringArray = tmpString.split(" ");
	//Loop through the array and match patterns for bad characters
	for (var i=0; i<tmpStringArray.length; i++) 
	{
		var tmpCheck = tmpStringArray[i];
		//Check for non-word characters
		var tmpArray = tmpCheck.match(BadChars)
		if (tmpArray != null)
		{
			//If there are multiple occurences or bad characters within a word, don't let them continue
			if (tmpArray.length > 1) 
				errorFlag = true;
			//Check for hyphenated word
//			else if (tmpCheck.match(DashCheck) != null) 
//				errorFlag = false;
			//Check for apostrophe between words
			else if (tmpCheck.match(AposCheck) != null)
				errorFlag = false;
			//Check for period at the end of a word
			else if (tmpCheck.match(PeriodCheck) != null)
				errorFlag = false;
			//Check for comma at the end of a word
			else if (tmpCheck.match(CommaCheck) != null)
				errorFlag = false;
			//Bad characters
			else 
				errorFlag = true;
		}
        if (errorFlag)
            break;
	}
	//Display message if there is an error and focus back on the form field
	if (errorFlag == true) 
	{
		var splitObj = frmObj.name.split("$");
		var fldName = splitObj[splitObj.length-1];
	    var strMsg = "The text you have entered for '" + fldName + "' contains invalid characters";
		alert(strMsg);
		frmObj.focus();
		return false;
	}
}

// Function: Peak_ChangeCase2(id, mid)
// Description: Used for changing case on characters
// Parameters:  (object) frmObj - Form object (TextBox) to be validated
function Peak_ChangeCase2(frmObj) 
{
	var index;
	var tmpStr;
	var tmpChar;
	var preString;
	var postString;
	var strlen;
	tmpStr = frmObj.value.toLowerCase();
	strLen = tmpStr.length;
	if (strLen > 0)  
	{
		for (index = 0; index < strLen; index++)  
		{
			if (index == 0)  
			{
				tmpChar = tmpStr.substring(0,1).toUpperCase();
				postString = tmpStr.substring(1,strLen);
				tmpStr = tmpChar + postString;
			} 
			else 
			{
				tmpChar = tmpStr.substring(index, index+1);
				if (tmpChar == " " && index < (strLen-1))  
				{
					tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
					preString = tmpStr.substring(0, index+1);
					postString = tmpStr.substring(index+2,strLen);
					tmpStr = preString + tmpChar + postString;
				}
			}
		}
	}
	frmObj.value = tmpStr;

    
    
    var Peak_QueryArray = new Array();
    function Peak_ReadQuesryString() 
    {
        var query = window.location.search.substring(1);
        var parms = query.split('&');
        for (var i=0; i<parms.length; i++) 
        {
            var pos = parms[i].indexOf('=');
            if (pos > 0) 
            {
                var key = parms[i].substring(0,pos);
                var val = parms[i].substring(pos+1);
                qsParm[key] = val;
            }   
        }
    } 

}

function Peak_CheckURL(fld)
{	
	if (fld.value.indexOf("http") != 0)
	    fld.value = "http://" + fld.value;
}

// *** Menu Options
var PeakMenu_CurrentState = [];
function PeakMenu_Collapse(item)
{
    var fld = document.getElementById(item);
    if (fld != null)
        fld.style.display = "none";
    var img = document.getElementById("img_" + item);
    if (img != null)
        img.src = "/images/rightcaret.gif";
    PeakMenu_CurrentState[item] = 0;
}

function PeakMenu_Expand(item)
{
    var fld = document.getElementById(item);
    if(fld != null)
		fld.style.display = "";
    var img = document.getElementById("img_" + item);
    if (img != null)
        img.src = "/images/downcaret.gif";
    PeakMenu_CurrentState[item] = 1;
}

function PeakMenu_ExpColl(item) 
{
    if (PeakMenu_CurrentState[item] == 1)
        PeakMenu_Collapse(item);
    else
        PeakMenu_Expand(item);
}