// $Header: /hsphere/local/home/hszeto/cvsrep/avire/lib/tiki-js.js,v 1.6 2006/09/29 04:47:25 hszeto Exp $

function chgArtType() {
  if(document.getElementById('articletype').value=='Article') {
    document.getElementById('isreview').style.display="none";
  } else {
    document.getElementById('isreview').style.display="block";
  }
}

function chgTrkFld() {
  if(document.getElementById('trkfldtype').value=='d') {
    document.getElementById('trkfldoptions').style.display="block";	
  } else {
    document.getElementById('trkfldoptions').style.display="none";		
  }
}


function setMenuCon(foo1,foo2) {
  document.getElementById('menu_url').value=foo1;
  document.getElementById('menu_name').value=foo2;
}

function genPass(w1,w2,w3){
  vo="aeiouAEU";
  co="bcdfgjklmnprstvwxzBCDFGHJKMNPQRSTVWXYZ0123456789_$%#";
  s=Math.round(Math.random());
  l=8;
  p='';
  for (i=0; i < l; i++)
  {
    if (s)
    {
      letter=vo.charAt(Math.round(Math.random() * (vo.length - 1) ));
      s=0;
    } else
    {
      letter=co.charAt(Math.round(Math.random() * (co.length - 1) ));
      s=1;
    }
    p=p + letter;
  }
  document.getElementById(w1).value=p;
  document.getElementById(w2).value=p;
  document.getElementById(w3).value=p;
}

function setUserModule(foo1) {
  document.getElementById('usermoduledata').value=foo1;
}

function setSomeElement(fooel,foo1) {
if (foo1!="")
  document.getElementById(fooel).value=foo1;
//  document.getElementById(fooel).value=document.getElementById(fooel).value +foo1;
  
  //"\r\n"
}

function replaceSome(fooel,what,repl) {
  document.getElementById(fooel).value = document.getElementById(fooel).value.replace(what,repl);
}

function replaceLimon(vec) {
  document.getElementById(vec[0]).value = document.getElementById(vec[0]).value.replace(vec[1],vec[2]);
}

function setUserModuleFromCombo(id) {
  document.getElementById('usermoduledata').value=document.getElementById('usermoduledata').value + document.getElementById(id).options[document.getElementById(id).selectedIndex].value;
  //document.getElementById('usermoduledata').value='das';
}
function setUserModuleFromCombo_tinymce(id) {
  tinyMCE.execCommand('mceInsertContent',false,document.getElementById(id).options[document.getElementById(id).selectedIndex].value);
  //document.getElementById('usermoduledata').value='das';
}

function show(foo) {
    document.getElementById(foo).style.display="block";
}

function hide(foo) {
    document.getElementById(foo).style.display="none";
}

function flip(foo) {
  if( document.getElementById(foo).style.display == "none") {
    show(foo);
  } else {
		if( document.getElementById(foo).style.display == "block") {
			hide(foo);
		} else {
			show(foo);
		}
	}
}

function toggle(foo) {
  if( document.getElementById(foo).style.display == "none") {
    show(foo);
    setCookie(foo, "o");
    HS_setArrowState(foo);
  } else {
  	if( document.getElementById(foo).style.display == "block") {
	    hide(foo);
	    setCookie(foo, "c");
	    HS_setArrowState(foo);
	} else {
	    show(foo);
	    setCookie(foo, "o");
	    HS_setArrowState(foo);
	}
  }
}

function tikitabs(focus,max) {
	for (var i = 1; i < max; i++) {
		var tabname = 'tab' + i;
		var content = 'content' + i;
		//if (document.getElementById(tabname) != 'undefined') {
			if (i == focus) {
				//show(tabname);
				show(content);
				setCookie('tab',focus);
				//document.getElementById(tabname).style.borderColor="black";
				document.getElementById(tabname).className="tabmarkon";
			} else {
				//hide(tabname);
				hide(content);
				//document.getElementById(tabname).style.borderColor="white";
				document.getElementById(tabname).className="tabmark";
			}
		//}
	}
}

function ikit_tabs(tabprefix,focus,max) {
	for (var i = 1; i < max; i++) {
		var tabname = tabprefix + i;
		var content = tabprefix+'content' + i;
		
		//if (document.getElementById(tabname) != 'undefined') {
			if (i == focus) {
				//show(tabname);
				show(content);
				setCookie(tabprefix,focus);
				//document.getElementById(tabname).style.borderColor="black";
				document.getElementById(tabname).className="tabmarkon";
			} else {
				//hide(tabname);
				hide(content);
				//document.getElementById(tabname).style.borderColor="white";
				document.getElementById(tabname).className="tabmark";
			}
		//}
	}
}


// From
// http://www.webmasterworld.com/forum91/296.htm				
function select(idname) {
	if(document.getElementById) { 
		document.getElementById(idname).className="tablink2on"; 
	} 
		
	else if(document.all) { 
		document.all[idname].className="tablink2on"; 
	} 
}
function unselect(idname) {
	if(document.getElementById) { 
		document.getElementById(idname).className="tablink2"; 
	} 
	else if(document.all) { 
		document.all[idname].className="tablink2";
	} 
}

function showzone(foo) {
	show(foo+"-zone");
	select(foo+"-tab");
}
function hidezone(foo) {
	hide(foo+"-zone");
	unselect(foo+"-tab");
}

function setfoldericonstate(foo) {
	pic = new Image();
	if (getCookie(foo) == "o")
 	{
		pic.src = "img/icons/ofo.gif";
	} else {
		pic.src = "img/icons/fo.gif";
	}
	document.getElementsByName(foo+'icn')[0].src = pic.src;
}
function HS_setArrowState(foo) {
	pic = new Image();
	if (getCookie(foo) == "o")
 	{
		pic.src = "images/t-o.gif";
	} else {
		pic.src = "images/t-c.gif";
	}
	document.getElementById(foo).parentNode.getElementsByTagName("a")[0].getElementsByTagName("img")[0].src = pic.src;
}

function setfolderstate(foo) {
	if (getCookie(foo) == "o")
 	{
		show(foo);
	} else {
		hide(foo);
	}
	setfoldericonstate(foo);
}

function icntoggle(foo) {
  if( document.getElementById(foo).style.display == "none") {
    show(foo);
    setCookie(foo, "o");
  } else {
    hide(foo);
    setCookie(foo, "c");
  }
  setfoldericonstate(foo);
}

//
// set folder icon state during page load
//
function setFolderIcons() { 
  var elements      = document.forms[the_form].elements[elements_name];
  var elements_cnt  = (typeof(elements.length) != 'undefined') ? elements.length : 0;
  if (elements_cnt) {
    for (var i = 0; i < elements_cnt; i++) {
      elements[i].checked = document.forms[the_form].elements[switcher_name].checked;
    } 
  } else {
    elements.checked        = document.forms[the_form].elements[switcher_name].checked;;
  } // end if... else
  return true;
} // setFolderIcons()

// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

//
// Expand/collapse lists
//
function flipWithSign(foo) {
 if( document.getElementById(foo).style.display == "none") {
  show(foo);
  collapseSign("flipper"+foo);
  setCookie(foo,"o");
 } else {
  hide(foo);
  expandSign("flipper"+foo);
  setCookie(foo,"c");
 }
}

// set the state of a flipped entry after page reload
function setFlipWithSign(foo)
{
  if (getCookie(foo) == "o") {
    collapseSign("flipper"+foo);
    show(foo);
  }
  else {
    expandSign("flipper"+foo);
    hide(foo);
  }
}

function expandSign(foo) {
 document.getElementById(foo).firstChild.nodeValue="[+]";
}

function collapseSign(foo) {
 document.getElementById(foo).firstChild.nodeValue="[-]";
} // flipWithSign()

//
// Check / Uncheck all Checkboxes
//
function switchCheckboxes(the_form,elements_name,switcher_name) { 
  // checkboxes need to have the same name elements_name
  // e.g. <input type="checkbox" name="my_ename[]">, will arrive as Array in php.
  var elements      = document.forms[the_form].elements[elements_name];
  var elements_cnt  = (typeof(elements.length) != 'undefined') ? elements.length : 0;
  if (elements_cnt) {
    for (var i = 0; i < elements_cnt; i++) {
      elements[i].checked = document.forms[the_form].elements[switcher_name].checked;
    } 
  } else {
    elements.checked        = document.forms[the_form].elements[switcher_name].checked;;
  } // end if... else
  return true;
} // switchCheckboxes()

//
// Set client offset (in minutes) to a cookie to avoid server-side DST issues
// Added 7/25/03 by Jeremy Jongsma (jjongsma@tickchat.com)
//
var expires = new Date();
var offset = -(expires.getTimezoneOffset() * 60);
expires.setFullYear(expires.getFullYear()+1);
setCookie("tz_offset", offset, expires, "/");


// function added for use in navigation dropdown
// example :
// <select name="anything" onchange="go(this);">
// <option value="http://tikiwiki.org">tikiwiki.org</option>
// </select>
function go(o){
	if (o.options[o.selectedIndex].value != "") {
		location = o.options[o.selectedIndex].value;
		o.options[o.selectedIndex] = 1;
	}
	return false;
}

// AVIRE

/*
function upload_or_save() {
	var instructionsDisplay = document.getElementById("instructions");
	if (document.uploadForm.userfile1.value!= "") {
		instructionsDisplay.innerHTML = "<h3>Enter details for the upload image</h3>Fields below are pre-filled with values from the selected image.<br />";
		document.uploadForm.upload.value = "Upload";
	} else {
		instructionsDisplay.innerHTML = "<h3>Edit the selected image</h3>";
		document.uploadForm.upload.value = "Save"
	}

}
*/
function updateImageTitleField(list) {
	var imageTitleField = document.getElementById("imageTitleField");
	var imageTitleDisplay = document.getElementById("imageTitleDisplay");
//	var work = 'cat_categories_work[]';
	var work = 'work';

	var selectedWork = document.uploadForm[work].options[document.uploadForm[work].selectedIndex].text;
//	var selectedWorkValue = document.uploadForm[work].options[document.uploadForm[work].selectedIndex].value;
	var abbrevWorkName = selectedWork.split("::");
	
	var new_work = document.uploadForm.new_work.value;
	if (new_work!= "") {
		var titleValue = new_work;
	} else {
		if (selectedWork == "Step 3. Select a Work") {
			titleValue = "";
		} else {
			var titleValue = abbrevWorkName[abbrevWorkName.length-1];
		}
	}

	var aspect = 'cat_categories_aspect[]';
	var aspect_sel = getSelectedCheckboxes(document.uploadForm[aspect]);
	//var titleValue = abbrevWorkName[abbrevWorkName.length-1];
	if (aspect_sel!= "") {
		titleValue += " - "+aspect_sel;
	}
	imageTitleField.value = titleValue;
	imageTitleDisplay.innerHTML = titleValue;
}

function getSelectedItems (list) {
	var selectedItems ="";
	for (var i=0;i<list.length; i++) {
		if (list.options[i].selected) {
			selectedItems += list.options[i].text+', ';
		}
	}
	return selectedItems.substring(0,selectedItems.length-2);
}

function getSelectedCheckboxes (checkboxes) {
	var selectedItems ="";
	for (var i=0;i<checkboxes.length; i++) {
		if (checkboxes[i].checked) {
			// Check boxes don't have .text value, so use nextSibling to 
			// get the text node.
			selectedItems += checkboxes[i].nextSibling.nodeValue+', ';
		}
	}
	return selectedItems.substring(0,selectedItems.length-2);
}

function selectByValue (list, v) {
	for (var i=0;i<list.length; i++) {
		if (list.options[i].value == v) {
			list.options.selectedIndex = i;
			//alert(v+" "+ i);
			break;
		}
	}
	return i;
}


// Start of Exhibition Criteria

// Called when Exhibition Criteria Field is selected e.g. Image title, Work title, Date catalogued
function updateCriteriaConditionsDropDown(listobj) {
	var selectedField = listobj.options[listobj.selectedIndex].value;  // Which field selected?
		
	// Work out which table row this drop down sits on
	var criteria_row = listobj.parentNode.parentNode;
	// Get the sibling conditions drop down. Note that all criertia fields are named the same way.
	var conditions_cell = criteria_row.getElementsByTagName('td')[1];
	var conditions_drop_down = conditions_cell.getElementsByTagName('select')[0];
	var selectedCondition = conditions_drop_down.options[conditions_drop_down.selectedIndex].value;
	
	// Rebuild the conditions drop down.
	conditions_drop_down.length=0; // Clear the drop down
	for(i=0;i<smart_criteria[selectedField]["conditions"].length;i++) {
		conditions_drop_down.options[i] = new Option(smart_criteria[selectedField]["conditions"][i],smart_criteria[selectedField]["conditions"][i]);
	}
	
	updateCriteriaValuesDropDown(conditions_drop_down);
}	
// Called when Exhibition Conditions Field is selected e.g. is, is not, contains
function updateCriteriaValuesDropDown(listobj) {
	var selectedCondition = listobj.options[listobj.selectedIndex].value;  // Which condition selected?
	
	// Work out which table row this drop down sits on
	var criteria_row = listobj.parentNode.parentNode;
	
	// Work out which field selected
	var fieldname_cell = criteria_row.getElementsByTagName('td')[0];
	var fieldname_drop_down = fieldname_cell.getElementsByTagName('select')[0];
	var selectedField = fieldname_drop_down.options[fieldname_drop_down.selectedIndex].value;
		
	var value_cell = criteria_row.getElementsByTagName('td')[2];
	input1_div = value_cell.getElementsByTagName("div")[0];
	select_div = value_cell.getElementsByTagName("div")[1];
	input2_div = value_cell.getElementsByTagName("div")[2];
	input1 = input1_div.getElementsByTagName("input")[0];
	select1 = select_div.getElementsByTagName("select")[0];
	input2 = input2_div.getElementsByTagName("input")[0];
	
	if (smart_criteria[selectedField]["conditions_input_type"][selectedCondition]=="input") {	
		input1_div.style.display="block";
		select_div.style.display="none";
		input2_div.style.display="none";
		
		if (smart_criteria[selectedField]["fieldtype"]=="date") {
			input1.setAttribute("size","12");
			var today = new Date();
			var yyyy = today.getFullYear();
			var mm = today.getMonth()+1;
			var dd = today.getDate();
			input1.setAttribute("value",yyyy+"/"+mm+"/"+dd);
		} else {
			input1.setAttribute("size","18");		
		}
	}
	if (smart_criteria[selectedField]["conditions_input_type"][selectedCondition]=="select") {
		input1_div.style.display="none";
		select_div.style.display="block";
		input2_div.style.display="none";
		
		select1.length=0;
		select1.options[0] = new Option("Any", "");
		for(i=1;i<=drop_downs[selectedField].length;i++) {
			select1.options[i] = new Option(drop_downs[selectedField][i],drop_downs[selectedField][i]);
		}
	}
	if (smart_criteria[selectedField]["conditions_input_type"][selectedCondition]=="input2") {
		input1_div.style.display="inline";
		select_div.style.display="none";
		input2_div.style.display="inline";
		
		if (smart_criteria[selectedField]["fieldtype"]=="date") {
			input2.setAttribute("size","12");
			var today = new Date();
			var yyyy = today.getFullYear();
			var mm = today.getMonth()+1;
			var dd = today.getDate();
			input1.setAttribute("value",yyyy+"/"+mm+"/"+dd);
			input2.setAttribute("value",yyyy+"/"+mm+"/"+dd);
		} else {
			input2.setAttribute("size","18");
		}

	}
}	

// Removes a criteria row
function delCriteria(delButton) {
	// Work out which table row this button sits on
	var criteria_row = delButton.parentNode.parentNode;
	var tablebody = criteria_row.parentNode;
	var value_cell = criteria_row.getElementsByTagName('td')[2];
	input1_div = value_cell.getElementsByTagName("div")[0];
	select_div = value_cell.getElementsByTagName("div")[1];
	input2_div = value_cell.getElementsByTagName("div")[2];
	input1 = input1_div.getElementsByTagName("input")[0];
	select1 = select_div.getElementsByTagName("select")[0];
	input2 = input2_div.getElementsByTagName("input")[0];
	input1.value="";
	input2.value="";
	select1.options.length=0;
	select1.options[0]= new Option("","");
	if (tablebody.rows.length>1) {
		tablebody.removeChild(criteria_row);
	}
	if (tablebody.rows.length == 1) {
		tablebody.rows[0].cells[3].getElementsByTagName('input')[0].disabled=true;
		document.getElementById('criteria_match_one').style.display="block";
		document.getElementById('criteria_match_multiple').style.display="none";
		
	}
}

// Adds a criteria row
function addCriteria(addButton) {
	// Work out which table row this button sits on
	var criteria_row = addButton.parentNode.parentNode;
//	alert(criteria_row.innerHTML);
	var tablebody = criteria_row.parentNode;
	// Enable the Delete button first (for the first row only)
	if (tablebody.rows.length == 1) {
		tablebody.rows[0].cells[3].getElementsByTagName('input')[0].disabled=false;
		document.getElementById('criteria_match_one').style.display="none";
		document.getElementById('criteria_match_multiple').style.display="block";
	}
	
	// Clone the row
	var new_row_temp = criteria_row.cloneNode(true);
	var new_row=tablebody.appendChild(new_row_temp);
//	new_row.getElementsByTagName('td')[0].getElementsByTagName('input')[0].setAttribute("type","hidden");
	var fieldname_cell = new_row.getElementsByTagName('td')[0];
	var fieldname_dropdown = fieldname_cell.getElementsByTagName('select')[0];
	updateCriteriaConditionsDropDown(fieldname_dropdown);

}

// End of Exhibition Criteria

function updateImageSubjectField(list) {
	var imageSubject = document.getElementById("imageSubject");
	var aspect = 'cat_categories_aspect[]';
	var feature = 'cat_categories_feature[]';
	var form = 'cat_categories_form[]';
	var structure = 'cat_categories_structure[]';
	var material = 'cat_categories_material[]';
	var performance = 'cat_categories_performance[]';
	
	var aspect_sel = getSelectedCheckboxes(document.uploadForm[aspect]);
	var feature_sel = getSelectedCheckboxes(document.uploadForm[feature]);
	var form_sel = getSelectedCheckboxes(document.uploadForm[form]);
	var structure_sel = getSelectedCheckboxes(document.uploadForm[structure]);
	var material_sel = getSelectedCheckboxes(document.uploadForm[material]);
	var performance_sel = getSelectedCheckboxes(document.uploadForm[performance]);
	imageSubject.value ="";
	if (aspect_sel!= "") imageSubject.value =  aspect_sel + "; ";
	if (feature_sel!= "") imageSubject.value +=  feature_sel + "; ";
	if (form_sel!= "") imageSubject.value += form_sel + "; ";
	if (structure_sel!= "") imageSubject.value +=  structure_sel + "; ";
	if (material_sel!= "") imageSubject.value +=  material_sel + "; ";
	if (performance_sel!= "") imageSubject.value +=  performance_sel + "; ";
}

// Called from when a country is selected.
function updateCityWorksDropDown() {
	var cityDropDown = document.uploadForm.city;
	var selectedCountry = document.uploadForm.country[document.uploadForm.country.selectedIndex].value;
	if (selectedCountry=="") {
		selectedCountry = "anythingthatwontmatch";
	}
//	alert(selectedCountry);
	var allCitiesDiv = document.getElementById("allcities");
	var allCities = allCitiesDiv.getElementsByTagName("div");
	cityDropDown.length=0; // Clear the drop down
	cityDropDown.options[0] = new Option("Step 2. Select a City/Town","");
	
	j=1;
	for(i=0;i<allCities.length;i++) {
		country_city = allCities[i].innerHTML.split(",");
		var re = new RegExp(selectedCountry);
		// re.compile doe snot work in safari 1.0
		//re.compile(selectedCountry,"gi");
		if (country_city[0].search(re) != -1) {
			cityDropDown.options[j] = new Option(country_city[1],country_city[1]);
			j++;
		}
	}
	cityDropDown.options[j] = new Option("[add new City]","new");
	updateWorksDropDown();
}	

function updateWorksDropDown() {
	var workDropDown = document.uploadForm.work;
	var cityDropDown = document.uploadForm.city;
	var selectedCountry = document.uploadForm.country[document.uploadForm.country.selectedIndex].value;
	var selectedCity = "";
	if (cityDropDown.length>0) {
		selectedCity = cityDropDown[cityDropDown.selectedIndex].value;
		//alert(selectedCity);
	}
	if (selectedCity=="") {
		selectedCity = "anythingthatwontmatch";
	}
	if (selectedCity == "new") {
		show('city-add-zone');
		show('work-add-zone');
		document.uploadForm.work.selectedIndex=document.uploadForm.work.length-1;
	} else {
		hide('city-add-zone');
		hide('work-add-zone');
		var allWorksDiv = document.getElementById("allworks");
		var allWorks = allWorksDiv.getElementsByTagName("div");
		workDropDown.length=0;
		workDropDown.options[0] = new Option("Step 3. Select a Work", "");
	//	var re_country = new RegExp();
		
	//	var re_city = new RegExp();
		j=1;
		for(i=0;i<allWorks.length;i++) {
			id_country_city_title = allWorks[i].innerHTML.split(",");
			// Re.compile needs to be done on every iteration. Why?????
			//re_city.compile(selectedCity,"gi");
			//re_country.compile(selectedCountry,"gi");
			var re_city = new RegExp(selectedCity);
			var re_country = new RegExp(selectedCountry);
	
			//re.compile(selectedCountry+","+selectedCity, "gi");
			//if (re_country.test(id_country_city_title[0]) && re_city.test(id_country_city_title[1])) 			
			if (id_country_city_title[1].search(re_country)!=-1 && id_country_city_title[2].search(re_city)!=-1) {
				optionValue = id_country_city_title[0];
				optiontext = id_country_city_title[3];
				if (id_country_city_title[4]!="") {
					optiontext += " ("+id_country_city_title[4]+")";
				}
				workDropDown.options[j] = new Option(optiontext, optionValue);
				j++;
			}
		}
		workDropDown.options[j] = new Option("[add new Work]", "new");
	}
}	

/*function updateNewWorkArea () {
	var workDropDown = document.uploadForm.work;
	if (workDropDown.length>0) {
		var selectedWork = workDropDown[workDropDown.selectedIndex].value;
	}
	if (selectedWork == "new") {
		show('work-add-zone');
		workDropDown.selectedIndex=workDropDown.length-1;
	} else {
		hide('work-add-zone');
	}
}
function updateNewCreatorArea () {
	var creatorDropDown = document.uploadForm.creator;
	if (creatorDropDown.length>0) {
		var selectedCreator = creatorDropDown[creatorDropDown.selectedIndex].value;
	}
	if (selectedCreator == "new") {
		show('creator-add-zone');
		creatorDropDown.selectedIndex=creatorDropDown.length-1;
	} else {
		hide('creator-add-zone');
	}
}*/

// For drop downs : [add new xxxx] value must be "new"
function updateNewDropDownArea (fieldname) {
	var dropDown = document.uploadForm[fieldname];
	if (dropDown.length>0) {
		var selectedValue = dropDown[dropDown.selectedIndex].value;
	}
	if (selectedValue == "new") {
		show(fieldname+'-add-zone');
		dropDown.selectedIndex=dropDown.length-1;
	} else {
		hide(fieldname+'-add-zone');
	}
}
// User has clicked on the + button
function highlightNewCityDropDown() {
	document.uploadForm.city.selectedIndex=document.uploadForm.city.length-1;
	
	// Also show the work add zone
	if( document.getElementById('work-add-zone').style.display == "none"
	     && document.getElementById('city-add-zone').style.display == "none") {
		show('work-add-zone');
		document.uploadForm.work.selectedIndex=document.uploadForm.work.length-1;
	}
	flip('city-add-zone'); // Open or close the add zone
}
// User has clicked on the + button
function highlightNewWorkDropDown() {
	flip('work-add-zone');// Open or close the add zone
	document.uploadForm.work.selectedIndex=document.uploadForm.work.length-1;
}
function highlightNewCreatorDropDown() {
	flip('creator-add-zone');// Open or close the add zone
	document.uploadForm.creator.selectedIndex=document.uploadForm.creator.length-1;
}
function highlightNewAuthorDropDown() {
	flip('author-add-zone');// Open or close the add zone
	document.uploadForm.author.selectedIndex=document.uploadForm.author.length-1;
}
function highlightNewRightsHolderDropDown() {
	flip('rightsholder-add-zone');// Open or close the add zone
	document.uploadForm.rightsholder.selectedIndex=document.uploadForm.rightsholder.length-1;
}


function updateImageTitleSubjectFields() {
	updateImageSubjectField();
	updateImageTitleField();
}

function hs_highlightVerticalMenuItem(menuclassname) {
	var sublinks = document.getElementsByClassName(menuclassname);
	for(var i = 0, len = sublinks.length; i < len; i++) {		
		currentPath = window.location.pathname.substring(1);
//		if (sublinks[i].href == window.location.href) {
		if (window.location.href.indexOf(sublinks[i].href) >= 0) {
			sublinks[i].className = menuclassname+"on";
			menuid = sublinks[i].parentNode.id;
			if (menuid) {
				show(menuid);
				setCookie(menuid, "o");
				HS_setArrowState(menuid);
			}
		} else {
			sublinks[i].className = menuclassname;
		}	
	}
}

function hs_highlightHorizontalMenuItem(menuclassname) {
	var sublinksoff = document.getElementsByClassName(menuclassname);
	var sublinkson = document.getElementsByClassName(menuclassname+"on");
	var sublinks = sublinksoff.concat(sublinkson);
	// Looop thru every tab, on or off
	for(var i = 0, len = sublinks.length; i < len; i++) {		
//		currentPath = window.location.pathname.substring(1);
//		if (sublinks[i].href == window.location.href) {
		if (window.location.href.indexOf(sublinks[i].href) >= 0) {
			// We've found a matching tab to the window url ...
			// so turn all the 'on' tabs off first ...
			for(var j = 0, len2 = sublinkson.length; j < len2; j++) {
				sublinkson[j].className = menuclassname;
				setCookie(sublinkson[j].id, "c");
			//	alert(menuclassname+": turning off: "+ sublinkson[j].href+" windowhref: "+window.location.href )
			}
			// Now turn on the tab
			sublinks[i].className = menuclassname+"on";
			//alert(menuclassname+": turning on: "+ sublinks[i].href+" windowhref: "+window.location.href )
			setCookie(sublinks[i].id, "o");
			
		} else {
			// Disbal ethis otherwise all tabs will be turned off when
			// we visit a page that uses the tab but does not match
			// any url in the tabs. eg. tiki-browse_image.php
			//sublinks[i].className = menuclassname;
			//setCookie(sublinks[i].id, "c");
		}	
	}
}

function hs_highlightMenuItem(menuclassname) {
	var menus = document.getElementsByClassName(menuclassname);
	for(var i = 0, len = menus.length; i < len; i++) {
	//alert(menus[i].id);
		var links = menus[i].getElementsByTagName("a"); // all links
		var linkon_index = -1;
		for(var j = 0, len2 = links.length; j < len2; j++) {
		
			if (window.location.href.indexOf(links[j].href) >= 0) {
			//alert(menuclassname+": turning on: "+ links[j].href+" windowhref: "+window.location.href );
				links[j].className = "on";
				//setCookie(menus[i].id, links[j].id);
				linkon_index = j;
			}
		}
		if (linkon_index == -1) {
		
		previousOnMenuItem = getCookie(menus[i].id) ;  //id
		//alert("no url match, trying cookie.."+previousOnMenuItem);
		for(var j = 0, len2 = links.length; j < len2; j++) {
			if (previousOnMenuItem == links[j].id) {
			//alert("Cookie:"+menuclassname+": turning on: "+ links[j].href+" windowhref: "+window.location.href );
				links[j].className = "on";
			}
		}
		
		}
		
	}
}

// Progress box
var isDom = document.getElementById?true:false;
var isIE  = document.all?true:false;
var isNS4 = document.layers?true:false;
var cellCount = 10;

function setprogress(pIdent, pValue, pString, pDeterminate)
{
        if (isDom)
            prog = document.getElementById(pIdent+'installationProgress');
        if (isIE)
            prog = document.all[pIdent+'installationProgress'];
        if (isNS4)
            prog = document.layers[pIdent+'installationProgress'];
	if (prog != null) 
	    prog.innerHTML = pString;

        if (pValue == pDeterminate) {
	    for (i=0; i < cellCount; i++) {
                showCell(i, pIdent, "hidden");	
            }
        }
        if ((pDeterminate > 0) && (pValue > 0)) {
            i = (pValue-1) % cellCount;
            showCell(i, pIdent, "visible");	
        } else {
            for (i=pValue-1; i >=0; i--) {
                showCell(i, pIdent, "visible");	
            }
	}
}

function showCell(pCell, pIdent, pVisibility)
{
	if (isDom)
	    document.getElementById(pIdent+'progressCell'+pCell+'A').style.visibility = pVisibility;
	if (isIE)
	    document.all[pIdent+'progressCell'+pCell+'A'].style.visibility = pVisibility;
	if (isNS4)
	    document.layers[pIdent+'progressCell'+pCell+'A'].style.visibility = pVisibility;

}

function hideProgressBox() {
	var progressBox = document.getElementById("progressBox");
	progressBox.style.display = "none";
}
// End of progress bar
function detect() {
	// simplify things
	var agent 	= navigator.userAgent.toLowerCase();
	
	// detect platform
	this.isMac		= (agent.indexOf('mac') != -1);
	this.isWin		= (agent.indexOf('win') != -1);
	this.isWin2k	= (this.isWin && (
			agent.indexOf('nt 5') != -1));
	this.isWinSP2	= (this.isWin && (
			agent.indexOf('xp') != -1 || 
			agent.indexOf('sv1') != -1));
	this.isOther	= (
			agent.indexOf('unix') != -1 || 
			agent.indexOf('sunos') != -1 || 
			agent.indexOf('bsd') != -1 ||
			agent.indexOf('x11') != -1 || 
			agent.indexOf('linux') != -1);
	
	// detect browser
	this.isSafari	= (agent.indexOf('safari') != -1);
	this.isSafari2 = (this.isSafari && (parseFloat(agent.substring(agent.indexOf("applewebkit/")+"applewebkit/".length,agent.length).substring(0,agent.substring(agent.indexOf("applewebkit/")+"applewebkit/".length,agent.length).indexOf(' '))) >=  300));
	this.isOpera	= (agent.indexOf('opera') != -1);
	this.isNN		= (agent.indexOf('netscape') != -1);
	this.isIE		= (agent.indexOf('msie') != -1);
	
	// itunes compabibility
	this.isiTunesOK	= this.isMac || this.isWin2k;
}
var browser = new detect();



window.onload = function() {
	hs_highlightVerticalMenuItem("sublinkmenu");
	hs_highlightVerticalMenuItem("menulink");
	hs_highlightHorizontalMenuItem("tabdown");
	hs_highlightHorizontalMenuItem("tabup");
	hs_highlightHorizontalMenuItem("tab");
	hs_highlightHorizontalMenuItem("tabsub");
	hs_highlightHorizontalMenuItem("h_menu");
	hs_highlightMenuItem("itab");
//	hideProgressBox();
}

