/********************************************************************************************************
 *RESOLUTION INTERACTIVE MEDIA INC.																		*
 *560 Wellington Street, London, Ontario, Canada														*
 *Jonathan Kochis (jonathan@resolutionim.com)															*
 *Brendan Farr-Gaynor (brendan@resolutionim.com)														*
 *Owain Johnson (owain@resolutionim.com)																*
 *																										*
 *Version: 1.10 Brendan Farr-Gaynor - 12/19/2007: Removed old project specific functions				*
 *Version: 1.00 Brendan Farr-Gaynor - 11/17/2006: Added handlers for res2.0 ajax cms 					*
 *Version: 0.90 Brendan Farr-Gaynor - 11/17/2006: Extended master ajax functions to run					*
 *												  multiple connections concurrently						*
 ********************************************************************************************************
 *Date Last Modified: 12/19/2007																			*
 *Last Modified By: brendan@resolutionim.com												 			*
 *																										*
 ********************************************************************************************************/


/****************************** GIVE ME A BOX TOGGLE GRAPHICS ***********************************/

/*
These are global variables that need to be included on every page. They are used by the toggle js functions in giveMeABox()
*/


		var toggleOpenPic = new Image;
		var toggleClosePic = new Image;
		
		toggleOpenPic.src = "/images/arrow1.gif";
		toggleClosePic.src = "/images/arrow2.gif";
		
function toggleMyGiveMeABoxTog(myGID) {
	var str = document.getElementById(myGID).src;
	
	if(str.indexOf("arrow1.gif") != -1) {
		document.getElementById(myGID).src = toggleClosePic.src;
	} else {
		document.getElementById(myGID).src = toggleOpenPic.src;
	}
	
	//alert();
	//= closePic.src;
	
}


/****************************** END GIVE ME A BOX TOGGLE GRAPHICS ***********************************/




/****************************** RESOLUTION MASTER AJAX FUNCTIONS ***********************************/

/*
The following are global functions to be used with all new AJAX activity, some applications require thier
own AJAX functions but these should cover the majority.
*/

//array that stores all ajax request objects
var masterRO = new Array();

function resCreateNewRequestObject() {
  
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        masterRO[masterRO.length] = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        masterRO[masterRO.length] = new XMLHttpRequest();
    }
    return masterRO.length - 1;
	//masterRO[masterRO.length];
}


function resSndReqPOST(url, params, whatFunctionToRun) {
	//new function to handle POST requests
	
	reshttp = masterRO[resCreateNewRequestObject()];
	
			//alert(whatFunctionToRun);
		   resAJAXRequestStarted = true;
		   var functionToSend = whatFunctionToRun;
		   reshttp.open('POST', url, true);
		   reshttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		   reshttp.setRequestHeader("Content-length", params.length);
		   reshttp.setRequestHeader("Connection", "close");
		   
		   reshttp.onreadystatechange = function() {
				//data is loaded
					if (reshttp.readyState == 4) {
						resAJAXRequestStarted = false;
						
						var resResponse = reshttp.responseText;
						var arrayToSendBack = new Array();
					
						if(resResponse.indexOf('|' != -1)) {
//							alert(resResponse);
							arrayToSendBack = resResponse.split('|');
							if(functionToSend != false) {
								eval(functionToSend + "(arrayToSendBack);");
							}
							
						}
					}
				
			// end of data loaded
			 }
			 
			reshttp.send(params);
	
}


function resSndReq(queryStringToSend, whatFunctionToRun) {
	
	reshttp = masterRO[resCreateNewRequestObject()];
	

		   resAJAXRequestStarted = true;
		   var functionToSend = whatFunctionToRun;
		   reshttp.open('get', queryStringToSend, true);
		   reshttp.onreadystatechange = function() {
				//data is loaded
					if (reshttp.readyState == 4) {
						resAJAXRequestStarted = false;
						
						var resResponse = reshttp.responseText;
						var arrayToSendBack = new Array();
					
						if(resResponse.indexOf('|' != -1)) {
//							alert(resResponse);
							arrayToSendBack = resResponse.split('|');
							eval(functionToSend + "(arrayToSendBack);");
							
						}
					}
				
			// end of data loaded
			 }
			 
			reshttp.send(null);
	
}


/************************** END OF RESOLUTION MASTER AJAX FUNCTIONS ***********************************/



/************************** END OF RESOLUTION ADMIN AJAX FUNCTIONS ************************************/



//Resolution WYSIWYG functions

function confirmDownloadDelete(loc) {
		if(confirm("You are about to permanently delete this file. Are you sure you want to do this?")) {
			window.location = loc;
		}
	}


function confirmDelete(loc) {
		if(confirm("Are you sure you would like to delete this listing?")) {
			window.location = loc;
		}
	}
	

var x = null;
var xSet = false;


function setActiveName(nameToSet) { //switches X var to the active
	
	if(navigator.appName == "Netscape") {
		
		x = document.getElementById(nameToSet).contentDocument;
	
	} else { //damn IE why can't you be standards compliant
	
		x = eval(nameToSet + ".document"); //iView.document;
	}	
	
	
	
}


function Init(myName) { //turns on designMode and sets the first X
	
	//alert('calling init');
	setActiveName(myName);
	x.designMode = 'On'; //turns them all on
	x.body.style.fontFamily = "Verdana";
	x.body.style.fontSize = "12px";
	x.body.bgColor = "";	
	x.body.text = '#000000';
	xSet = true;
	
}

function setContent(theContent, myName) {
	
	setActiveName(myName);
	x.body.innerHTML = theContent;
	
}


function postIt(textareaName, myName) {  
	setActiveName(myName);
	document.getElementById(textareaName).value = x.body.innerHTML;

}




/// specific action functions

function boldIt(myName) { 
	setActiveName(myName);
	x.execCommand('bold', false, null);
}
	
function italIt(myName) {
	setActiveName(myName);
	x.execCommand('italic', false, null); 
}


function ordListIt(myName) {
	setActiveName(myName);
	x.execCommand('insertorderedlist', false, null); 
}

function supIt(myName) {
	setActiveName(myName);
	x.execCommand('superscript', false, null); 
}

function subIt(myName) {
	setActiveName(myName);
	x.execCommand('subscript', false, null); 
}

function bulListIt(myName) {
	setActiveName(myName);
	x.execCommand('insertunorderedlist', false, null); 
}

function linkIt(myName) { 
	setActiveName(myName);
	
	if(navigator.appName == "Netscape") {
		var szURL = prompt("URL (ex. http://www.google.com)", ""); //Mozilla doensn't support new interface rendering and does not work with safari yet (as of 1.3)
		x.execCommand('createlink',false,szURL); 
	 } else {
		x.execCommand('CreateLink');
	 }
}

function selOn(ctrl) {
	ctrl.style.borderColor = '#CDDAD7';
	ctrl.style.backgroundColor = '#FFFFFF';
	ctrl.style.cursor = 'hand';	
}

function selOff(ctrl) {
	ctrl.style.borderColor = '#E7EEEC';  
	ctrl.style.backgroundColor = '#E7EEEC';
}

function selDown(ctrl) {
	ctrl.style.backgroundColor = '#D3E4DF';
}

function selUp(ctrl) {
	ctrl.style.backgroundColor = '#E7EEEC';
}


//END OF WY FUNCTIONS



/* Resolution Interactive Media */
/* Developed By: Jonathan Kochis And Brendan Farr-Gaynor */
/* jonathan@teamresolution.com / brendan@teamresolution.com */



//Resolution Multi-Selector functions

function addToList(listField, newText, newValue) {
if ( ( newValue == "" ) || ( newText == "" ) ) {
alert("You cannot add blank values!");
} else {
var len = listField.length++; // Increase the size of list and return the size
listField.options[len].value = newValue;
listField.options[len].text = newText;

listField.selectedIndex = len; // Highlight the one just entered (shows the user that it was entered) //BUG FIX
} // Ends the check to see if the value entered on the form is empty
}	

function removeFromList(listField) {
if ( listField.length == -1) { // If the list is empty
alert("There are no values which can be removed!");
} else if (listField.options[listField.selectedIndex].text == "") { //If no value is selected
alert("You cannot remove blank values!"); 
} else {
var selected = listField.selectedIndex;
if (selected == -1) {
alert("You must select an entry to be removed!");
} else { // Build arrays with the text and values to remain
var replaceTextArray = new Array(listField.length-1);
var replaceValueArray = new Array(listField.length-1);
for (var i = 0; i < listField.length; i++) {
// Put everything except the selected one into the array
if ( i < selected) { replaceTextArray[i] = listField.options[i].text; }
if ( i > selected ) { replaceTextArray[i-1] = listField.options[i].text; }
if ( i < selected) { replaceValueArray[i] = listField.options[i].value; }
if ( i > selected ) { replaceValueArray[i-1] = listField.options[i].value; }
}
listField.length = replaceTextArray.length; // Shorten the input list
for (i = 0; i < replaceTextArray.length; i++) { // Put the array back into the list
listField.options[i].value = replaceValueArray[i];
listField.options[i].text = replaceTextArray[i];
}
} // Ends the check to make sure something was selected
} // Ends the check for there being none in the list
}


function moveUpList(listField) {
if ( listField.length == -1) { // If the list is empty
alert("There are no values which can be moved!");
} else {
var selected = listField.selectedIndex;
if (selected == -1) {
alert("You must select an entry to be moved!");
} else { // Something is selected 
if ( listField.length == 0 ) { // If there's only one in the list
alert("There is only one entry!\nThe one entry will remain in place.");
} else { // There's more than one in the list, rearrange the list order
if ( selected == 0 ) {
alert("The first entry in the list cannot be moved up.");
} else {
// Get the text/value of the one directly above the hightlighted entry as
// well as the highlighted entry; then flip them
var moveText1 = listField[selected-1].text;
var moveText2 = listField[selected].text;
var moveValue1 = listField[selected-1].value;
var moveValue2 = listField[selected].value;
listField[selected].text = moveText1;
listField[selected].value = moveValue1;
listField[selected-1].text = moveText2;
listField[selected-1].value = moveValue2;
listField.selectedIndex = selected-1; // Select the one that was selected before  //BUG FIX
} // Ends the check for selecting one which can be moved
} // Ends the check for there only being one in the list to begin with
} // Ends the check for there being something selected
} // Ends the check for there being none in the list
}


function moveDownList(listField) {
if ( listField.length == -1) { // If the list is empty
alert("There are no values which can be moved!");
} else {
var selected = listField.selectedIndex;
if (selected == -1) {
alert("You must select an entry to be moved!");
} else { // Something is selected 
if ( listField.length == 0 ) { // If there's only one in the list
alert("There is only one entry!\nThe one entry will remain in place.");
} else { // There's more than one in the list, rearrange the list order
if ( selected == listField.length-1 ) {
alert("The last entry in the list cannot be moved down.");
} else {
// Get the text/value of the one directly below the hightlighted entry as
// well as the highlighted entry; then flip them
var moveText1 = listField[selected+1].text;
var moveText2 = listField[selected].text;
var moveValue1 = listField[selected+1].value;
var moveValue2 = listField[selected].value;
listField[selected].text = moveText1;
listField[selected].value = moveValue1;
listField[selected+1].text = moveText2;
listField[selected+1].value = moveValue2;
listField.selectedIndex = selected+1; // Select the one that was selected before //BUG FIX
} // Ends the check for selecting one which can be moved
} // Ends the check for there only being one in the list to begin with
} // Ends the check for there being something selected
} // Ends the check for there being none in the list
}


function submitBuckets(textfieldName, listField1) {
var len1 = listField1.length;
var buck1 = "";


for(i=0; i<len1; i++){
	buck1 += listField1.options[i].value;
}


document.getElementById(textfieldName).value = buck1.substring(1);
//alert(buck1);


}

//End Of Multi-Selector functions


function submitDateItem(textfieldName, prefix) {
	//submitDateItem('itemDateTimeEnd', 'ed');
	
	day = document.getElementById(prefix + "Day").value;
	month = document.getElementById(prefix + "Month").value;
	year = document.getElementById(prefix + "Year").value;
	hour = document.getElementById(prefix + "Hour").value;
	minute = document.getElementById(prefix + "Minute").value;
	AMPM = document.getElementById(prefix + "AMPM").value;
	
//vvvvvvvvvvvv was setting 08/20 and 09/21 to 12/00	
//	hour = parseInt(hour);
//^^^^^^^^^^^^ was setting 08/20 and 09/21 to 12/00	
	hour = Number(hour);
	needsExtraZero = true;
	
	if(AMPM == "PM") {
		if(hour != 12) {
			hour = hour + 12;
		}
	} else {
		if(hour == 12) {
			hour = '00';
			needsExtraZero = false;
		}
		
	}
	
	
	if(needsExtraZero) {
		if(hour < 10) {
			hour = "0" + hour;	
		}
	}
		
	
	document.getElementById(textfieldName).value = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":00";
	
}

