// Global frontend Javascript file ----------------------------------------------------------------


// opens the print popup window.
function printPage(url) {		

	newwin=window.open(url,'print','height=567,width=580,toolbar=no,locationbar=no,status=no,menubar=yes,scrollbars=yes,resizable=no,copyhistory=no');

}

// Deactivates the not relevant search form elements for the person search
function deactivateSearchFormElements() {
	deactivateEditionFormElements();
	deactivatePersonFormElements();
	deactivateCirculationAreaFormElements();
}



function deactivateEditionFormElements() {
	if(document.getElementById('mediaTitle').checked == true) {
		document.getElementById('editionElements').style.display = 'block';
	} else {
		document.getElementById('editionElements').style.display = 'none';
	}
}


// deactivates the person related form elements in the media search form
function deactivatePersonFormElements() {
	if(document.getElementById('person').checked == true) {
		document.getElementById('personRoleGroups').style.display = 'block';
	} else {
		document.getElementById('personRoleGroups').style.display = 'none';
	}
}

// deactivates the circulation areas for austria
function deactivateCirculationAreaFormElements() {
	if(document.getElementById('distr_all').checked == false && document.getElementById('distr_part').checked == false) {
		document.getElementById('circulationAreasAustria').style.display = 'none';

	} else {
		if(document.getElementById('distr_all').checked == true) {
			document.getElementById('circulationAreasAustria').style.display = 'none';
			
			// Unselect all circulation areas
			for(x=0; x < 42; x++) {
				if (document.getElementById('circulationAreas_' + x) != null) {
					document.getElementById('circulationAreas_' + x).checked = false;
				}
			}
		} else {
			document.getElementById('circulationAreasAustria').style.display = 'block';
		}
	}
}



// deactivates the person related form elements in the media search form
function deactivateProsatextFormElements() {

	document.getElementById('prosatextType').style.backgroundColor = '#F2F2F2';
	document.getElementById('prosatextType').disabled = true;

}


// opens the export popup window
function exportPage(url) {

	newwin=window.open(url,'export','height=650,width=700,toolbar=no,locationbar=no,status=no,menubar=yes,scrollbars=no,resizable=no,copyhistory=no');

}
		
		
// opens the info popup window
function infoPopup(url) {

	newwin=window.open(url,'infoPopup','height=567,width=720,toolbar=no,locationbar=no,status=no,menubar=yes,scrollbars=yes,resizable=no,copyhistory=no');

}


// returns the URL to the tariff export servlet
function tariffExport(idMasterData) {

	if(document.getElementById('tariffExportFormatExcel').checked) {
		url = 'ExportTariff.xls?idMasterData=' + idMasterData + '&exportFormat=excel';
	}		
		
	if(document.getElementById('tariffExportFormatCsv').checked) {
		url = 'ExportTariff.csv?idMasterData=' + idMasterData + '&exportFormat=csv';
	}

	window.location.href = url;
	
}

// removes a search profile in the list
function removeSearchProfile(text, url, userProfileId){

	if(confirm("" + text)) {
		document.location = url + "&userProfileId=" + userProfileId;
	}

}

// checks keystrokes in login box and submits the form when pressing enter
function checkForLoginSubmit(e) {

	var whichCode;
	whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == "13") {
		document.loginBox.submit();
	}

}


// checks keystrokes in quick search box and submits the form when pressing enter
function checkForQuickSearchSubmit(e, searchType, searchForMedia, searchForPerson, searchForKeyword) {

	var whichCode;
	whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == "13") {
		quickSearchSubmit(searchType, searchForMedia, searchForPerson, searchForKeyword);
	}

}


// submits the quick search box
function quickSearchSubmit(searchType, searchForMedia, searchForPerson, searchForKeyword) {

	document.quickSearchBox.searchForQs.value = searchType;
	if (searchType == searchForMedia) {
		document.quickSearchBox.nameOfPersonQs.value = "";
		document.quickSearchBox.firstnameOfPersonQs.value = "";
		document.quickSearchBox.companyQs.value = "";
	} else if (searchType == searchForKeyword) {
		document.quickSearchBox.nameOfMediaQs.value = "";
		document.quickSearchBox.nameOfPersonQs.value = "";
		document.quickSearchBox.firstnameOfPersonQs.value = "";
	} else {
		document.quickSearchBox.nameOfMediaQs.value = "";
		document.quickSearchBox.companyQs.value = "";
	}
	document.quickSearchBox.submit();

}


// opens the demo popup
function openDemo(demoPage) {

	newwin=window.open(demoPage,'demo','height=587,width=700,toolbar=no,locationbar=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no');
	newwin.focus();

}


// opens the order page from a link in the demo popup
function openOrderPage(orderPage) {

	opener.location.href=orderPage;
	opener.focus();

}


// defines the action of distribution list export
function pressDistributionListExport() {

	if(document.getElementById('pressDistributionListExportFormatExcel').checked) {
		document.pressDistributionListExportForm.action = 'ExportPressDistributionList.xls';
	}		
		
	if(document.getElementById('pressDistributionListExportFormatCsv').checked) {
		document.pressDistributionListExportForm.action = 'ExportPressDistributionList.csv';
	}

	document.pressDistributionListExportForm.submit();

}

// defines the action of media title export
function mediaTitleExport() {

	if(document.getElementById('mediaTitleExportFormatExcel').checked) {
		document.mediaTitleExportForm.action = 'ExportMediaTitle.xls';
	}		
		
	if(document.getElementById('mediaTitleExportFormatCsv').checked) {
		document.mediaTitleExportForm.action = 'ExportMediaTitle.csv';
	}

	document.mediaTitleExportForm.submit();
}

function changeListSize(listbox, psize) {
	document.getElementById(listbox).size = psize;
}

function swapListSize(listbox, small, big) {
	
	if(document.getElementById(listbox).size == small) {
		document.getElementById(listbox).size = big;
	} else {
		if(document.getElementById(listbox).size == big) {
			document.getElementById(listbox).size = small;
		}
	}
}

function resetPressDistributionListExportFilter(field) {

	document.pressDistributionListExportForm.pressDistributionListExportFilter[3].checked=true;
	if(field == "name") {
		if(!document.pressDistributionListExportForm.flagName.checked) {
			document.pressDistributionListExportForm.pressDistributionListExportFilterName.disabled=true;
			document.getElementById("td_filter_name").className="textinactive";
		} else {
			document.pressDistributionListExportForm.pressDistributionListExportFilterName.disabled=false;
			document.getElementById("td_filter_name").className="text";
		}
	} else if(field == "address") {
		if(!document.pressDistributionListExportForm.flagAddress.checked) {
			document.pressDistributionListExportForm.pressDistributionListExportFilterAddress.disabled=true;
			document.getElementById("td_filter_address").className="textinactive";
		} else {
			document.pressDistributionListExportForm.pressDistributionListExportFilterAddress.disabled=false;
			document.getElementById("td_filter_address").className="text";
		}
	} else if(field == "emailAddress") {
		if(!document.pressDistributionListExportForm.flagEmail.checked) {
			document.pressDistributionListExportForm.pressDistributionListExportFilterEMail.disabled=true;
			document.getElementById("td_filter_email").className="textinactive";
		} else {
			document.pressDistributionListExportForm.pressDistributionListExportFilterEMail.disabled=false;
			document.getElementById("td_filter_email").className="text";
		}
	} 
}

function checkPressDistributionListExportFilterName() {
	document.pressDistributionListExportForm.flagName.checked = true;
}


function checkPressDistributionListExportFilterAddress() {
	document.pressDistributionListExportForm.flagAddress.checked = true;
}

function checkPressDistributionListExportFilterEMail() {
	document.pressDistributionListExportForm.flagEmail.checked = true;
}

function swapImage(image,swap) {
	document.getElementById(image).src = swap;
}

function activeURL(pageurl) {
	var url = window.location.href;
	var page = url.substring(url.lastIndexOf("/")+1,url.lastIndexOf(".")).toLowerCase() ;

	if (pageurl == page) return true;
	return false;
}




// Enables/Disables the given div layer
function DisplayDiv(strDivID, boolDisplay) {
	if (boolDisplay == 0) {
		document.getElementById(strDivID).style.display = 'none'
	}
	else {
		if (boolDisplay > 0) {
			document.getElementById(strDivID).style.display = 'block';
		}
		else {
			if (document.getElementById(strDivID).style.display == 'none') {
				document.getElementById(strDivID).style.display = 'block';
			}
			else {
				document.getElementById(strDivID).style.display = 'none';
			}
		}
	}
}

// Handles the treeview navigation
function DisplaySign(strDivID) {
	if (document.getElementById(strDivID).style.display == 'none') {
		document.getElementById(strDivID + '_Sign').src = '../../images/frontend/treeview_plus.gif';
	} else {
		document.getElementById(strDivID + '_Sign').src = '../../images/frontend/treeview_minus.gif';
	}
}

/**
 * Select/Unselect all x children for the supplied parent id
 * 
 * @param parentId the id of the parent
 * @param numberOfChildren the number of children of this parent
 * 
 */
function roleGroupSubs(parentId, numberOfChildren) {
	if(document.getElementById("roleGroupParent_" + parentId).checked == true) {
		for(x=0; x < numberOfChildren; x++) {
			document.getElementById("roleGroupChildren_" + parentId + "_" + x).checked = true;
		}
	} else if (document.getElementById("roleGroupParent_" + parentId).checked == false) {
		for(x=0; x < numberOfChildren; x++) {
			document.getElementById("roleGroupChildren_" + parentId + "_" + x).checked = false;
		}
	} 
}

/**
 * Unselects the checkbox with the supplied id
 * 
 * @param String the id of the checkbox
 */
function unselectCheckbox(checkboxId) {
	if(document.getElementById(checkboxId).checked == true) {
		document.getElementById(checkboxId).checked = false;
	}	
}

/**
 * Toogles the ul/li list expand/collapse for the
 * media and journalist index.
 * 
 * @param name name of the ul element to show/hide
 * @param container name of the parent ul element to change icon
 */
function toggleIndex(name, container) {
	if (document.getElementById(name).style.display == 'none') {
		document.getElementById(name).style.display = 'block';
		document.getElementById(container).setAttribute("class", "collapse");
		document.getElementById(container).setAttribute("className", "collapse"); /* IE, *sigh* */
	} else {
		document.getElementById(name).style.display = 'none';
		document.getElementById(container).setAttribute("class", "expand");
		document.getElementById(container).setAttribute("className", "expand"); /* IE, *sigh* */
	}
}
