// JavaScript Document
// Written by Francis Villanueva (fv@medianueva.com)
// For GRETEL / Greg Hahn / www.gretelny.com
// April 15, 2005

function checkForDupes(whatForm) {
	//get length of all elements in form passed to function
	var formLength = document.forms[whatForm].elements.length;
	//make new array to hold only the order text boxes
	var checkArray = new Array();
	//loop through all elements in form, find only "link order" boxes
	//push those into checkArray
	for (i=0; i<formLength; i++) {
		var currElement = document.forms[whatForm].elements[i];
		var currName = currElement.name.slice(0, 5);
		if (currName == "order") {
			checkArray.push(i);
		}
	}
	//get length of checkArray
	var checkLength = checkArray.length;
	//loop through all link order text boxes
	for (i=0; i<checkLength; i++) {
		var currCheckBox = document.forms[whatForm].elements[checkArray[i]];
		//if current link order text box is blank, stop and alert user
		if (currCheckBox.value == "") {
			alert("The order number for \""+document.forms[whatForm].elements[(checkArray[i]-2)].value+"\" was left blank");
			return false;
		}
		//loop through order text boxes and compare current order box with those
		//if any of them have same value, stop and alert user
		for (j=(i+1); j<checkLength; j++) {	
			if (currCheckBox.value == document.forms[whatForm].elements[checkArray[j]].value) {
				alert ("You have more than one "+currCheckBox.value+" in your order list");
				return false;
			}
			
		}
	}
	return true;
}

function openCenteredWindow(URL,winName,w,h,winfeatures) {
	//finds middle point of user's screen
	var left = (screen.width-w)/2;
	var top = (screen.height-h)/2;
	//open's new window
	var newWin = window.open(URL,winName,'width='+w+',height='+h+',left='+left+',top='+top+'toolbar=0,menubar=0,location=0,resizable=0,scrollbars=no,status=no,'+winfeatures);
	//move window based off of left and top variables
	newWin.moveTo(left,top)
	//focuses to new window
	newWin.focus();

}
//THIS FUNCTION IS CALLED WHEN A USER FIRST GOES TO THE MAIN PAGE.
//IT SETS THEIR BROWSER WINDOW TO BE AT LEAST 875 PIXELS BIG.
function maximizeWindow() {
	if (document.all) {
	//IE
	//alert(document.body.clientWidth);
		if (document.body.clientWidth < 800) {
			window.resizeTo(875,600);
		}
	} else {
  	//Firefox
    //alert("not all"+window.innerWidth);
		if (window.innerWidth < 875) {
			window.resizeTo(875,700);
		}
	}
}
//==========================================
//EXTERNAL INTERFACE ADDED ON APRIL 15, 2006
//==========================================
// CALLS ONE OF THE FLASH MOVIE'S FUNCTIONS: sendTextToFlash. PASSES TEXT FROM TEXTAREA AS AN ARGUMENT.
function callExternalInterface() {
	var textBoxWidth = document.getElementById("textBoxWidth").value;
	var userText = document.getElementById("extraTxt").value;
	var valueUpdated = getFlashMovie("dynamicTextBox").sendTextToFlash(textBoxWidth, userText);
	toggleSubmit(false);
}

//FUNCTION TO UPDATE THE INPUT VALUE OF THE FLASH'S NEW MOVIE HEIGHT
function setFlashMovieHeight(textBoxHeight) {
	var heightInput = document.getElementById('flashMovieHeight');
	heightInput.value = textBoxHeight;
	return true;
}
//FUNCTION THAT FINDS THE FLASH MOVIE IN THE PAGE
function getFlashMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}
function toggleSubmit(state) {
	var submitBtn = document.getElementById("flashHeightSubmit");
	submitBtn.disabled = state;
}

function updateSort() {
	document.getElementById("newOrder").value = Sortable.serialize('sortList');
	return true;
}

window.onload = function () {
	//CHECK TO SEE IF FLASH DYNAMIC TEXT BOX IS ON PAGE
	if(document.getElementById("dynamicTextBox")) {
		var textBox = document.getElementById("extraTxt");
		textBox.onblur = function() {
			callExternalInterface();
		}
		textBox.onfocus = function() {
			toggleSubmit("disabled");
		}
		//var textBoxWidth = document.getElementById("textBoxWidth").value;
		//getFlashMovie("dynamicTextBox").setTextBoxWidth(textBoxWidth);
	}
	//CREATE DRAG/DROP SORT LIST ITEMS
	if(document.getElementById("sortList")) {
		Sortable.create('sortList',{tag:'li'});
	}
	if(document.getElementById("sortListForm")) {
		document.getElementById("sortListForm").onsubmit = function() {
			return updateSort();
		}
	}
}