function updateModulesSelect(elem_check,elem_select,selected)
{
	var c = document.getElementById(elem_check);
	var f = 0;
	if(c.checked) f = getIndex(elem_select,selected);
	document.getElementById(elem_select).selectedIndex = f;
}

function getIndex(elem,val)
{
	var e = document.getElementById(elem);
	var f = e.length;
	for(i = 0; i < f; i++) if(e.options[i].value == val) return i;
	return -1;
}

function showUsers(id){parent.location.href="/management/a_security/index.php?command=view&userid="+id;}
function save(form2){if(confirm("Save Changes?")){form2.submit();}}
function search(form2){form2.submit();}
function upload(form2){if(confirm("Upload file, this may take a few minutes?")){form2.submit();}}
function removeUser(form2){
	if(confirm("Remove User "))
	{
		form2.command.value="removeuser";
		form2.submit();
	}
}
function link(url){parent.location.href = url;}
function setFocus(elem){elem.focus();}
function showAlert(mesg){alert(mesg);}
function openNewWindow(url,width,height){
    x = (1024 - width)/2, y = (768 - height)/2;
    if (screen) {
       	y = (screen.availHeight - height)/2;
        x = (screen.availWidth - width)/2;
    }
	window.open(url,"nw","height="+height+",width="+width+",left="+x+",top="+y+",scrollbars=yes");
}



function insertText(input, insTexte){
	if(!insTexte) return;

	startTag = '<';
	endTag = '>';

	if(input.createTextRange){
		var text;
		input.focus(input.caretPos);
		input.caretPos = document.selection.createRange().duplicate();
		if(input.caretPos.text.length>0){
			input.caretPos.text = startTag + input.caretPos.text + endTag;
		} else {
			input.caretPos.text = startTag +  insTexte +  endTag;
		}
	} else {
		input.value += startTag + insTexte + endTag;
	}
}

function addNewCategory(elem){
	document.constants.mode.value='add';
	document.constants.type.value=elem;
	document.constants.submit();
}

function saveCategory(elem){
	if(confirm("Save?")){
		document.constants.mode.value='edit';
		document.constants.type.value=elem;
		document.constants.submit();	
	}
}

function saveOrder(elem){
	document.constants.mode.value='saveorder';
	document.constants.type.value=elem;
	document.constants.submit();	
}

function remove(elem)
{
	if(elem == "cat_2"){
		if(confirm("Remove selected category?\nNOTE: All child categories will be deleted as well")){
			document.constants.mode.value='remove';
			document.constants.type.value=elem;
			document.constants.submit();			
		}
	} else {
		if(confirm("Remove selected category?")){
			document.constants.mode.value='remove';
			document.constants.type.value=elem;
			document.constants.submit();
		}
	}
}

function saveProductionCompany(){
	document.forms[0].mode.value='save';
	document.forms[0].submit();
}

function saveFilmRating()
{
	document.forms[0].mode.value='save';
	document.forms[0].submit();
}

function saveFilmFormat()
{
	document.forms[0].mode.value='save';
	document.forms[0].submit();
}



function salary(e){
	e.value = new NumberFormat(e.value).toFormatted();
	pattern = /\d{1,3}(,\d{3})*\.\d{2}/;
	if(!pattern.exec(e.value)){
		alert('Please enter a number in this field e.g. 25000');
		e.value='';
	}
}

function addCast(){
	document.forms[0].action='/management/a_add_films/iframes/tempsave.php';
	document.forms[0].type.value='addcast';
	document.forms[0].target = 't';
	document.forms[0].submit();
}

function addCrew(){
	document.forms[0].action='/management/a_add_films/iframes/tempsave.php';
	document.forms[0].type.value='addcrew';
	document.forms[0].target = 't';
	document.forms[0].submit();
}

function addAwards(){
	document.forms[0].action='/management/a_add_films/iframes/tempsave.php';
	document.forms[0].type.value='addawards';
	document.forms[0].target = 't';
	document.forms[0].submit();
}

function addRights(){
	document.forms[0].action='/management/a_add_films/iframes/tempsave.php';
	document.forms[0].type.value='addrights';
	document.forms[0].target = 't';
	document.forms[0].submit();
}

function removeCast(id){
	if(confirm('Remove Cast')){
		document.forms[0].action='/management/a_add_films/iframes/tempsave.php';
		document.forms[0].type.value='removecast';
		document.forms[0].target = 't';
		document.forms[0].id.value = id;
		document.forms[0].submit();
	}
}

function removeCrew(id){
	if(confirm('Remove Crew')){
		document.forms[0].action='/management/a_add_films/iframes/tempsave.php';
		document.forms[0].type.value='removecrew';
		document.forms[0].target = 't';
		document.forms[0].id.value = id;
		document.forms[0].submit();
	}
}

function removeAwards(id){
	if(confirm('Remove Award')){
		document.forms[0].action='/management/a_add_films/iframes/tempsave.php';
		document.forms[0].type.value='removeawards';
		document.forms[0].target = 't';
		document.forms[0].id.value = id;
		document.forms[0].submit();
	}
}

function removeRights(id){
	if(confirm('Remove Rights')){
		document.forms[0].action='/management/a_add_films/iframes/tempsave.php';
		document.forms[0].type.value='removerights';
		document.forms[0].target = 't';
		document.forms[0].id.value = id;
		document.forms[0].submit();
	}
}

function saveMovie(){
	if(confirm("Save Movie")){
		document.forms[0].target = "_self";
		document.forms[0].action = "/management/a_add_films/index.php";
		document.forms[0].type.value='savemovie';
		document.forms[0].submit();
	}
}


function doFilmSearch(){
	document.forms[0].action='/management/a_film_database/iframes/search.php';
	document.forms[0].type.value='search';
	document.forms[0].target = 'sr';
	document.forms[0].submit();	
}

function doFilmSearchExport(){
	document.forms[0].action='/management/a_film_database/iframes/export.php';
	document.forms[0].type.value='search';
	document.forms[0].target = 'sr';
	document.forms[0].submit();	
}

function doFilmSearchPrint(){
	document.forms[0].action='/management/a_film_database/iframes/print.php';
	document.forms[0].type.value='search';
	document.forms[0].target = '_blank';
	document.forms[0].submit();	
}

function doMemberSearch(){
	document.forms[0].action='/management/a_members/iframes/search.php';
	document.forms[0].type.value='search';
	document.forms[0].target = 'iframe';
	document.forms[0].submit();
}


function sorderby(id)
{
	if(id != ""){
		document.forms[0].orderby.value = id;
	} else {
		document.forms[0].orderby.value = "title";
	}
	document.forms[0].submit();
}

function archiveMovie(){
	if(confirm("Archive Movie")){
		document.forms[0].target = "_self";
		document.forms[0].action = "/management/a_add_films/index.php";
		document.forms[0].type.value='archivemovie';
		document.forms[0].submit();	
	}
}

function restoreFromArchive(){
	if(confirm("Restore Movie from Archive")){
		document.forms[0].target = "_self";
		document.forms[0].action = "/management/a_add_films/index.php";
		document.forms[0].type.value='restore_from_archive';
		document.forms[0].submit();	
	}
}

function selectMembersAll(){
	var obj = new Object();
	obj = document.frames['iframe'].document.forms[0];
	if(obj != 'undefined'){
		var num = obj.email_count.value;
		var e = new Array();
		for(i = 0; i < num; i++){
			t = "emails["+i+"]";
			obj.elements[t].checked = true;
			e.push(obj.elements[t].value);
		}
	}
}

function emailMember(){
	var obj = new Object();
	obj = document.frames['iframe'].document.forms[0];
	var num = obj.email_count.value;
	var total = 0;
	for(i = 0; i < num; i++)
		if(obj.elements['emails['+i+']'].checked){total++;}
	
	if(total > 0){
		obj.action="/management/a_members/send_emails.php";
		obj.target="_blank";
		obj.submit();
	} else {
		alert("Please select atleast 1 Member");
	}
}

function positionMiddle(){
	var height = window.screen.height;
	var width = window.screen.width;
	
	var windowHeight = 600;
	var windowWidth = 800
	
	window.resizeTo(windowWidth,windowHeight);
	window.moveTo((width - windowWidth)/2,(windowHeight - 400)/2);
}

function sendMail(){
	if(document.emails.subject.value == ""){
		alert("Please fill in the Subject");
		return;
	}
	if(confirm("Send this mail")){
		document.emails.which_action.value = "sendmail";
		document.emails.submit();
	}
}

function doPaymentSearch(){
	document.forms[0].action='/management/a_payments/iframes/search.php';
	document.forms[0].type.value='search';
	document.forms[0].target = 'iframe';
	document.forms[0].submit();	
}

function doPaymentSearchExport(){
	document.forms[0].action='/management/a_payments/iframes/export.php';
	document.forms[0].type.value='search';
	document.forms[0].target = 'sr';
	document.forms[0].submit();	
}

function doPaymentSearchPrint(){
	document.forms[0].action='/management/a_payments/iframes/print.php';
	document.forms[0].type.value='search';
	document.forms[0].target = '_blank';
	document.forms[0].submit();	
}

function exportIframe(which){
	var loc = document.getElementById(which).src + '?export=1';
	window.open(loc);
}

function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string') element = document.getElementById(element);
		if (arguments.length == 1) return element;
		elements.push(element);
	}
	return elements;
}