////////////////////////////////////////////////////////////////////////////
////////////                 Global Javascripts                 ////////////
////////////////////////////////////////////////////////////////////////////
// Get Browser Version //
function VersionNav(Netscape, Explorer) {
	if ((navigator.appVersion.substring(0,3) >= Netscape && navigator.appName == 'Netscape') ||      
      (navigator.appVersion.substring(0,3) >= Explorer && navigator.appName.substring(0,9) == 'Microsoft'))
			return true;
	else return false;
}

// Refresh the Menu Frame //
function refresh() { 
  parent.frames[1].location = "frame-menu.cfm"; 
}

// Update Parent Window without Closing Popup //
function updateParent(newURL) {
  opener.window.focus();
  opener.document.location = newURL;
}

// Focus the Page Load //
function focusThis() {
  window.focus();
}

// Update Parent Window and close Popup //
function changetoParent(newURL) {
  opener.window.focus();
  opener.document.location = newURL;
  window.close();
}

// Old School Confirm Alert Box //
function DoConfirm(message) {
  return confirm(message);
}

// Open Popup Window //
function OpenWindow(theURL,winName,features) { //v2.0
  NewWindow=window.open(theURL,winName,features);
}

// Top Navigation Jump //
function navJump(menuURL,mainURL) { //v2.0
  parent.menu.location = menuURL;
  parent.main.location = mainURL;
}

////////////////////////////////////////////////////////////////////////////
////////////                 Editor Javascripts                 ////////////
////////////////////////////////////////////////////////////////////////////

// Insert plain tag - No Wrap, No Prompt //
function insertTag(tag,formname,fieldname) {
	if (document.forms[formname].elements[fieldname].createTextRange && document.forms[formname].elements[fieldname].caretPos) {      
		var caretPos = document.forms[formname].elements[fieldname].caretPos;      
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
		tag + ' ' : tag;
	}
	else document.forms[formname].elements[fieldname].value += tag;
	document.forms[formname].elements[fieldname].focus(caretPos)
}

//function insertTag(tag) {
//  document.selection.createRange().text = tag;
//}

// Insert Tag - No Wrap, Prompt for Value //
function insertTagPrompt(prompttext,pre,post) {
  var p_text = prompt(prompttext, '');
  if (p_text != null) document.selection.createRange().text = pre + p_text + post;
}

// Insert Tag - Wrap Selected Text, No Prompt //
function insertTagWrap(pre,post) {
  if (!document.selection) return;
  var str = document.selection.createRange().text;
  if (!str) {
    alert('Please select the text your wish to apply this action to.');
    return;
  }
  else document.selection.createRange().text = pre + str + post;
}

// Insert Tag - Wrap Selected Text, Prompt for Value //
function insertTagWrapPrompt(prompttext,filler,pre,mid,post) {
  if (!document.selection) return;
  var str = document.selection.createRange().text;
  if (!str) {
    alert('Please select the text your wish to apply this action to.');
    return;
  }
  else {
    var p_text = prompt(prompttext, filler + '');
    if (p_text != null) document.selection.createRange().text = pre + p_text + mid + str + post;
  }
}

// Editor Functions //
function bold() {
  insertTagWrap('<b>','</b>');
}

function italic() {
  insertTagWrap('<i>','</i>');
}

function underline() {
  insertTagWrap('<u>','</u>');
}

function div(type) {
  if (type == 'c') insertTagWrapPrompt('Alignment (left/center/right):','','<div align="','">','</div>')
  if (type == 'left') insertTagWrap('<div align="left">','</div>')
  if (type == 'center') insertTagWrapPrompt('<div align="center">','</div>')
  if (type == 'right') insertTagWrapPrompt('<div align="right">','</div>')
}

function hr(formname,fieldname) {
  insertTag('<hr>',formname,fieldname);
}

function httplink() {
  if (!document.selection) return;
  var str = document.selection.createRange().text;
  if (!str) {
    alert('Please select the text your wish to apply this action to.');
    return;
  }
  else {
    var url_address = prompt('Enter the URL Address:', 'http://');
    var target = prompt('Link Target (Click CANCEL for None):', '_blank');
    
    if (target != null && target != '') var newtarget = ' target="' + target + '"';
    else var newtarget = '';
  
    if (url_address != null) document.selection.createRange().text = '<a href="' + url_address + '"' + newtarget + '>' + str + '</a>';
  }
}

function emaillink() {
  insertTagWrapPrompt('Enter the Email Address:','','<a href="mailto:','">','</a>')
}

function picture(formname,fieldname) {
  var picpath = prompt('Enter the Path to the Picture:', '');

  var border = prompt('Border: (Click CANCEL for None)', '0');
  if (border != null && border != '') var border = ' border="' + border + '"';
  else var border = '';

  var width = prompt('Width: (Click CANCEL for None)', '');
  if (width != null && width != '') var width = ' width="' + width + '"';
  else var width = '';

  var height = prompt('Height: (Click CANCEL for None)', '');
  if (height != null && height != '') var height = ' height="' + height + '"';
  else var height = '';

  var descp = prompt('Short Image Description: (Click CANCEL for None)', '');
  if (descp != null && descp != '') var descp = ' alt="' + descp + '"';
  else var descp = '';
  
  var align = prompt('Alignment: (Click CANCEL for None)', '');
  if (align != null && align != '') var align = ' align="' + align + '"';
  else var align = '';
  
  var tag = '<img src="' + picpath + '"' + border + width + height + descp + align + '>';
  
  if (picpath != null && picpath != '') {
  	if (document.forms[formname].elements[fieldname].createTextRange && document.forms[formname].elements[fieldname].caretPos) {      
  		var caretPos = document.forms[formname].elements[fieldname].caretPos;      
  		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?
  		tag + ' ' : tag;
  	}
  	else document.forms[formname].elements[fieldname].value += tag;
  	document.forms[formname].elements[fieldname].focus(caretPos)  
  }
}

// Short Cut Keys //
function shortCuts() {
  if (event.ctrlKey != true) return;
  if (event.keyCode == 1) div('c'); // CTRL+SHIFT+A - DIV ALIGN
  if (event.keyCode == 2) bold(); // CTRL+SHIFT+B - BOLD SELECTED TEXT
  if (event.keyCode == 5) emaillink(); // CTRL+SHIFT+E - EMAIL LINK SELECTED TEXT
  if (event.keyCode == 8) httplink(); // CTRL+SHIFT+H - HTML LINK SELECTED TEXT
  if (event.keyCode == 9) italic(); // CTRL+SHIFT+I - ITALIZCIZE SELECTED TEXT
  //if (event.keyCode == 16) picture(); // CTRL+SHIFT+P - ADD IMAGE 
  if (event.keyCode == 21) underline();  // CTRL+SHIFT+U - UNDERLINE SELECTED TEST
  //if (event.keyCode == 31) hr(); // CTRL+SHIFT+- - ADD HORAZONTAL RULE
}