/*
THIS LICENSE HEADER MUST REMAIN INTACT FOR LEGAL USE!
Pagefusion; Pagefusion Core Platform
Description: A powerful, easy-to-use, and highly customizable content management system.
Copyright (C), 2010 Pagefusion.com, All Rights Reserved.
Author: Chris Paul, chris.paul@pagefusion.com
Support portal: www.pagefusion.com

Filename: /libraries/core/javascript/main.js
Description: handles miscellaneous javascript stuff

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

var ie = document.all;
var nn = document.getElementById && !document.all;

function rand(min, max) {
    // example: rand(1, 1);
    // returns 1
    var argc = arguments.length;
    if (argc == 0) {
        min = 0;
        max = 2147483647;
    } else if (argc == 1) {
        throw new Error('Warning: rand() expects exactly 2 parameters (only 1 given)!');
    } // if
    return Math.floor(Math.random() * (max - min + 1)) + min;
} // function

function setStyle(objId, styleName, styleVal) {
    if (document.getElementById(objId)) {
		document.getElementById(objId).style[styleName]=styleVal;
	} // if
} // function

function setClass(objId, classNameVal) {
	if (document.getElementById(objId)) {
    	document.getElementById(objId).className=classNameVal;
	} // if
} // function

function openWin(url, width, height, winID) {
	if (!winID || winID == "undefined") {
		var winID = rand();
	} // if
	
	if (!width) {
		width = 900;
	} // if
	
	if (!height) {
		height = 600;
	} // if
	
	top.open(url, winID, 'width='+width+',height='+height+',scrollbars=1,resizable=1');
} // function

function stripLeading(string,chr) {
   var finished = false;
   for (var i = 0; i < string.length && !finished; i++)
       if (string.substring(i,i+1) != chr) finished = true;
   if (finished) return string.substring(i-1); else return string;
} // function

function stripTrailing(string,chr) {
   var finished = false;
   for (var i = string.length - 1; i > -1 && !finished; i--)
       if (string.substring(i,i+1) != chr) finished = true;
   if (finished) return string.substring(0,i+2); else return string;
} // function

function trim(string,chr) {
    return stripTrailing(stripLeading(string,chr),chr)
} // function

// open browser window
function pfmlWindow(id, url, title, width, height, scrollbars, resizable) {
	if (!id || id == "undefined") { var id = rand(); } // if
	if (!width) { var width = 800; } // if
	if (!height) { var height = 600; } // if
	
	// calculate middle of screen
	var leftVal = (width-screen.width)/2;
	var topVal = (height-screen.height)/2;
	
	top.open(url, id, 'width='+width+',height='+height+',left='+leftVal+',top='+topVal+',scrollbars='+scrollbars+',resizable='+resizable+'');
} // function

// open DHTML dialog window
function pfmlDialog(url, title, icon, closeIcon, width, height, scrolling) {
	
	var skinDir = get_cookie("skinDir");

	var tmpUrl="/index.php?";

	if (url.indexOf("PID") > -1) {
		url = tmpUrl + url;
	} else if (url.indexOf("PGID") > -1) {
		url = tmpUrl+"PID="+tPID+"&"+url;
	} else { 
		url = tmpUrl+"PID="+tPID+"&PGID="+tPGID+"&"+url;
	} // if
	
	if (skinDir == "undefined") { skinDir = "default"; } // if
	if (!title) { title = "New Dialog";	} // if
	if (!icon) { icon = "/components/dialog/images/dialog.png";	} // if
	if (!closeIcon) { closeIcon = "/skins/"+skinDir+"/images/close.png"; } // if
	//if (!width) { width = 0; } // if
	//if (!height) { height = 0; } // if
	if (scrolling == "undefined") { scrolling = 0; } // if
	
	modalDialogURL(1, dialog, 'dialog_iframe', skinDir, title, icon, closeIcon, url, width, height, scrolling);
} // function

// load javascript/css file dynamically within *.js file
function loadFile(filename, filetype) {
	if (filetype == "js") {
		var fileref = document.createElement('script');
		fileref.setAttribute("type","text/javascript");
		fileref.setAttribute("src", filename);

	} else if (filetype == "css") {
		var fileref = document.createElement("link");
		fileref.setAttribute("rel", "stylesheet");
		fileref.setAttribute("type", "text/css");
		fileref.setAttribute("href", filename);
	} // if
	
	if (typeof fileref != "undefined") {
		document.getElementsByTagName("head")[0].appendChild(fileref);
	} // if
} // function

// remove javascript/css file dynamically within *.js file
function removeFile(filename, filetype) {
	var targetelement=(filetype == "js")? "script" : (filetype=="css")? "link" : "none" // determine element type to create nodelist from
	var targetattr=(filetype == "js")? "src" : (filetype=="css")? "href" : "none" // determine corresponding attribute to test for
	var allsuspects=document.getElementsByTagName(targetelement)
	for (var i=allsuspects.length; i>=0; i--) { // search backwards within nodelist for matching elements to remove
		if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1)
		allsuspects[i].parentNode.removeChild(allsuspects[i]) // remove element by calling parentNode.removeChild()
	} // for
} // function


// select all rows within grid
function select_all(form, fieldname, len) {
	var strlen = fieldname.length;

	with (document.forms[form]) {
		for (i=0; i<elements.length; i++) {
			thiselm = elements[i];
			if (thiselm.name.substring(0,strlen) == fieldname || thiselm.name.substring(0,len) == fieldname) {
				if (thiselm.disabled == false) {
					thiselm.checked = !thiselm.checked;
				} // if
			} // if
		} // for
	} // with
} // function

// calculate height and width of specified panels
function pf_equalheight_set() {
 var i, objHeight, setHeight=0, numPixels, objCont=document.pf_equalheight, adjustment=document.adjustment;
	
	if (objCont && objCont.length) {
		for (i=1; i<objCont.length; i+=2) {
			objCont[i+1].style.paddingBottom = ""; // set padding for containers to NULL
		} // for
		
		// set height of both columns to second panel height
		if (objCont[3]) {
			setHeight = objCont[3].offsetHeight;
		
			// gets difference of height between containers and adds the necessary pixels
			for(i=1; i<objCont.length; i+=2) {
				objHeight = objCont[i].offsetHeight;
				if (objHeight < setHeight) {
					numPixels = setHeight-objHeight+adjustment;
					objCont[i+1].style.paddingBottom = numPixels + "px";
				} // if
				
				if (objHeight > setHeight) {
					numPixels = setHeight-objHeight+adjustment;
					objCont[i+1].style.paddingBottom = numPixels + "px";
				} // if
			} // for
		} // if
	} // if
} // function

// set height of lower container equal to that of higher container
function pf_equalheight_set2max() {
 var i, maxHeight, objHeight, setHeightTo=0, numPixels, objCont=document.pf_equalheight, adjustment=document.adjustment;
	
	if (objCont && objCont.length) {
		for (i=1; i<objCont.length; i+=2) {
			objCont[i+1].style.paddingBottom = ""; // set padding for containers to NULL
		} // for
		
		// set height of both columns to largest height
		for (i=1; i<objCont.length; i+=2) {
			objHeight = objCont[i].offsetHeight;
			if (objHeight > maxHeight) {
				maxHeight = objHeight;
			} // if
		} // for

		// gets difference of height between containers and adds the necessary pixels
		for(i=1; i<objCont.length; i+=2) {
			objHeight = objCont[i].offsetHeight;
			if (objHeight < setHeightTo) {
				numPixels = setHeightTo-objHeight;
				objCont[i+1].style.paddingBottom = numPixels + "px";
			} // if
		} // for
	} // if
} // function

// set ids for specified containers
function pf_equalheight() { // pf_equalheight("panels", "adjustment", id1, tag1, id2, tag2, id3, tag3);
	var container, specified_tag, current_element, adjustment;
	if (document.getElementById) {
		document.pf_equalheight = new Array();
		document.pf_equalheight[0] = arguments[0]; // passed arguments
		document.adjustment = arguments[1];

		for (i=2; i<arguments.length; i+=2) { // loop through arguments (element names specified): 
			current_element=null;
			container=document.getElementById(arguments[i]); // id of specified container
			if (container) {
				specified_tag = container.getElementsByTagName(arguments[i+1]); // specified tag (located within container)
				if (specified_tag) {
					current_element = specified_tag[specified_tag.length-1];
					if (!current_element.id) {
						current_element.id = "pf_eq" + i; // set id for each element
					} // if
				} // if
			} // if
			
			if (container && current_element) {
				document.pf_equalheight[document.pf_equalheight.length]=container; // last tag of specified container
				document.pf_equalheight[document.pf_equalheight.length]=current_element; // last tag within specified container
			} // if
		} // for
		
		if (arguments[0] == "panels") {
			setInterval("pf_equalheight_set2max()", 10);
		} else {
			setInterval("pf_equalheight_set()", 10);
		} // if
	} // if
} // function

// get elements by classname
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
} // function

// convert curly quotes and other characters
function convert(s) {
    s = s.replace( /\u2018/g, "'" );
    s = s.replace( /\u2019/g, "'" );
    s = s.replace( /\u201c/g, '"' );
    s = s.replace( /\u201d/g, '"' );
    s = s.replace( /\u2013/g, '-' );
    s = s.replace( /\u2014/g, '--' );
    return s;
} // function

function close_window(url) {
	if (window.top.dialog) {
		window.stop();
		window.top.dialog.hide();
		if (url) {
			window.top.location=url;
		} // if
	} else {
		window.stop();
		window.close();
		if (url) {
			window.parent.location=url;
		} // if
	} // if
} // function

function createCheckboxArrays(f) {
	for (var i=0;i<f.elements.length;i++) {
		var el=f.elements[i];
		if (el.type=="checkbox") {
			if (el.name.substring(el.name.length-2)=="[]") {
				var n=el.name.substring(0,el.name.length-2);
				if (!f.checkboxes) {f.checkboxes=new Object();}
				if (!f.checkboxes[n]) {f.checkboxes[n]=new Array();}
				f.checkboxes[n].length++;
				f.checkboxes[n][f.checkboxes[n].length-1]=el;
			} // if
		} // if
	} // for
} // if

function pfmlScrollToBottom() {
	dh=document.body.scrollHeight;
	ch=document.body.clientHeight;
	if (dh > ch) {
		moveme=dh-ch;
		window.scrollTo(0,moveme)
	} // if
} // function
