﻿
function returnObjById(id) { 
    if (document.getElementById) 
        var returnVar = document.getElementById(id); 
    else if (document.all) 
        var returnVar = document.all[id]; 
    else if (document.layers) 
        var returnVar = document.layers[id]; 
    return returnVar; 
}
 
function WriteInnerHTML(divId, strHtml) {
    if (document.getElementById) {
        document.getElementById(divId).innerHTML= strHtml;
	} else if (document.all) {
		document.all[divId].innerHTML = strHtml;
    } else {
        with (document.layers[divId].document) {
            open();
            write(strHtml);
            close();
        }
    }
}

// cross-browser function to get an object's style object given its id
function getStyleObject(objectId) {
    if(document.getElementById && document.getElementById(objectId)) { // W3C DOM	    
	    return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) { // MSIE 4 DOM	    
	    return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) { // NN 4 DOM.. note: this won't find nested layers	    
	    return document.layers[objectId];
    } else {
	    return false;
    }
}

function toggleShowHide(obj) {
    var currentDisplay = (getStyleObject(obj).display == 'none') ? 'block' : 'none';
    getStyleObject(obj).display = currentDisplay;
}
function show(obj) {
    getStyleObject(obj).display = 'block';
}
function hide(obj) {
    getStyleObject(obj).display = 'none';
}

//Cookies
function setCookie(c_name,value,expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}
function getCookie(c_name) {
    if (document.cookie.length>0) {
        c_start=document.cookie.indexOf(c_name + "=")
        if (c_start!=-1) { 
            c_start=c_start + c_name.length+1 
            c_end=document.cookie.indexOf(";",c_start)
            if (c_end==-1) c_end=document.cookie.length
            return unescape(document.cookie.substring(c_start,c_end))
        } 
    }
    return ""
}

function Set_Cookie(name, value, expires, path, domain, secure) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime(today.getTime());

    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
    */
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" + escape(value) + ((expires) ? ";expires=" + expires_date.toGMTString() : "") + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") + ((secure) ? ";secure" : "");
}

// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie(check_name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');


        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if (cookie_name == check_name) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}
// this deletes the cookie when called
function Delete_Cookie(name, path, domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
	

function WebPartMgrBar_OnLoad() {
    //alert(wpmId);
    var wpm = returnObjById(wpmId);
    var obj = returnObjById('wpmToggler');
    var togglestate = Get_Cookie('togglestate');
    if (togglestate==null || togglestate=='' || togglestate=='maximized') {
        wpm.style.height = '420px';
        obj.innerHTML = 'Minimize';
        Set_Cookie('togglestate', 'maximized', 365, '/', '', '');
    } else {
        wpm.style.height = '15px';
        obj.innerHTML = 'Maximize';
        Set_Cookie('togglestate', 'minimized', 365, '/', '', '');
    }
}

function WebPartMgrBar_Toggle(obj) {
    //alert(wpmId);
    var wpm = returnObjById(wpmId);
    if (wpm.style.height == '15px') {
        wpm.style.height = '420px';
        obj.innerHTML = 'Minimize';
        Set_Cookie('togglestate', 'maximized', 365, '/', '', '');
    } else {
        wpm.style.height = '15px';
        obj.innerHTML = 'Maximize';
        Set_Cookie('togglestate', 'minimized', 365, '/', '', '');
    }
}


//Side Nav
function rollOn(imgName) {
	if (document.images) {
    	document[imgName].src = eval(imgName + "_on.src");
	}
}
function rollOff(imgName) {
	if (document.images) {
		document[imgName].src = eval(imgName + "_off.src");
	}
}


function findObj(n, d) {
	var p,i,x;  if(!d) d=opener.document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

// Entry Form Rows
var currRow;
var oldRowClass;
function rowOn(obj) {
    do {
        obj = obj.parentNode;
        //alert(obj.tagName + ' ' + obj.className);
    } while (!(obj.tagName == 'TR' && obj.className.indexOf('entryformrow') != -1));
    //obj = obj.parentNode;
    currRow = obj;
    oldRowClass = obj.className;
    obj.className = "entryformrowOn";
}
function rowOff(obj) {
    //do { obj = obj.parentNode; } while (!(obj.tagName == 'tr' && obj.className.indexOf('entryformrowOn') != -1));
    currRow.className = oldRowClass;
}


var radioTbl;
var selectedRadioRow;
function formatCategories(id) {
    radioTbl = returnObjById(id);
    if ((radioTbl != undefined) && (radioTbl != null)) {
        radioTbl.style.display = 'block';
        var rows = radioTbl.getElementsByTagName("tr");
        for (var i = 0; i < rows.length; i++) {
            rows[i].style.verticalAlign = "top";
            rows[i].onmouseover = function() { this.className = 'entryformcatrowOn' };
            rows[i].onmouseout = function() { if (this != selectedRadioRow) this.className = 'entryformcatrow' };
            var radio = rows[i].getElementsByTagName("input");
            radio[0].onclick = function() {
                selectedRadioRow = this.parentNode.parentNode;
                this.parentNode.parentNode.className = 'entryformcatrowOn';
                var rows = radioTbl.getElementsByTagName("tr");
                for (var j = 0; j < rows.length; j++) {
                    if (selectedRadioRow != rows[j]) rows[j].className = 'entryformcatrow';
                }
            };
        }
    }
}
function showButtons(id) {
    var btns = returnObjById(id);
    if ((btns != undefined) && (btns != null)) {
        btns.style.display = 'block';
    }
}

function GetMaxWords(targetField) {
    return targetField.exMaxWords;
}

function LimitWords(targetField, counterElement, sourceEvent) {
    var isPermittedKeystroke;
    var enteredKeystroke;
    var maximumWordCount;
    var currentWordCount;
    var inputAllowed = true;
    var selectionWordCount = parseInt(CountWordsInSelection(targetField));

    if (GetMaxWords(targetField) != null) {
        // Get the current and maximum field length
        maximumWordCount = parseInt(GetMaxWords(targetField));
        currentWordCount = parseInt(CountWordsInString(targetField.value));

        // Allow non-printing, arrow and delete keys
        enteredKeystroke = window.event ? sourceEvent.keyCode : sourceEvent.which;
        isPermittedKeystroke = ((enteredKeystroke < 32)                                // Non printing
                 || (enteredKeystroke >= 33 && enteredKeystroke <= 40)    // Page Up, Down, Home, End, Arrow
                 || (enteredKeystroke == 46))                            // Delete

        // Decide whether the keystroke is allowed to proceed
        if (!isPermittedKeystroke) {
            if ((currentWordCount - selectionWordCount) >= maximumWordCount) {
                inputAllowed = false;
            }
        }

        // Force a trim of the textarea contents if necessary
        if (currentWordCount > maximumWordCount) {
            //targetField.value = targetField.value.substring(0, maximumFieldLength)
        }
    }

    var remainingWordCount = parseInt(maximumWordCount - currentWordCount);
    WriteInnerHTML(counterElement, remainingWordCount + ' Words Remaining');

    sourceEvent.returnValue = inputAllowed;
    return (inputAllowed);
}

function LimitPasteWords(targetField, counterElement, sourceEvent) {
    var clipboardText;
    var resultantWordCount;
    var maximumWordCount;
    var currentWordCount;
    var pasteAllowed = true;
    var selectionWordCount = parseInt(CountWordsInSelection(targetField));

    if (GetMaxWords(targetField) != null) {
        // Get the current and maximum field length
        maximumWordCount = parseInt(GetMaxWords(targetField));
        currentWordCount = parseInt(CountWordsInString(targetField.value));

        clipboardText = window.clipboardData.getData("Text");
        resultantWordCount = currentWordCount + CountWordsInString(clipboardText) - selectionWordCount;
        if (resultantWordCount > maximumWordCount) {
            pasteAllowed = false;
        }
    }
    
    var remainingWordCount = parseInt(maximumWordCount - currentWordCount);
    WriteInnerHTML(counterElement, remainingWordCount + ' Words Remaining');

    sourceEvent.returnValue = pasteAllowed;
    return (pasteAllowed);
}


function CountWordsInSelection(targetField) {
    if (targetField.selectionStart == undefined) {
        return CountWordsInString(document.selection.createRange().text);
    }
    else {
        return CountWordsInString(targetField.value.substring(targetField.selectionStart, targetField.selectionEnd));
    }
}

function CountWords(txtfield, divId, wordMax) {
    var char_count = txtfield.value.length;
    var fullStr = txtfield.value + " ";
    var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
    var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
    var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
    var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
    var splitString = cleanedStr.split(" ");
    var word_count = splitString.length - 1;
    if (fullStr.length < 2) {
        word_count = 0;
    }
    word_remain = wordMax - word_count;
    WriteInnerHTML(divId, word_remain);
    return word_count;
}
function CountWordsInString(str) {
    var char_count = str.length;
    var fullStr = str + " ";
    var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
    var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
    var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
    var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
    var splitString = cleanedStr.split(" ");
    var word_count = splitString.length - 1;
    if (fullStr.length < 2) {
        word_count = 0;
    }
    return word_count;
}

function showButtons() {
    var btns = returnObjById('actionbuttons');
    if ((btns != undefined) && (btns != null)) {
        btns.style.display = 'block';
    }
}


//######## Open Centered Window #############
function OpenCenteredWindow(url, width, height, windowname)
{
	var x, y = 0;
	if (screen) {
		x = (screen.availWidth - width) / 2;
		y = (screen.availHeight - height) / 2;
	}   
	var options = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=" + width + ",height=" + height + ",screenX=" + x + ",screenY=" + y + ",left=" + x + ",top=" + y;
	if (windowname == null) {
	    windowname = "new";
	}
	msgWindow=window.open(url,windowname,options);		
}

function OpenCenteredWindowOptions(url, width, height, scrollbars, resizable, windowname)
{
	var x, y = 0;
	if (screen) {
		x = (screen.availWidth - width) / 2;
		y = (screen.availHeight - height) / 2;
	}   
	var options = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=" + scrollbars + ",resizable=" + resizable + ",width=" + width + ",height=" + height + ",screenX=" + x + ",screenY=" + y + ",left=" + x + ",top=" + y;
	if (windowname == null) {
	    windowname = "new";
	}
	msgWindow=window.open(url,windowname,options);		
}

