// JavaScript Document

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

String.prototype.trim = function() {
	return this.ltrim().rtrim();
};


function rmObj(myObj){
	if(myObj){
		if(myObj.hasChildNodes()){
			for(var i = 0;i < myObj.childNodes.length;i++){
				rmObj(myObj.childNodes[i]);
			}
		}
		myObj.parentNode.removeChild(myObj);
	}
}

function preload_images(){
	MM_preloadImages("/__ezpost/images/nav_off_content.gif","/__ezpost/images/nav_on_content.gif","/__ezpost/images/nav_off_images.gif","/__ezpost/images/nav_on_images.gif","/__ezpost/images/nav_off_settings.gif","/__ezpost/images/nav_on_settings.gif","/__ezpost/images/nav_off_users.gif","/__ezpost/images/nav_on_users.gif","/__ezpost/images/nav_off_forms.gif","/__ezpost/images/nav_on_forms.gif","/__ezpost/images/nav_off_templates.gif","/__ezpost/images/nav_on_templates.gif","/__ezpost/images/nav_off_publications.gif","/__ezpost/images/nav_on_publications.gif","/__ezpost/images/btn_x_close.gif","/__ezpost/images/btn_save.gif","/__ezpost/images/btn_cancel.gif","/__ezpost/images/btn_add_group.gif");
}

function isEmail(s){ 
    if (s.trim() == "") return false;
    
    var i = 1;
    var sLength = s.length;

	while ((i < sLength) && (s.charAt(i) != "@")){ 
		i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    while ((i < sLength) && (s.charAt(i) != ".")){ 
		i++
    }

    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function focusSearch(searchBox){
	if(searchBox.value == "SEARCH"){
		searchBox.value = "";	
	}
}

function blurSearch(searchBox){
	if(searchBox.value ==""){
		searchBox.value = "SEARCH";	
	}		
}

function focusField(inputBox, txtDefault){
	if(inputBox.value == txtDefault){
		inputBox.value = "";	
	}
}

function blurField(inputBox, txtDefault){
	if(inputBox.value == ""){
		inputBox.value = txtDefault;	
	}		
}

function focusPassword(inputBox, txtDefault){
	if(inputBox.value == ""){
		inputBox.parentNode.getElementsByTagName("INPUT")[0].value = "";	
	}
}

function blurPassword(inputBox, txtDefault){
	if(inputBox.value == ""){
		inputBox.parentNode.getElementsByTagName("INPUT")[0].value = txtDefault;	
	}		
}

function toggleSectionContentFilter(filterButton){
	if(filterButton.className == "section_content_option"){
		filterButton.className = "section_content_option_selected";
	}
	else{
		filterButton.className = "section_content_option";
	}
}

function toggleSubgroup(parentGroup){
	var subDivs = parentGroup.getElementsByTagName("DIV");
	
	for(var i = 0;i<subDivs.length;i++){
		if(subDivs[i].className == "groups_box_group_collection_subgroup"){
			if(subDivs[i].style.display && subDivs[i].style.display == "block"){
				subDivs[i].style.display = "none";
			}
			else{
				subDivs[i].style.display = "block";
			}
		}
	}
}

function clickCheckbox(myCheckbox){
	if(myCheckbox.src.indexOf("/__ezpost/images/checkbox_off.gif") != -1){
		myCheckbox.src = "/__ezpost/images/checkbox_on.gif";
		myCheckbox.parentNode.getElementsByTagName("INPUT")[0].checked = true;
	}
	else{
		myCheckbox.src = "/__ezpost/images/checkbox_off.gif";
		myCheckbox.parentNode.getElementsByTagName("INPUT")[0].checked = false;
	}
}

function updateNavOver(myImage){
	myImage.src = myImage.src.replace("off","on");	
}

function updateNavOut(myImage){
	myImage.src = myImage.src.replace("on","off");	
}

function selectOptionOver(myOption){
	myOption.className = "select_box_option_selected";	
}

function selectOptionOut(myOption){
	myOption.className = "select_box_option";	
}

function showSelectBox(myShowClick){
	var divs = myShowClick.parentNode.parentNode.parentNode.getElementsByTagName("DIV");
	for(var i=0;i<divs.length;i++){
		if(divs[i].className == "select_box"){
			mySelectBox = divs[i];				
		}
	}
	window.setTimeout("mySelectBox.style.display = \"block\";", 0);
	return false;
}

function selectChoose(myOption){
	var myChoice = myOption.getElementsByTagName("SPAN")[0].innerHTML;
	myOption.parentNode.style.display = "none";
	if(myChoice.trim() == "NONE" || myChoice.trim() == ""){
		myOption.parentNode.parentNode.getElementsByTagName("A")[0].innerHTML = "Select One...";
	}
	else{
		myOption.parentNode.parentNode.getElementsByTagName("A")[0].innerHTML = myChoice;
	}
	
	if(arguments[1]){
		document.getElementById(arguments[1]).value = arguments[2];
	}
}

function click_clear_select_windows(){
	document.getElementsByTagName("BODY")[0].onclick =  function() {clear_select_windows();};
}

function clear_select_windows(){
	var divs = document.getElementsByTagName("DIV");
	for(var i=0;i < divs.length;i++){
		if(divs[i].className == "select_box"){
			divs[i].style.display = "none";	
		}
	}
}

function toggleButton(myButton){
	if(myButton.className == "form_area_input_selection"){
		myButton.className = "form_area_input_selection_selected"
	}
	else{
		myButton.className = "form_area_input_selection"	
	}
	
	
	var other_buttons = myButton.parentNode.getElementsByTagName("DIV");
	for(var i=0;i < other_buttons.length;i++){
		if(other_buttons[i].className == "form_area_input_selection_selected" && other_buttons[i] != myButton){
			other_buttons[i].className = "form_area_input_selection";
		}
	}
}

function checkArchive(myButton){
	if(myButton.innerHTML == "Yes" && myButton.className == "form_area_input_selection_selected"){
		document.getElementById("archive_extras").style.display = "block";	
	}
	else{
		document.getElementById("archive_extras").style.display = "none";	
	}
}

function RTE_update(){
	for(var i = 0;i < rte_elements.length; i++){
		document.getElementById(rte_elements[i]).value = tinyMCE.getContent(rte_elements[i]).replace(/<\/p>/g,"</p>\n\n").replace(/<br \/>/g,"<br />\n");
	}
}

function RTE_source_toggle(toggle_button,field_name){
	toggle_button.parentNode.parentNode.getElementsByTagName('TABLE')[0].style.display='none';
	toggle_button.parentNode.parentNode.getElementsByTagName('TEXTAREA')[0].style.display='block';
	toggle_button.parentNode.parentNode.getElementsByTagName('TEXTAREA')[0].value = tinyMCE.getContent(field_name).replace(/<\/h3>/g,"</h3>\n\n").replace(/<\/h2>/g,"</h2>\n\n").replace(/<\/h1>/g,"</h1>\n\n").replace(/<\/p>/g,"</p>\n\n").replace(/<\/ul>/g,"</ul>\n\n").replace(/<br \/>/g,"<br />\n").replace(/<\/li>/g,"</li>\n");
	
	toggle_button.getElementsByTagName("IMG")[0].src = toggle_button.getElementsByTagName("IMG")[0].src.replace("off","on");
	toggle_button.parentNode.getElementsByTagName("IMG")[0].src = toggle_button.parentNode.getElementsByTagName("IMG")[0].src.replace("on","off");
	toggle_button.parentNode.parentNode.getElementsByTagName('TEXTAREA')[0].focus();
	
	return false;
}

function RTE_visual_toggle(toggle_button,field_name){
	toggle_button.parentNode.parentNode.getElementsByTagName('TEXTAREA')[0].style.display='none';
	toggle_button.parentNode.parentNode.getElementsByTagName('TABLE')[0].style.display='block';
	tinyMCE.setContent(toggle_button.parentNode.parentNode.getElementsByTagName('TEXTAREA')[0].value);

	toggle_button.getElementsByTagName("IMG")[0].src = toggle_button.getElementsByTagName("IMG")[0].src.replace("off","on");
	toggle_button.parentNode.getElementsByTagName("IMG")[1].src = toggle_button.parentNode.getElementsByTagName("IMG")[1].src.replace("on","off");
	toggle_button.parentNode.parentNode.getElementsByTagName('TEXTAREA')[0].blur();
	
	return false;
}

function showPopUp(o){
	var div = document.createElement("DIV");
	div.style.width = "100%";
	div.style.height = "100%";
	div.style.position = "absolute";
	div.style.top = "0px";
	div.style.left = "0px";
	div.id = "popup_framework";
	
	var bg = document.createElement("DIV");
	bg.style.width = "100%";
	bg.style.height = "100%";
	bg.style.position = "absolute";
	bg.style.top = "0px";
	bg.style.left = "0px";
	bg.style.backgroundColor = "#FFFFFF";
	bg.style.opacity="0.90";
	bg.style.filter="alpha(opacity=90)";

	div.appendChild(bg);
	
	var table = document.createElement("TABLE");
	table.style.width = "100%";
	table.style.height = "100%";
	table.border = 0;
	table.cellPadding = 0;
	table.cellSpacing = 0;
	table.style.position = "absolute";
	if(window.innerHeight){
		table.style.display = "table";
	}
	else{
		table.style.display = "block";	
	}
	table.style.top = "0";
	table.style.left = "0";

	div.appendChild(table);
	
	var tr = document.createElement("TR");
	tr.style.width = "100%";
	tr.style.height = "100%";

	table.appendChild(tr);
	
	var td = document.createElement("TD");
	td.style.width = "100%";
	td.style.height = "100%";
	td.vAlign = "middle";
	td.style.textAlign = "center";

	tr.appendChild(td);
	
	td.appendChild(o);
	
	document.getElementsByTagName("BODY")[0].appendChild(div);
	if(!window.innerHeight){
		document.getElementById("popup_framework").innerHTML = document.getElementById("popup_framework").innerHTML;
	}

	var objEmbeds = document.getElementsByTagName("EMBED");
	for(var i=0; i < objEmbeds.length; i++){
		for(var j=0; j<objEmbeds[i].attributes.length; j++){
			if(objEmbeds[i].attributes[j].name == "wmode"){
				objEmbeds[i].attributes[j].value = "transparent";
			}
		}
	}

	document.getElementsByTagName("HTML")[0].style.overflow = "hidden";
	if(window.innerHeight){
		document.getElementsByTagName("BODY")[0].style.overflow = "hidden";
	}
	window.scrollTo(0,0);
}

function closePopup(){
	var divs = document.getElementsByTagName("DIV");
	for(var i=0;i < divs.length;i++){
		if(divs[i].className == "delete_popup"){
			divs[i].style.display = "none";
		}
	}
	
	rmObj(document.getElementById("popup_framework"));
	
	document.getElementsByTagName("HTML")[0].style.overflow = "auto";
	if(window.innerHeight){
		document.getElementsByTagName("BODY")[0].style.overflow = "auto";
	}
	
	var objEmbeds = document.getElementsByTagName("EMBED");
	for(var i=0; i < objEmbeds.length; i++){
		for(var j=0; j<objEmbeds[i].attributes.length; j++){
			if(objEmbeds[i].attributes[j].name == "wmode"){
				objEmbeds[i].attributes[j].value = "window";
			}
		}
	}	
}

function showDeletePopup(myButton){
	if(myButton.getElementsByTagName("DIV")[0]){
		if(myButton.getElementsByTagName("DIV")[0].className == "delete_popup"){
			myButton.getElementsByTagName("DIV")[0].style.display = "block";	
		}
	}
}

function hideDeletePopup(myButton){
	if(myButton.getElementsByTagName("DIV")[0]){
		if(myButton.getElementsByTagName("DIV")[0].className == "delete_popup"){
			myButton.getElementsByTagName("DIV")[0].style.display = "none";	
		}
	}
}

function showDeleteSubPopup(myButton){
	if(myButton.getElementsByTagName("SPAN")[0]){
		myButton.getElementsByTagName("SPAN")[0].style.display = "inline";	
	}
}

function hideDeleteSubPopup(myButton){
	if(myButton.getElementsByTagName("SPAN")[0]){
		myButton.getElementsByTagName("SPAN")[0].style.display = "none";	
	}
}

/*function reorderPosts(){		//Ascending
	var myForm = document.getElementById("content_action");
	var myElements = myForm.elements;
	var myMoveElements = Array();
	var lowestValue = "na";
	for(var i=0; i < myElements.length; i++){
		if(myElements[i].name.indexOf("move_") != -1){
			myMoveElements[myMoveElements.length] = myElements[i];
		}
	}
	myElements = myMoveElements;

	for(var i=0;i < myElements.length;i++){
		if(lowestValue == "na"){
			lowestValue = parseInt(myElements[i].value);
		}
		else if(parseInt(myElements[i].value) < lowestValue){
			lowestValue = parseInt(myElements[i].value);	
		}
	}
	
	if(lowestValue < 1){
		lowestValue = 1;	
	}

	for(var i=0;i < myElements.length;i++){
		myElements[i].value = i + lowestValue;	
	}
}*/

function reorderPosts(){  //Descending
	var myForm = document.getElementById("content_action");
	var myElements = myForm.elements;
	var myMoveElements = Array();
	var highestValue = "na";
	for(var i=0; i < myElements.length; i++){
		if(myElements[i].name.indexOf("move_") != -1){
			myMoveElements[myMoveElements.length] = myElements[i];
		}
	}
	myElements = myMoveElements;

	for(var i=0;i < myElements.length;i++){
		if(highestValue == "na"){
			highestValue = parseInt(myElements[i].value);
		}
		else if(parseInt(myElements[i].value) > highestValue){
			highestValue = parseInt(myElements[i].value);	
		}
	}
	
	if(highestValue < 1){
		highestValue = 1;	
	}
	if(highestValue < myElements.length){
		highestValue = myElements.length;	
	}

	for(var i=0;i < myElements.length;i++){
		myElements[i].value = highestValue - i;	
	}
	
	submitOrder(myElements);
}

function is_radio_selected(val) {
	var isSelected = false;

	if(!val){
	   return false;
    }
	
	if(!val.length){
		if(val.checked){
			isSelected = true;	
		}
	}

	for (i=0; i<val.length; i++) {
		var currVal = val[i];
		if (currVal.checked) {
			isSelected = true;
			break;
		}
	}

    if (!isSelected) {
        return false;
    } else return true;
}	  

function moveArrayItem(myArray, val_init, val_target){
	var init_pos = -1;
	var target_pos = -1;
	
	for(var i=0;i<myArray.length;i++){
		if(myArray[i] == val_init){
			init_pos = i;
			i = myArray.length;
		}
	}
	for(var i=0;i<myArray.length;i++){
		if(myArray[i] == val_target){
			target_pos = i;
			i = myArray.length;
		}
	}

	if(init_pos == -1 || target_pos == -1 || init_pos == target_pos){
		return myArray;
	}
	
	var targetArray = Array();
	targetArray[0] = val_init;
	
	myArray.splice(init_pos,1);

	for(var i=0;i<myArray.length;i++){
		if(myArray[i] == val_target){
			target_pos = i;
			i = myArray.length;
		}
	}

	if(target_pos < init_pos){
		myArray.splice(target_pos,0,targetArray);
	}
	else if(target_pos >= init_pos){
		myArray.splice(target_pos + 1,0,targetArray);
	}
	
	return myArray;
}