// global.js
// todo break this up into smaller specific files

// Useful constants

var APP_VERSION = parseFloat( navigator.appVersion );

var IE = false;
var NETSCAPE = true;
var GTCMS_PREFIX = "gtcms/";

var HEADER_style = ("<link href='mainstyle.css' rel='stylesheet' type='text/css'>");

if ( navigator.appName=="Netscape" )
	{
	IE = false;
	NETSCAPE = true;
	GTCMS_PREFIX = "gtcms/";
	}
else if ( navigator.appName=="Microsoft Internet Explorer" )
	{
	IE = true;
	NETSCAPE = false;
	GTCMS_PREFIX = "";
	}

// Util functions
function set_cookie(name, value, expires, path, domain, secure){
  document.cookie= name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function cookie_value(key){ // a fortune
	keypos = document.cookie.indexOf(key);
	result = "";
	if(keypos != -1){
		result = document.cookie.substr(keypos+key.length+1, document.cookie.length);
		endpos = result.indexOf(";");
		if(endpos != -1){
			result = result.substr(0, endpos);
		}
	}
	return (result);
}

function urldecode(text){
	text = text.replace(/\+/g, " ");
	return unescape(text);
}

function decodehtmlspecialchars(aText){
/*
Reversing the php htmlspecialchars function where
'&' (ampersand) becomes '&amp;'
'"' (double quote) becomes '&quot;'
'<' (less than) becomes '&lt;'
'>' (greater than) becomes '&gt;'
*/

//      exp = /&USE_WYSIWYG=\d/;

//  alert(aText);
  aText = aText.replace(/&quot;/g, '"');
//  alert(aText);

  aText = aText.replace(/&lt;/g, '<');
  aText = aText.replace(/&gt;/g, '>');
  aText = aText.replace(/&amp;/g, '&');
  return aText;
}

function test(text){
	alert(text);
}

function doreplace(url){
	window.top.location.replace(url);
}

function left_pad(text, fill, alength){
	while(String(text).length < alength){
		text = fill + text;
	}
	return(text);
}

function zero_pad(text, alength){
	return left_pad(text, "0", alength);
}

function add_slash(aString){
  if(aString[aString.length-1] != "/"){
    aString+="/";
  }
  return(aString);
}

// Date Utils

var DAYS_OF_WEEK = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
var DATE_ONE_DAY = (1000*60*60*24); // milliseconds in 1 day

function sqldate_to_jsdate(sql_date){
	year = parseInt(sql_date.substr(0,4));
	month = parseInt(sql_date.substr(5,2) - 2) + 1;
	day = parseInt(sql_date.substr(8,2) - 1) + 1;
	
//	alert(year + "/" + month + "/" + day);

	hour = parseInt(sql_date.substr(11,2));
	min = parseInt(sql_date.substr(14,2));
	sec = parseInt(sql_date.substr(17,2));

//	tempDate = new Date(0);

//	alert(year + " " + month + " " + day);

//	tempDate.setFullYear(year, month, day);
//	tempDate.setMonth(month);
//	tempDate.setDate(day);
//	tempDate.setHours(hour, min, sec);
//	tempDate.setMinutes(min);
//	tempDate.setSeconds(sec);

	tempDate = new Date(year, month, day, hour, min, sec);

	return(tempDate);
}

function jsdate_to_sqldate(js_date){
	return( js_date.getFullYear() + "-" +
		zero_pad((js_date.getMonth() + 1), 2) + "-" +
		zero_pad(js_date.getDate(), 2) + " " +
		zero_pad(js_date.getHours(), 2) + ":" +
		zero_pad(js_date.getMinutes(), 2) + ":" +
		zero_pad(js_date.getSeconds(), 2) );
}

function date_month_start(adate){
	day = 1;
	month = adate.getMonth();
	year = adate.getFullYear();
	hour = adate.getHours();
	min = adate.getMinutes();
	sec = adate.getSeconds();

	return new Date(year, month, day, hour, min, sec);
}

function simple_date(adate){
	// return string in form DD/MM/YYYY

	dtext = zero_pad(adate.getDate(), 2) + "/" +
		zero_pad(adate.getMonth() + 1, 2) + "/" +
		adate.getFullYear();

//	alert(adate + " " + dtext);

	return (dtext);
}

function days_in_month(aMonth, aYear){
	adate = new Date(aYear, aMonth, 1, 12, 0 ,0);
	count = 0;
	while(adate.getMonth()==aMonth){
//		adate = new Date(adate.valueOf() + DATE_ONE_DAY);
		adate.setDate(adate.getDate() + 1);
		count++;
	}
	return count;
}


// *** form editing controls ***

// combo box lookup editor

function lookup_editor(field_name, lookup_values, lookup_display, current_value){
	document.write("<select name='" + field_name + "'>\n");

	for(iField=0; iField<lookup_display.length; iField++){
		document.write("<option value='" + lookup_values[iField] + "'");
		if(lookup_values[iField] == current_value){
			document.write(" selected");
		}
		document.write(">");
		document.write(lookup_display[iField]);
		document.write("</option>\n");
	}

	document.write("</select>\n");

}

// text editor

function text_editor(field_name, text, rows, cols, do_on_change){
  if(!(rows)){
    rows = 1;
  }

  if(!(cols)){
    cols = 80;
  }

	if (rows==1){
		document.write("<input onchange='"+do_on_change+"' size=" + cols + "  name=\"" + field_name + "\" value=\"" + unescape(text) + "\">");
	}
	else{
		document.write("<textarea style='{width:99%;}' rows=" + rows + " cols=" + cols + "  name=\"" + field_name + "\">");
    document.write(unescape(text));
		document.write("</textarea>");
	}
}

// date editor with popup calendar

function popup_date_editor_html(field_name, adate){

	aMonthStart = date_month_start(adate);

	aTempDate = null;

	calendar = window.open("", "Calendar",
		"height=320,width=320,screenX=300,screenY=200"+
		",alwaysRaised=yes,dependent=yes,resizable=yes");

 	calendar.document.open();
	calendar.document.close();

	html = "";

	html +=

	html += ("<html><head><title>Calendar</title>\n");
	html += urldecode(HEADER_style);
	html += ("\n</head><body>");
	html += ("<table border=1 width='100%' height='260'>");

	html += ("<tr><td colspan='7'>");

	html += ("<table class='spacer' width='100%'><tr><td class='spacer'>");
	html += ("<p align='right'>");
	html += ("<input type='button' value='< Y' onclick='opener.popup_date_prev_year_click(window,\"" + field_name + "\"," + adate.valueOf() + ");'>");
	html += ("<input type='button' value='< M' onclick='opener.popup_date_prev_month_click(window,\"" + field_name + "\"," + adate.valueOf() + ");'>");
	html += ("</p>");
	html += ("</td><td width='100' class='spacer'><center>");
	html += simple_date(adate);
	html += ("</center></td><td class='spacer'>");
	html += ("<input type='button' value='M >' onclick='opener.popup_date_next_month_click(window,\"" + field_name + "\"," + adate.valueOf() + ");'>");
	html += ("<input type='button' value='Y >' onclick='opener.popup_date_next_year_click(window,\"" + field_name + "\"," + adate.valueOf() + ");'>");
	html += ("</td></tr></table>");

	html += ("</td></tr>");

	// day headings
	html += ("<tr>");
	for(iday=0; iday<DAYS_OF_WEEK.length; iday++){
		html += ("<th>");
		html += (DAYS_OF_WEEK[iday]);
		html += ("</th>");
	}
	html += ("</tr>");

	// date grid
	for(irow=0; irow<6; irow++){
		html += ("<tr>");
		for(iday=0; iday<7; iday++){

			if((irow==0) && (iday == aMonthStart.getDay())){
				// first row - contains first day of month
				aTempDate = new Date(aMonthStart);
			}

			if((aTempDate != null) && (aTempDate.getMonth() == aMonthStart.getMonth())){
				//alert(aTempDate + " " + aTempDate.getMonth() + " " + aMonthStart.getMonth());
				header = (aTempDate.getDate() == adate.getDate());

				if(header){
					html += ("<th class='calendar_numbers'>");
				}
				else{
					html += ("<td class='calendar_numbers'>");
				}

				aTempDate.setHours(adate.getHours(),adate.getMinutes(),adate.getSeconds());
				html += ("<a href='#' class='calendar_numbers' onclick='opener.popup_date_click(window,\"" + field_name + "\"," + aTempDate.valueOf() + ");'>");

				html += (left_pad(aTempDate.getDate(), "&nbsp;" , 2));

				//aTempDate = new Date(aTempDate.valueOf() + DATE_ONE_DAY);
				newday = parseInt(aTempDate.getDate()) + 1;
				aTempDate.setDate(newday);
				aTempDate.setHours(12,0,0); // fixes k bug

				html += ("</a>");

				if(header){
					html += ("</th>");
				}
				else{
					html += ("</td>");
				}
			}
			else{
				html += ("<td>");
				html += ("&nbsp;");
				html += ("</td>");
			}

		}
		html += ("</tr>");
	}

	html += ("<tr><td colspan='7'>");
	html += ("<input type='button' value='Cancel' onclick='opener.popup_date_cancel_click(window);'>");
	html += ("</td></tr>");
	html += ("</table>");
	html += ("</body></html>");

	calendar.document.write(html);
}

function popup_date_editor(field_name){
	afield = document.forms["edit_content"].elements[field_name];

	adate = sqldate_to_jsdate(afield.value);
//	alert("(" + afield.value + ") (" + adate + ")");

	popup_date_editor_html(field_name, adate);
}

function popup_date_cancel_click(calendar){
	calendar.close();
}

function popup_date_click(calendar, field_name, date){
	document.forms["edit_content"].elements[field_name].value =
		jsdate_to_sqldate(new Date(date));
	calendar.close();
}

function new_date_popup(adate, newYear, newMonth, field_name){
	// adjust day to within month
	newDay = Math.min(adate.getDate(), days_in_month(newMonth, newYear));

	aNewDate = new Date(newYear, newMonth, newDay,
		adate.getHours(), adate.getMinutes(), adate.getSeconds());

	popup_date_editor_html(field_name, aNewDate);
}

function popup_date_prev_month_click(calendar, field_name, date){
	adate = new Date(date);

	if(adate.getMonth() == 0){
		newYear = adate.getFullYear() - 1;
		newMonth = 11;
	}
	else{
		newYear = adate.getFullYear();
		newMonth = adate.getMonth() - 1;
	}

	new_date_popup(adate, newYear, newMonth, field_name);
}

function popup_date_next_month_click(calendar, field_name, date){
	adate = new Date(date);

	if(adate.getMonth() == 11){
		newYear = adate.getFullYear() + 1;
		newMonth = 0;
	}
	else{
		newYear = adate.getFullYear();
		newMonth = adate.getMonth() + 1;
	}

	new_date_popup(adate, newYear, newMonth, field_name);
}

function popup_date_prev_year_click(calendar, field_name, date){
	adate = new Date(date);

	newYear = adate.getFullYear() - 1;
	newMonth = adate.getMonth();

	new_date_popup(adate, newYear, newMonth, field_name);
}

function popup_date_next_year_click(calendar, field_name, date){
	adate = new Date(date);

	newYear = adate.getFullYear() + 1;
	newMonth = adate.getMonth();

	new_date_popup(adate, newYear, newMonth, field_name);
}

function now_date_editor(field_name){
	afield = document.forms["edit_content"].elements[field_name];
	afield.value = jsdate_to_sqldate(new Date());
}

function date_editor(field_name, text, rows, cols){
	text_editor(field_name, text, rows, 19);
	document.write("<!-- comment fixes k bug --> "+
		"<img alt='Popup Calendar' class='fake_link' "+
		"onclick='popup_date_editor(\""+field_name+"\");' border=0 src='gtcms/images/1day.gif'>");
	document.write("<img alt='Now' class='fake_link' "+
		"onclick='now_date_editor(\""+field_name+"\");' border=0 src='gtcms/images/today.gif'>");
}

// hidden

function hidden_editor(field_name, text){
  document.write("<input type=\"hidden\" name=\"" + field_name + "\" id=\"" + field_name + "\" value=\"" + unescape(text) + "\">");
}

// bool checkbox

function update_checkbox(hidden_name, checked){
  if(checked){
    document.forms["edit_content"].elements[hidden_name].value = 1;
  }
  else{
    document.forms["edit_content"].elements[hidden_name].value = 0;
  }
}

function bool_editor(field_name, text){
  if( text == 1 ){
    checked = ' CHECKED ';
  }
  else{
    checked = '';
  }
  document.write("<input type=\"checkbox\" ");
  document.write("onclick=\"update_checkbox('" + field_name + "',this.checked)\" ");
  document.write("name=\"_dummy_" + field_name + "\" " + checked + ">");
  document.write("<input type=\"hidden\" name=\"" + field_name + "\" value=\"" + text + "\">");
}

// window functions

function window_open_center(aURL, aTitle, aWidth, aHeight, aExtra){

  xPos = ((parseInt(screen.width) - parseInt(aWidth)) / 2);
  yPos = ((parseInt(screen.height) - parseInt(aHeight)) / 2);
  aWindow = window.open(aURL, aTitle,
    aExtra + ",width="+aWidth+",height="+aHeight+",left="+xPos+",top="+yPos);  
  return(aWindow);
}

// a plain standard window with no title bar or status bar and no resize
function window_open_dialog(aURL, aTitle){
  aWindow = window_open_center(aURL, aTitle, 600, 400,
    "toolbar=no,status=no,scrollbars=yes,resizable=no,menubar=no,dependent=yes");   
  return(aWindow);
}

// program functions

function new_content(){
  window.top.frames['content_preview'].location='gtcms/new_content.php';
}

function edit_permissions(content_id){
  url = add_slash(HTML_BASE) + "gtcms/browse_permissions.php?page_id=" + content_id;
  window_open_dialog(url, "_blank");
}

function edit_permission(permission_id){
  url = add_slash(HTML_BASE) + "gtcms/edit_permission.php?id=" + permission_id;
  window_open_dialog(url, "_blank");
}

function new_permission(content_id){
  url = add_slash(HTML_BASE) + "gtcms/new_permission.php?page_id=" + content_id;
  window_open_dialog(url, "_blank");
}

function delete_permission(content_id, delete_forward){
	if(confirm("This will delete permission record ("+content_id+"). This cannot be undone.")){
		location=add_slash(HTML_BASE) + "gtcms/delete_permission.php?id=" + content_id + "&forward="+delete_forward;
	}
}



function onSave(){
  ToggleHTML(true);
  
  //updateRTE('rte_content');
  
  document.forms["edit_content"].submit();
}

function top_buttons(){
	document.write("<img class='fake_link' onclick='onSave();' border=0 alt='Save' src='gtcms/images/filesave.gif'>");

	document.write("&nbsp;");

	document.write("<img class='fake_link' onclick='delete_page(\"" + id + "\",\"" + name + "\");' border=0 alt='Delete' src='gtcms/images/edittrash.gif'>");
	
	if(EDIT_PAGE){
		document.write("&nbsp;");
		document.write("<a href='gtcms/new_content.php?parent_id="+id+"'>");
		document.write("<img border=0 alt='New Child Record' src='gtcms/images/filenew2.gif'>");
		document.write("</a>");
		document.write("&nbsp;");
		document.write("<a target='_blank' href='" + HTML_BASE + "?page_id=" + id + "'>");
		document.write("<img alt='Preview' src='gtcms/images/1rightarrow.gif' border=0></a>");
		document.write("&nbsp;");

//		document.write("<a target='_blank' href='" + HTML_BASE + "?page=gtcms_admin/browse_permissions&permission_page=" + id + "'>");
//		document.write("<img alt='Edit Permissions' src='gtcms/images/encrypted.gif' border=0></a>");

		document.write("<img class='fake_link' alt='Edit Permissions' onclick='edit_permissions(" + id + ")' src='gtcms/images/encrypted.gif' border=0>");
	}
}

function delete_page(page_id, page_name){
	if(confirm("Delete record (" + page_name + ") and all child records?")){
		location=GTCMS_PREFIX + "delete_content.php?page_id=" + page_id;
	}
}

function browse_for_parent(child_id){
	url = GTCMS_PREFIX + "browse_for_parent.php?id=" + child_id;
	awindow = window.open(url, "browse_for_parent", "resizable=yes,scrollbars=yes,outerwidth=400,outerheight=600,width=400,height=600");
}

function browse_for_content(){
	url = GTCMS_PREFIX + "browse_for_content.php";
	awindow = window.open(url, "browse_for_content", "resizable=yes,scrollbars=yes,outerwidth=400,outerheight=600,width=400,height=600");
}

function browse_for_content_id(){
	url = GTCMS_PREFIX + "browse_for_content_id.php";
	awindow = window.open(url, "browse_for_content_id", "resizable=yes,scrollbars=yes,outerwidth=400,outerheight=600,width=400,height=600");
	return(awindow);
}

function LinkContent(){
  document.forms['edit_content'].elements['_browse_content_result'].value="";
  awindow = browse_for_content_id("_browse_result_id");
//  browse_content_id = document.forms['edit_content'].elements['_browse_result_id'].value;
//  a_content_field = document.forms['edit_content'].elements['content'];
//  a_content_field.value = a_content_field.value + '<a href="?page_id='+browse_content_id+'">Content '+browse_content_id+'</a>';
}

function selectionText(aTextArea){
  if (document.selection){
    return(document.selection.createRange().text);
  }
  else{
    return(aTextArea.value.substring(aTextArea.selectionStart, aTextArea.selectionEnd));
  }
}

//myField accepts an object reference, myValue accepts the text strint to add
// ripped from here: http://www.webmasterworld.com/forum91/4686.htm
function insertAtCursor(myField, myValue){
  //IE support
  if (document.selection){
    myField.focus();
    
    //in effect we are creating a text range with zero
    //length at the cursor location and replacing it
    //with myValue
    sel = document.selection.createRange();
    sel.text = myValue;
  } 
  //Mozilla/Firefox/Netscape 7+ support
  else{
    //Here we get the start and end points of the
    //selection. Then we create substrings up to the
    //start of the selection and from the end point
    //of the selection to the end of the field value.
    //Then we concatenate the first substring, myValue,
    //and the second substring to get the new value.
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length);
  }
} 


function AddContentLink(){  
//  browse_for_content_id("_browse_result_id");
  browse_result_id = document.forms['edit_content'].elements['_browse_content_result'].value;
  //alert(browse_result_id);
  if((browse_result_id!=undefined) && (browse_result_id!="")){
    a_content_field = document.forms['edit_content'].elements['content'];
    //a_content_field.value = a_content_field.value + '<a href="?page_id='+browse_result_id+'">Content '+browse_result_id+'</a>';
    current_selection = selectionText(a_content_field);
    insertAtCursor(a_content_field, '<a href="?page_id=' + browse_result_id + '">' + current_selection + '</a>');
  }
  document.forms['edit_content'].elements['_browse_content_result'].value="";
}

// Permissions

function edit_permission_buttons(){
	document.write("<img class='fake_link' onclick='document.forms[\"edit_content\"].submit();' border=0 alt='Save' src='gtcms/images/filesave.gif'>");

//	document.write("&nbsp;");
//	document.write("<a href='" + HTML_BASE + "?page=gtcms_admin/browse_permissions&permission_page=" + content_id + "'>");
//  document.write("<img alt='Browse Permissions' src='gtcms/images/encrypted.gif' border=0></a>");
}

//function delete_permission(id, permission, user_name, content_name){
//	if(confirm("Remove (" + permission + ") permission for user (" + user_name + ") from content (" + content_name + ")")){
//		location=GTCMS_PREFIX + "delete_permission.php?id=" + id;
//	}
//}




// Treeview

var tree_nodes_to_open;

function do_open_tree(node){
//  alert('do_open_tree');
/*
	if((node.className == "menubar")){
	
		if( (node.childNodes[0].className == "actuator") && (node.childNodes[0].id != "Actuator_0") ){
			if(node.childNodes[0].toggle != null){
				node.childNodes[0].toggle();
			}
		}

	}
*/

//  if(node.className=="actuator"){
//    tree_nodes_to_open[tree_nodes_to_open.length] = node;
//  }
//  else if(node.className=="menubar"){
//    tree_nodes_to_open[tree_nodes_to_open.length] = node;
//  }

  if(node.id!=null){

    if(node.id.substr(0,9)=="Actuator_"){
      node_to_add = document.getElementById("Actuator_" + node.id.substr(9));
      if(node_to_add.id!="Actuator_0"){
        if(!in_array(tree_nodes_to_open, node_to_add)){
          tree_nodes_to_open[tree_nodes_to_open.length] = node_to_add;
//          alert(tree_nodes_to_open.length + "  Actuator_" + node.id.substr(9));
        }
      }
    }

    if(node.id.substr(0,5)=="Menu_"){
      node_to_add = document.getElementById("Actuator_" + node.id.substr(5));
      if(node_to_add.id!="Actuator_0"){
        if(!in_array(tree_nodes_to_open, node_to_add)){
          tree_nodes_to_open[tree_nodes_to_open.length] = node_to_add;
//          alert(tree_nodes_to_open.length + "  Actuator_" + node.id.substr(5));
        }
      }
    }
  }
//	alert("test");

  if(node.parentNode != null){
		do_open_tree(node.parentNode);
	}
	

}

function open_tree(){
//  alert('open_tree');
  
//  tree_nodes_to_open = new Array();
  
//  do_open_tree(node);

//  alert(tree_nodes_to_open.length);

//  alert(window.top.frames["content_tree"].document);

  tree_frame = window.top.frames["content_tree"];

  if(tree_frame == null){
    tree_frame = window;
  }

  tree_document = tree_frame.document;
  

  content_form = tree_document.forms["tree_content"];
  content_field = content_form.elements["loaded_nodes"];
  loaded_nodes = content_field.value.split(",");
  
  for(i=0;i<loaded_nodes.length;i++){
    node_id = loaded_nodes[i];
    if((node_id != 0) && (node_id!="")){
      a_node = tree_document.getElementById("Actuator_" + node_id);

      if(a_node != null){

//          alert(node_id + " " + a_node.toggle);
        if(a_node.toggle != undefined){
          a_node.open();
//          if(!a_node.is_open()){
//            a_node.toggle();
//          }

        }
      }
    }
  }
}

/*
function select_tree_node(node_id){
//	var actuator = window.top.frames["content_tree"].document.getElementById("Actuator_" + node_id);
	var actuator = document.getElementById("Actuator_" + node_id);

//	document.getElementById("Actuator_" + node_id).toggle();
//  alert(document.getElementById("Actuator_" + node_id).id);

	if(actuator != null){
		open_tree(actuator);
	}
}
*/

function reload_tree(){
  content_window = window.top.frames["content_tree"];
  if(content_window != null){
    //content_window.location = GTCMS_PREFIX + 'reload_tree.php' + content_window.location.search;
    content_window.document.forms["tree_content"].submit();
  }
}

function go_content_tree(){
  content_window = window.top.frames["content_tree"];
  if(content_window != null){
//	alert(GTCMS_PREFIX + 'content_tree.php' + location.search);
	 content_window.location.replace(GTCMS_PREFIX + 'content_tree.php' + content_window.location.search);
  }
}

function set_last_clicked_node(node_id){
	parent.document.last_clicked_node = node_id;
}

function get_last_clicked_node(){
	return(parent.document.last_clicked_node);
}

function tree_click(node_id){
	set_last_clicked_node(node_id); // gth keep track of selected node
	return (true); // run link too
}

function synch_tree(){
//	last_clicked_node = get_last_clicked_node();
//	select_tree_node(last_clicked_node)
  open_tree();
}

// bullet
function bullet_item(aText, aLink){
	bullet = "<img align=middle src='bullet01.jpg'> ";
	document.write('<a href="' + aLink + '">' + bullet + aText + '</a><br>');
}

//button
function button(aText, aLink){
	aHtml = "";
	aHtml += ('<input class="buy_button" type="button" value="');
	aHtml += (aText);
	aHtml += ('" onclick="location=\'');
	aHtml += (aLink);
	aHtml += ('\'">');

	document.write(aHtml);
}

function buy_button(aLink){
	button("BUY NOW", aLink);
}

// GTCMS Options
function edit_option(option_name, option_value){
  url = "gtcms/edit_option.php?option_name=" + option_name + "&option_value=" + option_value;
  msg = window_open_center(url, "editWindow", 300, 200, "menubar=no");
}


// Security
function logout(){
  document.cookie="";
	parent.document.cookie="";
	doreplace("gtcms/logout.php");
}

function logged_in_username(){
	return (cookie_value('GTCMS_USERNAME'));
}

function logged_in(){
	return (logged_in_username() != '');
}

function check_login(){
//	alert(document.cookie);
	if(!logged_in()){
//		alert("LOG - OUT");
		doreplace("logout.php");
		document.write("</html>");
		document.close();
	}
}

function in_array(AArray, AElement){
  result = false;
  for(i=0;i<AArray.length;i++){
    if(AArray[i]==AElement){
      result = true;
      break;
    }
  }
  return(result);
}


// edit form stuff

  function toggle_textarea(){
    aField = document.forms['edit_content'].elements['content'];
    aButton = document.forms['edit_content'].elements['expand_content'];

    expanded_rows = 200;
    contracted_rows = 15;
    current_rows = aField.rows;
    if(current_rows == expanded_rows){
      current_rows = contracted_rows;
      aButton.value = "  +  ";
    }
    else{
      current_rows = expanded_rows;
      aButton.value = "  -  ";
    }
    aField.rows = current_rows;
  }

  function content_changed(){
    current_value = document.forms['edit_content'].elements['content'].value;
    return (current_value != decodehtmlspecialchars(unescape(content)));
  }

  function ShowRTE(show){
    var rte = document.getElementById("rte_content");
    var rteFrame = getFrame("rte_content");
    var arteDiv = document.getElementById("rteDiv");

    var rteToolbar1 = document.getElementById("rteBack1");
    var rteToolbar2 = document.getElementById("rteBack2");

//    alert(rteToolbar1.style.height);

    if(show){
      //alert(rteFrame);
      rteToolbar1.style.display = "";
      rteToolbar2.style.display = "";
      arteDiv.style.display = "";

      rteFrame.document.designMode = "On";
//      arteDiv.style.visibility = "visible";
//      arteDiv.style.position = "relative";
//      rte.style.visibility = "visible";
//      rte.height = EDITOR_HEIGHT;
//      rte.width = EDITOR_WIDTH;
      enableDesignMode('rte_content', rte_content, false);
    }
    else{
      rteToolbar1.style.display = "none";
      rteToolbar2.style.display = "none";
      arteDiv.style.display = "none";

      rteFrame.document.designMode = "Off";
//      arteDiv.style.visibility = "hidden";
//      arteDiv.style.position = "fixed";
//      rte.style.visibility = "hidden";
//      rte.height = '0';
//      rte.width = '0';
    }
  }

  function ShowHTML(show){
    if(show){
      htmldiv.style.display = "";

      htmldiv.style.visibility = "visible";
      htmldiv.style.position = "";
//      htmldiv.style.height = EDITOR_HEIGHT;
//      htmldiv.style.width = EDITOR_WIDTH;
    }
    else{
      htmldiv.style.display = "none";

      htmldiv.style.visibility = "hidden";
      htmldiv.style.position = "absolute";
//      htmldiv.style.height = '0';
//      htmldiv.style.width = '0';
    }
  }

  function ToggleHTML(show){
    var htmldiv;
    if(htmldiv != undefined){
      if(show){
        if(htmldiv.style.visibility != "visible"){
       	  updateRTE('rte_content');
          //document.getElementById('content').value = document.getElementById('hdnrte_content').value;
          document.forms['edit_content'].elements['content'].value = document.getElementById('hdnrte_content').value;
          ShowHTML(true);
          ShowRTE(false);
        }
      }
      else{
        if(htmldiv.style.visibility == "visible"){
          rte_content = document.forms['edit_content'].elements['content'].value;//document.getElementById('content').value;
          ShowHTML(false);
          ShowRTE(true);
        }
      }
    }
  }

function edit_skin_file(aSkinName){
  aWindow = window_open_center(HTML_BASE+"gtcms/edit_skin.php?skin_name="+aSkinName, 
    "Edit_Skin", 1024, 768,
    "toolbar=no,status=yes,scrollbars=yes,resizable=yes,menubar=no");
}

function edit_skin_css(aSkinName){
  aWindow = window_open_center(HTML_BASE+"gtcms/edit_skin.php?skin_name="+aSkinName+"&css=yes", 
    "Edit_Skin CSS", 1024, 768,
    "toolbar=no,status=yes,scrollbars=yes,resizable=yes,menubar=no");
}

function edit_admin_skin_file(aSkinName){
  aWindow = window_open_center(HTML_BASE+"gtcms/edit_skin.php?skin_name="+aSkinName+"&admin=_ADMIN", 
    "Edit_Skin", 1024, 768,
    "toolbar=no,status=yes,scrollbars=yes,resizable=yes,menubar=no");
}

function edit_admin_skin_css(aSkinName){
  aWindow = window_open_center(HTML_BASE+"gtcms/edit_skin.php?skin_name="+aSkinName+"&admin=_ADMIN"+"&css=yes", 
    "Edit_Skin CSS", 1024, 768,
    "toolbar=no,status=yes,scrollbars=yes,resizable=yes,menubar=no");
}

function keyPress(aEvent){
  result = true;
  
  if(aEvent.ctrlKey){
    if(window.event){// IE
      aKeyCode = aEvent.keyCode;
    }
    else if(aEvent.which){// Netscape/Firefox/Opera
      aKeyCode = aEvent.which;    
    }  
  
    if(aKeyCode == 83){
      //alert(aKeyCode + " " + aEvent.ctrlKey);
      onSave();
      result = false;
    }    
  }
  
  return(result);
}
