// JavaScript Document

//Libary Function
function $(id) {
  return document.getElementById(id);
}

//*****************************FOR TEXT EDITOR **************************//


function enableDesignMode(){

if(!$("editorWindow"))
return;

getIFrameDocument("editorWindow").designMode = "On";
}


function getIFrameDocument(aID){
  // if contentDocument exists, W3C compliant (Mozilla)
  if ($(aID).contentDocument){
    return $(aID).contentDocument;
} else {
    // IE
    return document.frames[aID].document;
  }
}


function doRichEditCommand(aName, aArg){
  
  getIFrameDocument('editorWindow').execCommand(aName,false, aArg);
  $('editorWindow').contentWindow.focus();
}


function populateTextArea()
{
	
$('rte').value = getIFrameDocument('editorWindow').body.innerHTML;

if($('rte').value == "")
{
alert("Solution is blank.");
//$('rte').focus();
return false;
}
else if($('solutiontitle').value == "")
{
alert("Solution title is blank.");
$('solutiontitle').focus();
return false;
}
else if($('name').value == "")
{
alert("Name field is blank.");
$('name').focus();
return false;
}
else if($('email').value == "")
{
alert("Email field is blank.");
$('email').focus();
return false;
}
else if($('email').value != "" && !IsEmail($('email').value))
{
alert("Email field is invalid.");
$('email').focus();
return false;
}

$('getURL').value = getURL();
}


//For RTE system
function populateTextArea_rte()
{
	
$('rte').value = getIFrameDocument('editorWindow').body.innerHTML;

if($('rte').value == "")
{
alert("Please enter some content.");
//$('rte').focus();
return false;
}
else if($('name').value == "")
{
alert("Enter a page name.");
$('name').focus();
return false;
}

$('getURL').value = getURL();

}


function approveSolution()
{
$('getURL').value = getURL();

}

//===========TOGGLE COLOR PICKER============

var ToggleColorPicker = false;
function toggleColorPicker()
{
	if(!ToggleColorPicker)
	{
	$('colorPicker').style.display = "block";
	ToggleColorPicker = true;
	}
	else
	{
	$('colorPicker').style.display = "none";
	ToggleColorPicker = false;	
	}
}

function toggleSource()
{
$('rte').value = getIFrameDocument('editorWindow').body.innerHTML;

	if($('sourceLayer').style.display == 'none')
	$('sourceLayer').style.display = "block";
	else
	$('sourceLayer').style.display = "none";
	
}

function updateInputArea()
{
	getIFrameDocument('editorWindow').body.innerHTML = $('rte').value;
	$('sourceLayer').style.display = "none";
}

function cancelSource()
{
	$('sourceLayer').style.display = "none";
}

function createLink()
{
	theSrc == null;
	var theSrc = prompt("Please enter the URL. (example: http://www.google.com)", "http://");
	
	if(theSrc)
	{
	var Popup = confirm("Load link in a new window? (Pressing Cancel will have the link load in it's current window.)");
	
	if(Popup)
	javaLink = "javascript:openWindow(\'"+theSrc+"\');"
	else
	javaLink = "javascript:location.href=\""+theSrc+"\"";
	
	doRichEditCommand('createlink',javaLink);
	}
}


function createEmail()
{
	
	var theSrc = prompt("Please enter an email address. (example: jon@doe.com)", "")
	
	javaLink = "mailto:"+theSrc;
	
	doRichEditCommand('createlink',javaLink);

}




function checkPopForm_noUpload(form)
{
	if($('picture').value == "")
	{
	alert("Picture Field is blank.");
	$('picture').focus();
	}
	else
	{
	$('head').style.display = "block";
	window.opener.$('editorWindow').contentWindow.focus();
	window.opener.doRichEditCommand('insertImage',$('picture').value);
	window.close();	
	}

}

function checkPopForm2(form)
{
		if(form.picture1.value == "")
	{
		alert("Picture Field is blank.");
		form.picture1.focus();
		return false;
	}

}

function openWindow(theLink)
{
window.open(theLink);	
}

var uploadImgWindow = null;
function uploadImage()
{
params = "left=10,top=20,width="+440+",height="+230+",scrollbars=no";

if(uploadImgWindow == null)
uploadImgWindow= window.open("uploadPop.php",'_blank',params);	
else
{
uploadImgWindow.close();	
uploadImgWindow= window.open("uploadPop.php",'_blank',params);	

}
}

function uploadPDF()
{
params = "left=10,top=20,width="+440+",height="+230+",scrollbars=no";
window.open("uploadPop_PDF.php",'_blank',params);	
}

function uploadPDF_process(theSrc)
{
	javaLink = "javascript:openWindow(\'"+theSrc+"\');"
	doRichEditCommand('createlink',javaLink);	
}

//==============GETURL==================

function getURL()
{
test = location.href;

test= test.split("/");
str = "";
for(i=0;i<(test.length-1);i++)
{
	if(i == 0)
	str = test[i]+"/";
	else
	str = str + test[i] + "/";
}
return str;
}

//==============VERIFYDELETE==============

function verifyDeletePage()
{
for (i=0;i<$('name').length;i++)
   {
     var current = $('name').options[i];
		
		if(current.selected == true)
		pagename = $('name').options[i].text;	
   }
var answer = confirm ("Are you sure you want to delete the page: " + pagename + "?")	

if(answer)
{
$('getURL').value = getURL();	
return true;
}
else
return false;	
}


function IsEmail(sText)
{
var whereisAT = sText.indexOf('@');

if(whereisAT == -1)
return false;
   
var whereisDOT = sText.indexOf('.');  

if(whereisDOT == -1)
return false;

//if makes through checks, return true     
return true;
}
