//---------- [one.ngn] Content Managent System --------------
//----------    created by zerophobic.com      --------------
//-----------------------------------------------------------
// MODULE	:	misc javascript
// FUNCTION	:	tools and snippets
// AUTHOR	:	brett stanley <brett@zerophobic.com>
// CREATED	:	01/10/2001
// MODIFIED	:	11/01/2002
//-----------------------------------------------------------
//-----------------------------------------------------------

//-------------------------------------------------------------------------
// check the browser type
//-------------------------------------------------------------------------
var menuReadyState=false; 
var exclude=true; 
var agt=navigator.userAgent.toLowerCase();
var ie=false; 
var ie4=false; 
var ie5=false; 
var op5=false; 
var op7=false; 
var konqi=false;


//-------------------------------------------------------------------------
//window openers
//-------------------------------------------------------------------------
function openWindow(file){
	window.open(file, 'image', config='width=150, height=150');
}

// all your window popping needs
function winPop(file,name,width,height){
	win=window.open(file, name, "width="+width+", height="+height+",status=no,scrollbars=no,scrolling=no,toolbar=no,menubar=no,location=no,resizable=yes");
	win.resizeTo(width,height);
	win.moveTo((screen.availWidth/2)-(width/2),(screen.availHeight/2)-(height/2));
	win.focus();
	return(win);
}

//-------------------------------------------------------------------------
// Select box jump
//-------------------------------------------------------------------------
function selectJump(selectObj){
	for (i = 0; i < selectObj.options.length; i++){
		if (selectObj.options[i].selected == true){
			var thisOption = selectObj.options[i].value;
		}
	}
	window.location=thisOption;
}

//-------------------------------------------------------------------------
// Article select scripts
//-------------------------------------------------------------------------
function open_select_window(ids,field){
	// opens the right window
	var width = 350;
	var height = 400;
	//thisWin = winPop('dsp_article_select.php?field='+field+'&selected='+ids,'link',300,300);
	win=window.open('dsp_article_select.php?field='+field+'&selected='+ids, 'link', "width="+width+", height="+height+",status=no,scrollbars=yes,scrolling=yes,toolbar=no,menubar=no,location=no,resizable=yes");
	win.resizeTo(width,height);
	win.moveTo((screen.availWidth/2)-(width/2),(screen.availHeight/2)-(height/2));
	win.focus();
}

function article_select(articles,field){
	// this displays the articles in the Select Box
	//alert(articles);
	var article_ids = new Array();
	var thisFieldList = eval("document.this_form."+field+"_list");
	var thisField = eval("document.this_form."+field);
	var n=0;
	if (articles != ""){
		for (i = 0; i < articles.length; i++){
			var article = new Option(articles[i][1],articles[i][0]);
			thisFieldList.options.length = n;
			thisFieldList.options[n] = article;
			article_ids[n] = articles[i][0]+";"+articles[i][1];
			n++;
	  	}
	}else{
		thisFieldList.options.length = 1;
		thisFieldList.options[0].text = "---------No Linked Articles---------";
	}
	thisField.value = article_ids;
	//alert(articles);
}


//-------------------------------------------------------------------------
//Disable right click
//-------------------------------------------------------------------------
function right(e) {
  	var msg = "   [ONE.NGN]\n\nContent Management\n\n ©2002 Zerophobic";

	if (navigator.appName == 'Netscape' && e.which == 3) {
	 	alert(msg);
	 	return false;
	}
	
	if (navigator.appName == 'Microsoft Internet Explorer' && event.button==2) {
		alert(msg);
	 	return false;
	}
  	else return true;
}

function trap() {
	// trap right click on the page
	if(document){ 
	  	document.onmousedown = right;
	   	document.onmouseup = right;
	}
	// trap right click on any images
	if(document.images){ 
	  	for(i=0;i<document.images.length;i++) {
	   		document.images[i].onmousedown = right;
	   		document.images[i].onmouseup = right;
	  	}
	}
}

//trap();


//-------------------------------------------------------------------------
// Printer Friendly version
//-------------------------------------------------------------------------
function ShowPrintPage() {
	if ((document.all)&&(window.parent.name != 'print')) {
		isIE = true;
	    var innerContent = document.all.content.innerHTML;
		objWindow = winPop('','print',650,600);
		objWindow.document.open();
		objWindow.document.write('<html>');
		objWindow.document.write('<head>');
		objWindow.document.write('<title>'+document.title+'</title>\n');
		objWindow.document.write('<link href="lib/styles/style_print.css" rel="stylesheet" type="text/css">');
		objWindow.document.write('</head>');
		objWindow.document.write('<body bgcolor="#FFFFFF" text="#000000" leftmargin="5" topmargin="5" marginwidth="5" marginheight="5">');
		objWindow.document.write('<a href="#" onclick="window.print();">Print this page</a>&nbsp;|&nbsp;<a href="#" onclick="window.close();">Close this window</a><p>');
		objWindow.document.write(innerContent);
		objWindow.document.write('<p>Printed from <a href="'+document.location+'">'+document.location+'</a>');
		objWindow.document.write('</body>');
		objWindow.document.write('</html>');	
		objWindow.document.close();
	} else if(document.layers) {
		window.print();
	} else {
		window.print();
		window.close('print');
	}
}

//-------------------------------------------------------------------------
//html editor
//-------------------------------------------------------------------------
function bold(field) 
{
var dark=prompt('Bold text','Text');
if (dark)
	{
		field.value+=('<b>'+dark+'</b>');
	}
}

function highlight(field) 
{
var dark=prompt('highlight text','Text');
if (dark)
	{
		field.value+=('<h>'+dark+'</h>');
	}
}

function hrrule(field)
{
field.value+=('<hr>')
}

function putemotion(field,emo)
{
	field.value+=(emo);
}

function quote(field) 
{
var quotet=prompt('Quote Text','Text');
if (quotet)
	{
		field.value+=('<blockquote>'+quotet+'</blockquote>');
	}
}

function italic(field) 
{
var ital=prompt('Italicized Text','Text');
if (ital)
	{
		field.value+=('<i>'+ital+'</i>');
	}
}

function underlined(field)
{
var underline=prompt('Underlined Text','Text');
if (underline)
	{
		field.value+=('<u>'+underline+'</u>');
	}
}

function link(field) 
{
var hyper=prompt('URL for the link','http://');
var hypertwo=prompt('Text for the link','Text');
var hyperthree=prompt('Window for the link (leave empty for current window)','_blank');
if (hyper) 
	{
		field.value+=('<a href=\"'+hyper+'\" target=\"'+hyperthree+'\">'+hypertwo+'</a>');	
	}
}

function linkAdvanced(field){
	if (typeof(zp_linkwin) == "undefined" || zp_linkwin.closed) { //short circuit eval
		//spawn image window
		var szURL;
		
		szURL=inc+'hyperlink'+scriptExt+'?mode=simple&field='+field;
		zp_linkwin = winPop(szURL ,"zp_linkwin", 400, 300);
	}
}

function ftplink(field) 
{
var hyper=prompt('URL for the link','ftp://');
var hypertwo=prompt('Text for the link','Text');
var hyperthree=prompt('Window for the link (leave empty for current window)','_blank');
if (hyper)
	{
		field.value+=('<a href=\"'+hyper+'\" target=\"'+hyperthree+'\">'+hypertwo+'</a>');
	}
}

function securelink(field) 
{
var hyper=prompt('URL for the link','https://');
var hypertwo=prompt('Text for the link','Text');
var hyperthree=prompt('Window for the link (leave empty for current window)','_blank');
if (hyper)
	{
		field.value+=('<a href=\"'+hyper+'\" target=\"'+hyperthree+'\">'+hypertwo+'</a>');
	}
}

function emaillink(field) 
{
var emailit=prompt('Your E-Mail Address','nobody@nowhere.com');
var emailittwo=prompt('Text for the link','Text');
if (emailit)
	{
		field.value+=('<a href=\"mailto:'+emailit+'\">'+emailittwo+'</a>');
	}
}

function image(field)
{
var img=prompt('URL for the image','');
if (img)
	{
		field.value+=('<img src=\"'+img+'\">');
	}
}

function imageUpload(field){
	if (typeof(zp_filewin) == "undefined" || zp_filewin.closed) { //short circuit eval
		//spawn image window
		var szURL;
		
		szURL=inc+'upload_image'+scriptExt+'?mode=simple&field='+field;
		zp_filewin = winPop(szURL ,"zp_imgwin", 400, 300);
	}
}

function fileUpload(field){
	if (typeof(zp_filewin) == "undefined" || zp_filewin.closed) { //short circuit eval
		//spawn image window
		var szURL;
		
		szURL=inc+'upload_file'+scriptExt+'?mode=simple&field='+field;
		zp_filewin = winPop(szURL ,"zp_filewin", 300, 120);
	}
}

function orderedl(field)
{
field.value+=('<ol></ol>')
}

function unorderedl(field)
{
field.value+=('<ul></ul>')
}

function newlistentry(field)
{
var newentry=prompt('Text for new entry','Text')
if (newentry)
	{
		field.value+=('<li>'+newentry+'\r\n')
	}
}



//-------------------------------------------------------------------------
//all the stuff for the resizer
//-------------------------------------------------------------------------
var d = document;
var isNav = false, isNS6 = false, isIE = false;

if(d.layers) {
	isNav = true;
	var range = "";
	var styleObj = "";
	} 
else if (d.all) {
	isIE = true;
	range = "all.";
	styleObj = ".style";
	} 
else if(d.getElementById) {
	isNS6 = true;
	var range = "getElementById('";
	var styleObj = "').style";
}

//do the resize	
function erection(iw,ih) {
	if(isIE) {
		var cx = parseInt(top.document.body.clientWidth);
		var cy = parseInt(top.document.body.clientHeight);
	}
	else {
		var cx = (window.innerWidth);
		var cy = (window.innerHeight);	
	}
	var speed = 2;
	
	//resize width
	if (cx < iw-10) {
		resizeBy(((iw-cx)/speed),0);
	}
	else if(cx > iw+10) {
		resizeBy(((iw-cx)/speed),0);
	}
	else {
		resizeBy(iw-cx,0);
	}
	
	//resize height
	if (cy < ih-10) {
		resizeBy(0,((ih-cy)/speed));
	}
	else if (cy > ih+10) {
		resizeBy(0,((ih-cy)/speed));
	}
	else {
		resizeBy(0,ih-cy);
	}
	
	if((cx != iw) || (cy != ih)) {
		setTimeout('erection('+iw+','+ih+')','20');
	}
	else {
		if(isIE) {
			if(document.all)for(var i in document.links)document.links[i].onfocus=document.links[i].blur;
			hide('loadinglayer');
		}
	}
}
		
function getObject(obj) {
	var theObj = eval("document." + range + obj + styleObj);
	return theObj;
}
		

function hide(layername) {
	if(isIE)
		eval("d.all['"+layername+"'].style.visibility = 'hidden'");
	else if(isNS6) {
		eval("d.getElementById('"+layername+"').style.visibility = 'hidden'");
		}
	else {
		eval("d."+layername+".visibility = 'hide'");
		}
}

function show(layername) {
	if(isIE)
		eval("d.all['"+layername+"'].style.visibility = 'visible'");
	else if(isNS6) {
		eval("d.getElementById('"+layername+"').style.visibility = 'visible'");
		}
	else {
		eval("d."+layername+".visibility = 'show'");
	}
}



//-------------------------------------------------------------------------
// table Mouseover code
//-------------------------------------------------------------------------
/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object   the table row
 * @param   string   the action calling this script (over, out or click)
 * @param   string   the default background color
 * @param   string   the color to use for mouseover
 * @param   string   the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor = theMarkColor;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()) {
        if (theAction == 'out') {
            newColor = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor = theMarkColor;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor = (thePointerColor != '')
                     ? thePointerColor
                     : theDefaultColor;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function




//-------------------------------------------------------------------------
// Date Widget Code
//-------------------------------------------------------------------------

// build the date widget
var month_array = [
                    { name:'January', days:31 },
                    { name:'February', days:28 },
                    { name:'March', days:31 },
                    { name:'April', days:30 },
                    { name:'May', days:31 },
                    { name:'June', days:30 },
                    { name:'July', days:31 },
                    { name:'August', days:31 },
                    { name:'September', days:30 },
                    { name:'October', days:31 },
                    { name:'November', days:30 },
                    { name:'December', days:31 }
                  ];
					  
function setLeapYear(year) {

    var isLeapYear = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);

    month_array[1].days = (isLeapYear) ? 29 : 28;

    return isLeapYear;
}

// display function for the date
function date_widget(day_field, month_field, year_field, hour_field, minute_field, hidden_field, default_date) {
	
	// for reference
    if (default_date == null)
        default_date = new Date();

    var output;

    setLeapYear(default_date.getFullYear());

    // do days of the month
    output = '<select name="' + day_field + '" onChange="return doDateChange(this.form.elements[\'' + day_field + '\'], this.form.elements[\'' + month_field + '\'], this.form.elements[\'' + year_field + '\'], this.form.elements[\'' + hour_field + '\'], this.form.elements[\'' + minute_field + '\'], this.form.elements[\'' + hidden_field + '\']);">';
    for (var day = 1 ; day <= month_array[default_date.getMonth()].days ; day++) {
        output += '<option value="' + day + '"' + (day == default_date.getDate() ? ' selected>' : '>') + day + '</option>';
    }
    output += '</select>';

    // do months
    output += '<select name="' + month_field + '" onChange="return doDateChange(this.form.elements[\'' + day_field + '\'], this.form.elements[\'' + month_field + '\'], this.form.elements[\'' + year_field + '\'], this.form.elements[\'' + hour_field + '\'], this.form.elements[\'' + minute_field + '\'], this.form.elements[\'' + hidden_field + '\']);">';
    for (var month = 0 ; month < month_array.length ; month++) {
        output += '<option value="' + (month + 1) + '"' + (month == default_date.getMonth() ? ' selected>' : '>') + month_array[month].name + '</option>';
    }
    output += '</select>';

    // and year, just do from a minimum of this year, or default year
    var today = new Date();
    var start_date = today.getFullYear();
    if (default_date.getFullYear() < start_date) {
        start_date = default_date.getFullYear();
    }

    output += '<select name="' + year_field + '" onChange="return doDateChange(this.form.elements[\'' + day_field + '\'], this.form.elements[\'' + month_field + '\'], this.form.elements[\'' + year_field + '\'], this.form.elements[\'' + hour_field + '\'], this.form.elements[\'' + minute_field + '\'], this.form.elements[\'' + hidden_field + '\']);">';
    for (var year = start_date ; year <= today.getFullYear() + 15 ; year++) {
        output += '<option value="' + year + '"' + (year == default_date.getFullYear() ? ' selected>' : '>') + year + '</option>';
    }
    output += '</select>';

	
	// Do the hours bit first.
    output += '&nbsp;&nbsp;<select name="' + hour_field + '" onChange="return doDateChange(this.form.elements[\'' + day_field + '\'], this.form.elements[\'' + month_field + '\'], this.form.elements[\'' + year_field + '\'], this.form.elements[\'' + hour_field + '\'], this.form.elements[\'' + minute_field + '\'], this.form.elements[\'' + hidden_field + '\']);">';
    for (var hour = 0 ; hour < 24 ; hour++) {
        output += '<option value="' + (hour < 10 ? "0" : "") + hour + '"' + (hour == default_date.getHours() ? " SELECTED" : "") + '>' + (hour < 10 ? "0" : "") + hour + '</option>';
    }
    output += '</select>';

    // The divider (should probably be more locale specific)
    output += ":";

    // And then the minutes
    output += '<select name="' + minute_field + '" onChange="return doDateChange(this.form.elements[\'' + day_field + '\'], this.form.elements[\'' + month_field + '\'], this.form.elements[\'' + year_field + '\'], this.form.elements[\'' + hour_field + '\'], this.form.elements[\'' + minute_field + '\'], this.form.elements[\'' + hidden_field + '\']);">';
    for (var minute = 0 ; minute < 60 ; minute++) {
        output += '<option value="' + (minute < 10 ? "0" : "") + minute + '"' + (minute == default_date.getMinutes() ? " SELECTED" : "") + '>' + (minute < 10 ? "0" : "") + minute + '</option>';
    }
    output += '</select>';
    
    // This can be changed to a text field to check the value of the
    output += '<input type="hidden" name="' + hidden_field + '" value="' +  default_date.getFullYear() + '-' + (default_date.getMonth() + 1) + '-' + default_date.getDate() + " " + (default_date.getHours() < 10 ? "0" : "") + default_date.getHours() + ':' + (default_date.getMinutes() < 10 ? "0" : "") + default_date.getMinutes() + '">';

    document.write(output);

    return true;
}

// event functions
function doDateChange(day_field, month_field, year_field, hour_field, minute_field, hidden_field) {

    // first set the leap year
    var year = year_field.options[year_field.selectedIndex].value,
        month = month_field.options[month_field.selectedIndex].value - 1,
        day = day_field.options[day_field.selectedIndex].value;
		hour = hour_field.options[hour_field.selectedIndex].value;
		minute = minute_field.options[minute_field.selectedIndex].value;
		
    // set the month_array up
    setLeapYear(year);

    // and then reset the days field
    day_field.options.length = month_array[month].days;

    // reset the day value if no longer valid
    if (day > month_array[month].days)
        day = month_array[month].days;

    for (var index = 0 ; index < month_array[month].days ; index++) {

        // options goes from 0 to day-1
        day_field.options[index].text = day_field.options[index].value = index + 1;

        // and set the default
        if (index + 1 == day)
            day_field.options[index].selected = true;
    }

    // update the hidden field with the combined data from the drop-down select lists.
    hidden_field.value = year + '-' + (month + 1) + '-' + day + " " + hour + ":" + minute;


    return true;
}


//-------------------------------------------------------------------------
// Linked Drop Down Lists
//-------------------------------------------------------------------------
//change list
function changeList(level)
{
	var newLevel = level+1;
	var thisField = fieldName+'_list_'+level;
	var childField = fieldName+'_list_'+newLevel;
	var item_id = document[formName][thisField][document[formName][thisField].selectedIndex].value;
	// reset all the sub-lists
	for (x=level+1; x <= totalLevels; x++){
		//clear list select
		var thisChildField = fieldName+'_list_'+x;
		oldLength = document[formName][thisChildField].length;
		for (i = oldLength; i >= 1; i--){
			document[formName][thisChildField].options[i] = null;
		}
	}
	//build list select
	if (item_id==""){
		//document[formName][childField].options[0] = new Option("Any model     ","");
	}else{
		for (i = 0; i <= listArray[item_id].length-1; i=i+2){
			document[formName][childField].options[1+i/2] = new Option(listArray[item_id][i],listArray[item_id][i+1]);
		}
	}
		
	saveList();
}
// save chosen items
function saveList(){
	var itemList = "";
	for (i=1; i <= totalLevels; i++){
		var thisList = fieldName+'_list_'+i;
		if (i>1){
			itemList = itemList+';'
		}
		itemList = itemList+document[formName][thisList][document[formName][thisList].selectedIndex].value;
	}
	document[formName][fieldName].value = itemList;
	//alert(document[formName][fieldName].value);
}



