
// Opens given url in help window.
function openHelp(url) {
	window.open(url,'legal','toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,width=500,height=400'); 
	return false;
}



// This function is called from the onload event
// of intranet layout pages.
function executeOnLoadFunctions() {
	trimTextAreas();
	gotoFirstInputField();
	startNavigation();
}

function gotoFirstInputField() {
	fields = document.body.getElementsByTagName("INPUT");
	if(fields && fields.length > 0) {
		for(i = 0; i < fields.length; i++) {
			if (fields[i].type == 'text') {
				fields[i].focus();
				break;
			}
		}
	}
}

function doSubmit(method, formName) {
	if(formName==null) {
		document.forms[0].method.value=method;
	} else {
		document.forms[formName].method.value=method;
	}
	return false;
}

function switchMeasureSystem(measureSystem) {

	newURL = location.href;
	pos = newURL.indexOf('ms=');

	if (pos > 0) {
		
		startString = newURL.substr(0, pos + 3);
		endString = newURL.substr(pos + 4);
		newURL = startString + measureSystem + endString;
		
	} else {
	
		if (newURL.indexOf('?') > 0) {
				newURL += '&ms=' + measureSystem;
			} else {
				newURL += '?ms=' + measureSystem;
			}
	}

	location.href = newURL;
}
function validateAndSave(fieldName, errorMessage) {
	// get an array of all input fields
	var submit = true;
	var aFields = document.getElementsByTagName("INPUT");
	for (var iField=0; iField < aFields.length; iField++) {
		if(aFields[iField].id==fieldName) {
			if(aFields[iField].value<=0) {
				aFields[iField].focus();
				submit = false;
			}
		}
	}
	if(submit==false) {
		alert(errorMessage);
	} else {
		document.forms['MaintainBasisWeightForm'].submit();
	}
}
// functions used in MaintainBasisWeight - start
function loadMillBrand(millBrandId) {
	document.forms['MaintainBasisWeightForm'].millBrandId.value=millBrandId;
	document.forms['MaintainBasisWeightForm'].submit();
	return false;
}
function addProperty(measureSystem) {

	if(measureSystem=='m') {
		document.forms['MaintainBasisWeightForm'].propertyId.value=document.forms['MaintainBasisWeightForm'].propertyIdMetric.value;
	} else if(measureSystem=='i') {
		document.forms['MaintainBasisWeightForm'].propertyId.value=document.forms['MaintainBasisWeightForm'].propertyIdImperial.value;
	} else {
		alert("error: non existing measureSystem");
	}
	doSubmit('addProperty');
}
function removeProperty(propId) {
	document.forms['MaintainBasisWeightForm'].propertyId.value=propId;
	doSubmit('removeProperty');
}
function editBasisWeight(position) {
	document.forms['MaintainBasisWeightForm'].bwPosition.value=position;
	doSubmit('editbw');
	document.forms[0].submit();
}
function removeBasisWeight(position) {
	alert(position);
	//document.forms['MaintainBasisWeightForm'].bwPosition.value=position;
	//doSubmit('deletebw');
}
// functions used in MaintainBasisWeight - end

function refreshSearchForm() {
	doSubmit('refresh','searchForm');
	document.forms['searchForm'].submit();
}

// This function is used to workaround a problem with the <textarea /> tag.
// If empty, a space was added to force the full rendering of the tag in the
// form of <textarea> </textarea>.  Since we don't want this space, here
// we trim it again.
function trimTextAreas() {
	areas = document.body.getElementsByTagName("TEXTAREA");
	if(areas.length>0) {
		for(i = 0; i < areas.length; i++) {
			if (areas[i].value == ' ' || areas[i].value == String.fromCharCode(160) ) {
				areas[i].value='';
			}
		}
	}
}

function startNavigation() {
	$('ul.sf-menu').superfish({ 
        delay:       100,                            // one second delay on mouseout 
        animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
        speed:       1,                          // faster animation speed 
        autoArrows:  true,                           // disable generation of arrow mark-up 
        dropShadows: false                            // disable drop shadows 
    }); 
}

function replace(s, t, u) {
  /*
  **  Replace a token in a string
  **    s  string to be processed
  **    t  token to be found and removed
  **    u  token to be inserted
  **  returns new String
  */
  i = s.indexOf(t);
  r = "";
  if (i == -1) return s;
  r += s.substring(0,i) + u;
  if ( i + t.length < s.length)
    r += replace(s.substring(i + t.length, s.length), t, u);
  return r;
  }


function redirect(elementId) {

	var button = document.getElementById(elementId);
   	var TARG_ID = "COOL_REDIRECT";
   	var e = document.getElementById(TARG_ID);
   	if (e == null || button == null) {
   		return;
   	}
   	
   	var func = function() {
		if(cTicks) {
			e.innerHTML = --cTicks;
		} else {
			clearInterval(timer);
			//button.click();	
			button.focus();
		}
	}
   	
	var cTicks = parseInt(e.innerHTML);
	var timer = setInterval(func, 1000);
}

// global var to make sure that tinyMCE is only loaded once
var tinyMCEinit = false;

// function that loads the TinyMCE edit fields
function initTinyMCE (language, helpUrl) {
	if (tinyMCEinit == false) {
		tinyMCE.init({
			mode : "specific_textareas",
			textarea_trigger : "accesskey",
			theme : "catalog",
			language : language,
			htmlHelpURL : helpUrl,
			dialog_type : 'window',
			force_p_newlines : false
		});
		tinyMCEinit = true;
	}
}

// this function toggles the visibility of an element
function toggleById(id){
	if (!document.getElementById) { return; }
	var elem = document.getElementById(id);

    if (elem.style.display == "none"){
		elem.style.display = "";
	} else {
		elem.style.display = "none";
	}
}

// show additional input field for link keyword
function showLinkKeyword () {
	if (!document.getElementById) { 
		return; 
	}
	var keywordType = new Number(document.forms[0]['keyword.keywordType'].value);
	
	if (keywordType == 11) {
		document.getElementById(11).style.display= '';
	} else {
		document.getElementById(11).style.display= 'none';
	}
}

function switchMarket(newValue, oldValue) {
	var oldMarket;
	if (oldValue == 0) {
		oldMarket = "/global/";
	}
	if (oldValue == 1) {
		oldMarket = "/eu/";
	}
	if (oldValue == 2) {
		oldMarket = "/na/";
	}
	if (oldValue == 3) {
		oldMarket = "/ap/";
	}
	var newMarket = "/" + newValue + "/";
	var newUrl = replace(document.location.href, oldMarket, newMarket);
	document.location.href = newUrl;
}

function openUrl(url, title) {
	while(url.indexOf('&amp;') > -1) {
		url = url.replace('&amp;', '&');
	}
	window.open(url, title);
}