/*
Authors		Alastair Telford & Ian Sims
Copyright	Webscript Designs Limited & On-Music
Version		$Id: stdfunctions.js,v 1.15 2005/07/15 16:53:42 alastair Exp $
*/

var error_element;

function submitSearch(e) {
	var f = e.form;
	if (check_search(f)) {
		f.submit();
	} else {
		alert("You need to supply more data for the search -\n" + 
				"A genre should be chosen and/or at least one of the text boxes must be completed with three or more characters");
	}
}
	
function submitChannel() {
	document.channel.submit();
	}	
	
function submitAdminSearch() {
	if (!document.getElementById('form_search').admin_keywords.value) {
		alert('Please supply one or more keywords');
		return(false);
		}
	else {
		document.getElementById('form_search').submit();
		return(true);
		}
	}
	
function submitTrackEdit() {

	var submitfrm;
	var classification = document.edittrack.type[document.edittrack.type.selectedIndex].value;
	var ratecard = document.edittrack.ratecard[document.edittrack.ratecard.selectedIndex].value;
	var label = document.edittrack.label[document.edittrack.label.selectedIndex].value;
	//var publisher = document.edittrack.publisher[document.edittrack.publisher.selectedIndex].value;
	
	if (!document.edittrack.title.value) {
		alert('Please supply a music title');
		}
	else if (!document.edittrack.composer.value) {
		alert('Please supply the composer(s) of this song');
		}
	else if (!document.edittrack.description.value) {
		alert('Please supply a brief description that accurately conveys the instrumentation and style of this song');
		}
	else if (!document.edittrack.stream_url.value) {
		alert('Please supply the location of the Stream so that broadcasters can preview the song. Without a stream, the track will not be available to users of the system');
		}
	else if (!document.edittrack.download_url.value && !document.edittrack.cd_ref.value) {
		alert('Please either supply the location of the Download URL of the song or the CD name and number so that broadcasters can get hold of the song once selected for a cuesheet');
		}
	//else if (!document.edittrack.publisher_add.value && publisher == "0") {
	//	alert('Please either select a Publisher from the list or enter the name of a new Publisher');
	//	}
	else if (!document.edittrack.label_add.value && label == "0") {
		alert('Please either select a Record Label/Music Library from the list or enter the name of a new label or library company');
		}
	else if (classification == "P" && (label != document.edittrack.my_lab_id.value || publisher != document.edittrack.my_pub_id.value)) {
		if (confirm('If you choose to Pre-Price your music, you must hold all the rights to the track and you are happy to fully authorise its use in any programme without exception. Click OK if you agree with this or click CANCEL and pick another Classification if you do not agree or do not hold all the rights.')) {
			submitfrm = "y";
			}
		}
	else if (classification == "L" && ratecard != "L") {
		alert('If you classify your music as Library music, it is registered with the MCPS as such and the selected ratecard must correlate. Please either change the classification or select the MCPS ratecard from the list');
		}
	else if (classification == "C" && ratecard != "X") {
		alert('If you classify your music as Commercial music, you must select a ratecard that notifies the broadcaster that the song requires clearance (X). Please either change the classification or select the UnPriced (X) ratecard from the list. You may choose Pre-Priced as an option and set a price for your music providing that you own both the Master and Publishing rights');
		}
	else if (classification == "P" && ratecard == "X") {
		alert('If you choose to Pre-Price your music, you must select a pre-cleared ratecard from the list that will then present the costs to the broadcaster (ie: not L or X)');
		}
	else if (classification == "C" && !document.edittrack.performer.value) {
		alert('If you classify your music as Commercial music, you must enter a performer for reporting usage');
		}
	else if (classification == "L" && !document.edittrack.composer.value) {
		alert('If you classify your music as Library music, you must enter a composer for reporting usage');
		}
	//else if (document.edittrack.publisher_add.value && publisher != "0") {
	//		if (confirm('Are you sure you wish to override the currently selected Publisher with the new one you have provided?')) {
	//			submitfrm = "y";
	//			}
	//		}
	else if (document.edittrack.label_add.value && label != "0") {
			if (confirm('Are you sure you wish to override the currently selected Record Label with the new one you have provided?')) {
				submitfrm = "y";
				}
			}
	else {
		submitfrm = "y";
		}
		
	if (submitfrm == "y") {
		if(confirm('Have you tested the "Stream URL" using the "TEST" button provided? If the URL is incorrect, the track will not be available to broadcasters')) {
			//Make sure all the publishers are selected to pass back to PHP
			var i;
		  	for(i = 0; i < document.edittrack.elements['publisher[]'].length; i++) {
		    	document.edittrack.elements['publisher[]'].options[i].selected = true;
		  		} 
			document.edittrack.submit();
			}
	}
}

function submitQuickTrackEdit() {
	var submitfrm;
	var reccode = document.edittrack.rec[document.edittrack.rec.selectedIndex].value;
	// var label = document.edittrack.label[document.edittrack.label.selectedIndex].value;
	if (!document.edittrack.title.value) {
		alert('Please supply a music title');
	} else if (!document.edittrack.composer.value) {
		alert('Please supply the composer(s) of this song');
	/* 
	} else if (!document.edittrack.label_add.value && label == "0") {
		alert('Please either select a Record Label/Music Library from the list or enter the name of a new label or library company');
	*/
	} 	else if (reccode == "C" && !document.edittrack.performer.value) {
		alert('If you classify your music as Commercial music, you must enter a performer for reporting usage');
	} else if ((reccode == "L" || reccode == "M") && !document.edittrack.composer.value) {
		alert('If you classify your music as Library music, you must enter a composer for reporting usage');
	}
/* 
else if (document.edittrack.label_add.value && label != "0") {
			if (confirm('Are you sure you wish to override the currently selected Record Label with the new one you have provided?')) {
				submitfrm = "y";
			} }
*/
	else {
		submitfrm = "y";
	}
	if (submitfrm == "y") {
		//Make sure all the publishers are selected to pass back to PHP
		document.edittrack.submit();
	} else {
		return(false);
	}
}
	
function viewRates() {
	var ratecard = document.edittrack.ratecard[document.edittrack.ratecard.selectedIndex].value;
	popup('music_ratecard.php?type='+ratecard,'ratewindow','500','300')
	}
	
function popup(url,win,pwidth,pheight) {
	var winA = Array();
    winA['id'] = win;
	ShowWindow(url,pwidth,pheight,winA,'status=no,resizable=no');
	}
	
function popupScroll(url,win,pwidth,pheight) {
	window.open(url,win,'width='+pwidth+',height='+pheight+' ,scrollbars=yes');
	}
	
function redirect(url) {
	window.location.href = url;
	}
	
function gotoAname(aname) {
	window.location.href = "#" + aname;
	}

function checkSong(urltype) {
	if (urltype == 'stream') {
		var url = document.edittrack.stream_url.value;
		}
	else {
		var url = document.edittrack.download_url.value;
		}
	if (url) {
		window.open('checksong.php?url='+url,'checksong','width=450,height=210');
		}
	else {
		alert('Please enter the URL of the song into the box first');
		}
	}
	
function copyPub(object) {
    var index = object.publisherlist.selectedIndex;
    if (index > -1) {
        var newoption = new Option(object.publisherlist.options[index].text, object.publisherlist.options[index].value, true, true);
        object.elements['publisher[]'].options[object.elements['publisher[]'].length] = newoption;
        if (!document.getElementById) history.go(0);
        //object.publisherlist.options[index] = null;
        object.publisherlist.selectedIndex = 0;
    }
}

function removePub(object) {
    object.elements['publisher[]'].options[object.elements['publisher[]'].selectedIndex]= null;
}

//Check numbers are being entered
// EG: <input type="text" onChange="if (!checkChars(this.value, '1234567890')) alert('Please enter a numeric value in Seconds')">
function checkChars(string, allowed) {
    if (!string) return false;
    var Chars = allowed;

    for (var i = 0; i < string.length; i++) {
       if (Chars.indexOf(string.charAt(i)) == -1)
          return false;
    }
    return true;
} 

function highlightField(elem) {
	if (error_element && error_element != elem) {
		elem.blur();
		error_element.focus();
	} else {
		error_element = null;
		f1 = document.edit;
		for (a=0;a<f1.length;a++) {
			if (f1[a] == elem) {
				f1[a].style.background = "#efefef";
			}
		}
	}
 }
 
function unhighlightField(elem) {
	f1 = document.edit;
	for (a=0;a<f1.length;a++) {
	 if (f1[a] == elem) {
	 	f1[a].style.background = "#fff";
		}
	 }
 }
 
function skipTab(elem, skipnumber, event) {
	f1 = document.edit;
	for (a=0;a<f1.length;a++) {
	 if (f1[a] == elem) {
	 	if (event.keyCode == 9 && event.shiftKey) {	// SHIFT KEY (as in shift and backtab)
	 		goto_field = f1[a-1];
			}
		else {
			goto_field = f1[a+skipnumber];
			}
		goto_field.focus();
	  }
	}
}

function ShowWindow(loc,wd,ht,re,feat)
{
  var ah = screen.availHeight;
  var aw = screen.availWidth;
  var hh = Math.floor((ah - ht) / 2);
  var hw = Math.floor((aw - wd) / 2);
  var wn = (re && re.id)?re.id:"ModalChild";
  var dff = (feat)?feat:"menubar=yes,scrollbars=yes,dependent=yes";
  var newwin = window.open(loc,wn,dff + 
       ",width=" + wd + ",height=" + ht + ",left="+hw+",top="+hh);
  newwin.focus();
}

function reset_musicsearch() {
	var f = document.getElementById('form_search');
	
	if (f.title) {
		f.title.value = '';
	}
	if (f.keywords) {
		f.keywords.value = '';
	}
	if (f.description) {
		f.description.value = '';
	}
	if (f.rec) {
		f.rec.value = '';
	}
	if (f.performer) {
		f.performer.value = '';
	}
	if (f.composer) {
		f.composer.value = '';
	}
	if (f.arranger) {
		f.arranger.value = '';
	}
	if (f.publisher) {
		f.publisher.value = '';
	}
	if (f.label) {
		f.label.value = '';
	}
	if (f.company) {
		f.company.value = '';
	}
	if (f.genre) {
		f.genre.value = 0;
	}
	if (f.album_title) {
		f.album_title.value = '';
	}
	if (f.cat_num) {
		f.cat_num.value = '';
	}
	if (f.isrc) {
		f.isrc.value = '';
	}
	if (f.iswc) {
		f.iswc.value = '';
	}
	if (document.getElementById('match_all_true')) {
		document.getElementById('match_all_true').checked = 'checked';
	}
	if (document.getElementById('type_p')) {
		document.getElementById('type_p').checked = 'checked';
	}
	if (document.getElementById('type_c')) {
		document.getElementById('type_c').checked = 'checked';
	}
	if (document.getElementById('type_l')) {
		document.getElementById('type_l').checked = 'checked';
	}
}

function advanced_display() {
	var b = document.getElementById('advanced').value;
	if (b == '1') {
		b = 0;
		document.getElementById('show_text').innerHTML = 'Show';
		document.getElementById('publishing').style.display = 'none';
		document.getElementById('search_options').style.display = 'none';
	} else {
		b = 1;
		document.getElementById('publishing').style.display = 'block';
		document.getElementById('search_options').style.display = 'block';
		document.getElementById('show_text').innerHTML = 'Hide';
	}
	document.getElementById('advanced').value = b;
}

function blank_unknown(e) {
	var s = e.value;
	if (s.match(/^\s*$/)) {
		e.value = "UNKNOWN";
	}
}

function all_select(sel,item_name) {
    if (!item_name) {
    var item_name = 'act_list[]';
    }
	var l = document.getElementsByName(item_name);
	for (var i = 0; i < l.length; i++) {
		if (sel) {
			l[i].checked = 'checked';
		} else {
			l[i].checked = '';
		}
	} 
	return(1);
}

function some_selected()  {
	var act_list = document.getElementsByName('act_list[]');
	var some_selected = false;
	for (var i = 0; i < act_list.length; i++) {
		if (act_list[i].checked) {
			some_selected = true;
			break;
		}
	}
	if (!some_selected) {
		alert("None selected!");
	}
	return(some_selected);
}

function check_search(f) {
	var some_set = false; 
    if (document.getElementById('genre').value > 0) {
        some_set = true;
    } else {
        for (var i = 0; i < f.elements.length; i++) {
            var e = f.elements[i];
            if (e.type == 'text') {
                var pat = /\w{3,}/i;
                if (pat.test(e.value)) {
                    some_set = true;
                }
            } 
        }
    }
	return(some_set);
}

function goAddTrack() {
    var fields = ['rec','title','performer','composer','arranger','is_bbc'];
    var qystr = 'mini=true';
    for (var i = 0; i < fields.length; i++) {
		qystr += '&' + fields[i] + '=' + document.getElementById(fields[i]).value;
    }
	window.location.href = '/add_track.php?' + qystr;
}

/* 
* Replaces the code with the value in the search if a live, catch all value is being searched.
*/
function replace_code(c) {
	var is_bbc = document.getElementById('is_bbc') && (document.getElementById('is_bbc').value > 0);
	var live_value = is_bbc?'L':'P';
	return((document.getElementById('rec').value == live_value)?live_value:c);
}


function AJAX() {
	var o = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try {
		o = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			o = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			o = false;
		}
	}
	@end @*/
	if (!o && typeof XMLHttpRequest!='undefined') {
		o = new XMLHttpRequest();
	} 
 return o;
} 

function show_hide(e,rexp,hideshow_name) {
	var  hs = document.getElementById(hideshow_name);
	var found = e.value.match(rexp);
	hs.style.display = found?'block':'none';
}

function reset_checked(els_name) {
	var es = document.getElementsByName(els_name);
	for (var i = 0; i < es.length; i++) {
		es[i].checked = false;
	}
}

function add_selected() {
    var items = Array();
    var all_items = document.getElementsByName('items[]');
    for (var i=0; i<all_items.length; i++) {
        if (all_items[i].checked) {
            items[items.length] = all_items[i].value;
        }
    }
    opener.add_tracks(items);
    window.close();
}
