function MoveOption(objSourceElement, objTargetElement){
	var aryTempSourceOptions = new Array();
	var x = 0;
	
	//looping through source element to find selected options
	for (var i = 0; i < objSourceElement.length; i++) {
		if (objSourceElement.options[i].selected) {
			//need to move this option to target element
			var intTargetLen = objTargetElement.length++;
			objTargetElement.options[intTargetLen].text = objSourceElement.options[i].text;
			objTargetElement.options[intTargetLen].value = objSourceElement.options[i].value;
		}
		else {
			//storing options that stay to recreate select element
			var objTempValues = new Object();
			objTempValues.text = objSourceElement.options[i].text;
			objTempValues.value = objSourceElement.options[i].value;
			aryTempSourceOptions[x] = objTempValues;
			x++;
		}
	}
	
	//resetting length of source
	objSourceElement.length = aryTempSourceOptions.length;
	//looping through temp array to recreate source select element
	for (var i = 0; i < aryTempSourceOptions.length; i++) {
		objSourceElement.options[i].text = aryTempSourceOptions[i].text;
		objSourceElement.options[i].value = aryTempSourceOptions[i].value;
		objSourceElement.options[i].selected = false;
	}
}

function selectAllListItems(objTargetElement){
	for(var i = 0; i < objTargetElement.length; i++){
		objTargetElement.options[i].selected = true;
	}
}

function setValue(id, value){
	document.getElementById(id).value = value;
}

function setSelectedToValue(selectID, value){
	var box = document.getElementById(selectID);
	
	for(var i = 0; i < box.options.length; i++){
		if(box.options[i].value == value){
			box.selectedIndex = i;
			return;
		}
	}
}

function checkRadioForValue(name, value){
	var radios = document.getElementsByName(name);
	
	for(var i = 0; i < radios.length; i++){
		if(radios[i].value == value){
			radios[i].checked = "checked";
			return;
		}
	}
}

function createDateControl(name, size, value, timeComp){
	document.write('<span><input type="text" name="' + name + '" size="' + size + '" value="' + value + '" /> ');
	document.write('<img style="cursor:pointer;" onclick="showCal(this.parentNode, ' + timeComp + ');" src="images/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the date"></span>');
}

//this is a temp solution to create the date control without an ID.
function showCal(panel, timeComp){
	var field = panel.childNodes[0];
	
	var cal = new calendar3(field);
	cal.year_scroll = true;
	cal.time_comp = timeComp;
	
	cal.popup();
}

function update(id, type, value){
	switch(type){
		case "html":
			document.getElementById(id).innerHTML = unescape(value);
			break;
		case "value":
			document.getElementById(id).value = unescape(value);
			break;
	}
}

//this should only be called once, so re-enabling submit button here don't feel so awkward.
function updateNotice(message){
	document.getElementById('message').style.display = "block";
	
	update('message', 'html', message);
	colorFade('message','background','00FFFF','FFFFFF',40,30);
	
	//Re-enable submit button.
	var submitButton = document.getElementById('submitButton');
	
	if(submitButton != null){
		submitButton.disabled = false;
		submitButton.value = buttonText;
	}
}

function processForm(){
	document.getElementById('message').style.display = "none";
	disableSubmit(false);
	showLoading();
	
	if(typeof(tinyMCE) != 'undefined')
		tinyMCE.triggerSave();
	
	var xmlhttp=GetXmlHttpObject();
	if(xmlhttp == null){
		//alert("Your browser does not support XMLHTTP!");
		return;
	}
	
	var url = "index.php";
	var params = serialize(document.getElementById('form1'));
	var method = document.getElementById('form1').attributes["method"].value;
	
	xmlhttp.onreadystatechange= function(){
		if(xmlhttp.readyState == 4){
			//alert(xmlhttp.responseText);
			hideLoading();
			
			try{
				eval(xmlhttp.responseText);
			}catch(e){
				//alert(xmlhttp.responseText);
			}
		}
	}
	
	switch(method.toLowerCase()){
		case "get":
			xmlhttp.open("GET", url + "?" + params + "&sid=" + Math.random(), true);
			xmlhttp.send(null);
			break;
		
		case "post":
			xmlhttp.open("POST", url, true);
			
			//Send the proper header information along with the request
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.setRequestHeader("Content-length", params.length);
			xmlhttp.setRequestHeader("Connection", "close");
			
			xmlhttp.send(params + "&sid=" + Math.random());
			break;
	}
}

function processLogin(){
	
	var xmlhttp=GetXmlHttpObject();
	if(xmlhttp == null){
		//alert("Your browser does not support XMLHTTP!");
		return;
	}
	
	var url = "index.php";
	var params = serialize(document.getElementById('loginForm'));
	var method = document.getElementById('loginForm').attributes["method"].value;
	
	xmlhttp.onreadystatechange= function(){
		if(xmlhttp.readyState == 4){
			//alert(xmlhttp.responseText);
			//hideLoading();
			
			try{
				eval(xmlhttp.responseText);
			}catch(e){
				//alert(xmlhttp.responseText);
			}
		}
	}
	
	switch(method.toLowerCase()){
		case "get":
			xmlhttp.open("GET", url + "?" + params + "&sid=" + Math.random(), true);
			xmlhttp.send(null);
			break;
		
		case "post":
			xmlhttp.open("POST", url, true);
			
			//Send the proper header information along with the request
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.setRequestHeader("Content-length", params.length);
			xmlhttp.setRequestHeader("Connection", "close");
			
			xmlhttp.send(params + "&sid=" + Math.random());
			break;
	}
}

//Called when getting menu details.
function performAction(action, method, values){
	document.getElementById('message').style.display = "none";
	showLoading();
	
	var xmlhttp=GetXmlHttpObject();
	if(xmlhttp == null){
		alert("Your browser does not support XMLHTTP!");
		return;
	}
	
	var url = "index.php";
	var params = 'action=' + action + values;
	var method = document.getElementById('form1').method;
	
	xmlhttp.onreadystatechange= function(){
		if(xmlhttp.readyState == 4){
			try{
				eval(xmlhttp.responseText);
			}catch(e){
				//alert(xmlhttp.responseText);
			}
			hideLoading();
		}
	}
	
	switch(method){
		case "get":
			xmlhttp.open("GET", url + "?" + params + "&sid=" + Math.random(), true);
			xmlhttp.send(null);
			break;
		
		case "post":
			xmlhttp.open("POST", url, true);
			
			//Send the proper header information along with the request
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.setRequestHeader("Content-length", params.length);
			xmlhttp.setRequestHeader("Connection", "close");
			
			xmlhttp.send(params + "&sid=" + Math.random());
			break;
	}
}

function serialize(form){
  if(!form || !form.elements) return;

  var serial = [], i, j, first;
  var add = function(name, value){
    serial.push(encodeURIComponent(name) + '=' + encodeURIComponent(value));
  }

  var elems = form.elements;
  for(i = 0; i < elems.length; i += 1, first = false){
    if(elems[i].name != null && elems[i].name.length > 0){ /* don't include unnamed elements */
      switch (elems[i].type){
        case 'select-one': first = true;
        case 'select-multiple':
          for(j = 0; j < elems[i].options.length; j += 1)
            if(elems[i].options[j].selected){
              add(elems[i].name, elems[i].options[j].value);
              if(first) break; /* stop searching for select-one */
            }
          break;
        case 'checkbox':
        case 'radio': if(!elems[i].checked) break; /* else continue */
        default: add(elems[i].name, elems[i].value); break;
      }
    }
  }

  return serial.join('&');
}

function GetXmlHttpObject(){
	if(window.XMLHttpRequest){
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if(window.ActiveXObject){
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function addComponent(id){
	var output = document.getElementById(id + "Output");
	var component = document.getElementById(id + "Template").cloneNode(true);
	
	component.setAttribute("id", "");
	component.style.display = "block";
	
	output.appendChild(component);
	
	return component;
}
function removeSelf(self){
	self.parentNode.removeChild(self);
}
function getChildrenByName(element, tagName, nameOfChildElement){
	var list = new Array();
	var nodeList = element.getElementsByTagName(tagName);
	
	for(var i = 0; i < nodeList.length; i++)
		if(nodeList[i].getAttribute("name") == nameOfChildElement)
			list[list.length] = nodeList[i];
	
	return list;
}
function setObjectSelectedToValue(box, value){
	for(var i = 0; i < box.options.length; i++){
		if(box.options[i].value == value){
			box.selectedIndex = i;
			return;
		}
	}
}
function checkObjectsRadioForValue(radios, value){
	for(var i = 0; i < radios.length; i++){
		if(radios[i].value == value){
			radios[i].checked = "checked";
			return;
		}
	}
}

var buttonText = "";
function disableSubmit(submitAfterDisable){
	//Temporarily disable submit button till end of processing.
	var submitButton = document.getElementById('submitButton');
	showLoading();
	
	if(submitButton != null){
		submitButton.disabled = true;
		buttonText = submitButton.value;
		submitButton.value = "Processing...";
	}
	
	if(submitAfterDisable)
		document.getElementById("form1").submit();
}
function uploadDone(){ //Function will be called when iframe is loaded
	var ret = frames['upload_target'].document.getElementsByTagName("body")[0].innerHTML;
	if(ret == "") return;
	
	try{
		eval(ret);
	}catch(e){
		alert(ret);
	}
	restoreButton();
	hideLoading();
}

function fnStartInit(){
   if(uploadTarget.readyState=="complete")
	  uploadDone();
}

function restoreButton(){
	var submitButton = document.getElementById('submitButton');
	
	if(submitButton != null){
		submitButton.disabled = false;
		submitButton.value = buttonText;
	}
}

function showLoading(){
	document.getElementById("loading").style.display = "block";
}

function hideLoading(){
	document.getElementById("loading").style.display = "none";
}
