

////////////////

// DRAG AND DROP ELEMENTS
var mousex = 0;
var mousey = 0;
var grabx = 0;
var graby = 0;
var orix = 0;
var oriy = 0;
var elex = 0;
var eley = 0;
var algor = 0;

var dragobj = null;

function falsefunc() { return false; } // used to block cascading events

function init()
{
  document.onmousemove = update; // update(event) implied on NS, update(null) implied on IE
  update();
}

function getMouseXY(e) // works on IE6,FF,Moz,Opera7
{ 
  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)
 
  if (e)
  { 
    if (e.pageX || e.pageY)
    { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
      mousex = e.pageX;
      mousey = e.pageY;
      algor = '[e.pageX]';
      if (e.clientX || e.clientY) algor += ' [e.clientX] '
    }
    else if (e.clientX || e.clientY)
    { // works on IE6,FF,Moz,Opera7
      // Note: I am adding together both the "body" and "documentElement" scroll positions
      //       this lets me cover for the quirks that happen based on the "doctype" of the html page.
      //         (example: IE6 in compatibility mode or strict)
      //       Based on the different ways that IE,FF,Moz,Opera use these ScrollValues for body and documentElement
      //       it looks like they will fill EITHER ONE SCROLL VALUE OR THE OTHER, NOT BOTH 
      //         (from info at http://www.quirksmode.org/js/doctypes.html)
      mousex = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
      mousey = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
      algor = '[e.clientX]';
      if (e.pageX || e.pageY) algor += ' [e.pageX] '
    }
  }
}

function update(e)
{
  getMouseXY(e); // NS is passing (event), while IE is passing (null)
}

function grab(context)
{
  document.onmousedown = falsefunc; // in NS this prevents cascading of events, thus disabling text selection
  dragobj = document.getElementById(context);
  dragobj.style.zIndex = 1000000; // move it to the top
  document.onmousemove = drag;
  document.onmouseup = drop;
  grabx = mousex;
  graby = mousey;
  elex = orix = dragobj.offsetLeft;
  eley = oriy = dragobj.offsetTop;
  update();
}

function drag(e) // parameter passing is important for NS family 
{
  if (dragobj)
  {
    elex = orix + (mousex-grabx);
    eley = oriy + (mousey-graby);
    //dragobj.style.position = "absolute";
	dragobj.style.marginLeft = 0 + 'px';
    dragobj.style.marginTop = 0 + 'px';
    dragobj.style.left = (elex).toString(10) + 'px';
    dragobj.style.top  = (eley).toString(10) + 'px';
	
  }
  update(e);
  return false; // in IE this prevents cascading of events, thus text selection is disabled
}

function drop()
{
  if (dragobj)
  {
    dragobj.style.zIndex = 20;
    dragobj = null;
  }
  update(e);
  document.onmousemove = update;
  document.onmouseup = null;
  document.onmousedown = true;   // re-enables text selection on NS
}

function nl2br (str, is_xhtml) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Philip Peterson
    // +   improved by: Onno Marsman
    // +   improved by: Atli Þór
    // +   bugfixed by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   improved by: Maximusya
    // *     example 1: nl2br('Kevin\nvan\nZonneveld');
    // *     returns 1: 'Kevin<br />\nvan<br />\nZonneveld'
    // *     example 2: nl2br("\nOne\nTwo\n\nThree\n", false);
    // *     returns 2: '<br>\nOne<br>\nTwo<br>\n<br>\nThree<br>\n'
    // *     example 3: nl2br("\nOne\nTwo\n\nThree\n", true);
    // *     returns 3: '<br />\nOne<br />\nTwo<br />\n<br />\nThree<br />\n'

    var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';

    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}

function startTimer(fullTime,prefix){
	var alertTimerId = 0;
	alertTimerId = setTimeout("alarm(prefix)",fullTime);
	
	return alertTimerId;
}

function stopTimer(timerID){
	clearTimeout(timerID);
}

function alarm(prefix){
		document.location.replace(prefix + 'logout.php?inactivity=true');
}


///////////////

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;
	var done = false;
	
    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
			
			if(i == opacEnd){
				done = true;	
			}
			
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
	
	if(done){// Getting rid of the div
		setTimeout("hideDiv('" + id + "')",millisec);
	}
}

function goTo(url) {
	document.location.replace(url);	
}




//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

function fadeDiv(id) {
	//safe function to fade an element with a specified id
		
	// alert(id + 'will be faded by ' + time + ' milliseconds.');
	
	if (document.getElementById) { // DOM3 = IE5, NS6
		
		if(opacity(id,100,0,500)){
			//
		}
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-100000);
}


function winPop(url,name,width,height)
{
 var width  = width;
 var height = height;
 var left   = (screen.width  - width)/4;
 var top    = (screen.height - height)/4;
 var params = 'width='+width+', height='+height;
 params += ', top='+top+', left='+left;
 params += ', directories=no';
 params += ', location=no';
 params += ', menubar=no';
 params += ', resizable=yes';
 params += ', scrollbars=yes';
 params += ', status=no';
 params += ', toolbar=no';
 newwin=window.open(url,name, params);
 if (window.focus) {newwin.focus()}
 return false;
}


function switchId(id,ids){	
	hideallIds(ids);
	showDiv(id);
}

function hideallIds(idsString){
	var ids = idsString.split(',');
	
	//loop through the array and hide each element by id
	for (var i=0;i<ids.length;i++){
		hideDiv(ids[i]);
	}		  
}
function makeInput(id,type,name)
{	
	var Output = '<input type="' + type + '" value="" name="' + name + '" id="' + name + '" maxlength="100";" />';
	document.getElementById(id).innerHTML = Output;
	document.getElementById(name).focus();
}
function deleteInput(id)
{
	document.getElementById(id).innerHTML = '';
}

function addValue(idTO,idFROM)
{
	document.getElementById(idTO).value = document.getElementById(idFROM).value;
}
function addTextEditValue(idTO,idFROM)
{
	document.getElementById(idTO).value = document.getElementById(idFROM).innerHTML;
}

function showEdit(element,id){
	var Output = '<a href="../pop.php?a=14&artistId=' + id +'" class="minilink">Edit Artist</a>';
	
	if(id > 0)
		document.getElementById(element).innerHTML = Output;
}
function hideEdit(element,id)
{
	document.getElementById(element).innerHTML = '';
}

function rand (minimum, maximum)
{
  return (Math.floor(Math.random() * maximum + minimum) );
}


function moveDiv(id,top,left){
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.marginTop =  - top + 'px';
		document.getElementById(id).style.marginLeft = - left + 'px';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.marginTop = - top + 'px';
			document.id.marginLeft = - left + 'px';
		}
		else { // IE 4
			document.all.id.style.marginTop = - top + 'px';
			document.all.id.style.marginLeft = - left + 'px';
		}
	}
}

function showDiv(id,display,randomize)
{

 display = (typeof display == 'undefined') ? 'block' : display;
 randomize = (typeof randomize == 'undefined') ? false : randomize;

	//safe function to show an element with a specified id
	if(randomize){
		var top = rand(0,350);
		var left = rand(0,350);
		moveDiv(id,top,left);
	}
	
	
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = display;
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = display;
		}
		else { // IE 4
			document.all.id.style.display = display;
		}
	}
	
	
}
function hideDiv(id)
{
	//safe function to show an element with a specified id
		  
	if (document.getElementById) { // DOM3 = IE5, NS6		
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function hideDivTime(id,time){		  
	setTimeout("hideDiv(id)",time);
}

function replaceDiv(id, replaceId,display) {
	//safe function to show an element with a specified id

  display = (typeof display == 'undefined') ? 'block' : display;

var show = false;
var hide = false;

if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = display;
		show = true;
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = display;
			show = true;
		}
		else { // IE 4
			document.all.id.style.display = display;
			show = true;
		}
	}
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(replaceId).style.display = 'none';
		hide = true;
	}
	else {
		if (document.layers) { // Netscape 4
			document.replaceId.display = 'none';
			hide = true;
		}
		else { // IE 4
			document.all.replaceId.style.display = 'none';
			hide = true;
		}
	}
	
	if(show && hide)
		return true;
	else
		return false;
}

<!-- Adding an image to the TET EDIT window
function AddImage(imgSrc){
	
	if(document.form)
		var width = document.form.width.value;
	else
		var width = 300; 
	
	var html = '<img src="' + imgSrc + '" width="' + width +'" heigth="auto" border="0" />';
	
		window.opener.insertHTML(html);
		window.close();
	
}

function scrollToBottom(){
		window.scrollTo(0, f_clientHeight());
	}
	

function submitForm() {
//make sure hidden and iframe values are in sync before submitting form
//updateRTE('newText'); //use this when syncing only 1 rich text editor ("rtel" is name of editor)
updateRTEs(); //uncomment and call this line instead if there are multiple rich text editors inside the form
//alert(document.addTrackInfo.trackInfo.value) //alert submitted value
return true; //Set to false to disable form submission, for easy debugging.
}

function submitFormUnique(id) {
//alert('hej hej');
//make sure hidden and iframe values are in sync before submitting form
updateRTE(id);//use this when syncing only 1 rich text editor ("rtel" is name of editor)
//alert(document.getElementById(id).innerHTML);
return true;
}

function writeDays(tot,inputId){
	var days;
	var dayId = 'day' + inputId;
	
	
	for(var i = 01; i <= tot; i++){
	
		if(i < 10)
			days = days + '<option value="0' + i + '" id="day0' + i + '_' + inputId + '">0' + i + '</option><br />';
		else
			days = days + '<option value="' + i + '" id="day' + i + '_' + inputId + '">' + i + '</option><br />';
	}
	
	// Insert inte the day select
	document.getElementById(dayId).innerHTML = days;
}

function copyLive(){
	document.getElementById('a').value = 19;		
}

function message(html){
	document.getElementById('message').style.top = f_scrollTop();
	
	
	// document.getElementById('messageBoard').innerHTML = html;
	showDiv('message');
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function checkEmail(email) {	
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (!filter.test(email))
		return false;
	else
		return true;
}

function titleLenght(inputId,spanId,totLenght){
	var title = document.getElementById(inputId).value;
	document.getElementById(spanId).innerHTML = title.length + '/' + totLenght;
}
							
function changeImage(newImageId,newImageSrc,image,imageId){
	if (document.getElementById){
		opener.document.getElementById(image).src = newImageSrc;
		opener.document.getElementById(imageId).value = newImageId;
		opener.focus();
		self.close();
	}
}

function submitSearch(){
	var msg = '';
	var answer = true;
	
	if(document.searchForm.searchFor.value == ''){
		msg = 'Enter a searchstring \n'; 	
		answer = false;
	}
	if(!answer) alert(msg);
	
	return answer;
}

function submitFormArtist(){
	var msg = '';
	var answer = true;
	
	if(document.addArtistInfo.name == ''){
		msg = 'Enter a name \n'; 	
		answer = false;
	}
	
	if(!answer) alert(msg);
	
	updateRTEs();
	return answer;
}

function submitFormAlbum(){
	var msg = '';
	var answer = true;
	
	if(document.addAlbum.name == ''){
		msg = 'Enter a name \n'; 	
		answer = false;
	}
	
	if(!answer) alert(msg);
	
	updateRTEs();
	return answer;
}

function submitReviewForm(inputTitle,inputLink,inputRTE,id){
	document.getElementById('reviewFormTitle' + id).value = document.getElementById(inputTitle).value;
	document.getElementById('reviewFormLink' + id).value = document.getElementById(inputLink).value;
	document.getElementById('reviewFormText' + id).value = document.getElementById(inputRTE).contentWindow.document.body.innerHTML;
	
	document.getElementById('reviewForm' + id).submit();
}

function submitLiveForm(inputTitle,inputPlace,inputInfo,id, startId){

	var year = 'year' + id;
	
	var start = document.getElementById('yymmdd' + id).value + ' ' + document.getElementById('hour' + id).value + ':' + document.getElementById('minute' + id).value;
	
	if(start){
	
		document.getElementById('liveFormTitle' + id).value = document.getElementById(inputTitle).value;
		document.getElementById('liveFormPlace' + id).value = document.getElementById(inputPlace).value;
		document.getElementById('liveFormInfo' + id).value = document.getElementById(inputInfo).value;
		document.getElementById('liveFormStart' + id).value = start;
		document.getElementById('liveForm' + id).submit();
	}
}

function submitLiveFormEdit(inputTitle,inputPlace,inputInfo,id, startId){

	var year = 'year' + id;
	
	var start = document.getElementById('yymmdd' + id).value + ' ' + document.getElementById('hour' + id).value + ':' + document.getElementById('minute' + id).value;

	if(start){
	
		document.getElementById('editLiveFormTitle' + id).value = document.getElementById(inputTitle).value;
		document.getElementById('editLiveFormPlace' + id).value = document.getElementById(inputPlace).value;
		document.getElementById('editLiveFormInfo' + id).value = document.getElementById(inputInfo).value;
		document.getElementById('editLiveFormStart' + id).value = start;
		document.getElementById('editLiveForm' + id).submit();
	}
}


function check(id){
		if(document.getElementById(id).checked == true) {
			document.getElementById(id).checked = false;
		}
		else
			document.getElementById(id).checked = true;
}

function minimize_cookie(id){
		var left = 0;
		var margin = '-32px 0px 0px 0px';
	
	var windows = readCookie('windowsMinimized');
	
		if(windows !== null){
			if(windows > 0 && windows <= 5){
				for(i=0;i<windows;i++){
					left = left + 205;
				}
			}
			else{
				if(windows >= 6 && windows <= 11){
					left = 0;
					margin = '-64px 0px 0px 0px';
					//alert(windows + 'above');
					for(i=6;i<windows;i++){
						left = left + 205;
					}
				}
			}
			createCookie(id,left,1);
			windows++;
			createCookie('windowsMinimized',windows,1);
		}
		else{
			createCookie('windowsMinimized',1,1);
			createCookie(id,0,1);
		}
		
	//	alert(windows + ' ' + left + ' ' + document.cookie);
	document.getElementById(id).style.width = 200 + 'px';
	document.getElementById(id).style.height = 27 + 'px';
	document.getElementById(id).style.top = 100 + '%';
	document.getElementById(id).style.left = left + 5 + 'px';
	document.getElementById(id).style.margin = margin;
	document.getElementById('blobLogo').style.fontsize = 12 + 'px';

	var onlyId = id.substr(8);
	replaceDiv('maximize'+ onlyId,'minimize'+ onlyId,'inline-block');
}

function maximize_cookie(id){
	document.getElementById(id).style.width = 600 + 'px';
	document.getElementById(id).style.height = '';
	document.getElementById(id).style.top = 50 + '%';
	document.getElementById(id).style.left = 50 + '%';
	document.getElementById(id).style.margin = '-300px 0px 0px -300px';
	document.getElementById('blobLogo').style.fontsize = 18 + 'px';
	eraseCookie(id);
	
	var windows = readCookie('windowsMinimized');
	windows--;
	createCookie('windowsMinimized',windows,1);
	
	var onlyId = id.substr(8);
	replaceDiv('minimize'+ onlyId,'maximize'+ onlyId,'inline-block');
}

function hover(id,color){
		document.getElementById(id).style.background = color;
	}
function deHover(id){
	document.getElementById(id).style.background = '';
}

function imageHover(type,id){
	document.getElementById(id).src = '/img/' + type + '_hover.png';
}
function imageDeHover(type,id){
	document.getElementById(id).src = '/img/' + type + '_dehover.png';
}

function deleteFinder(id){
	document.deleteForm.finderId.value = id;	
	document.deleteForm.submit();	
}
function deleteContact(id){
	document.deleteForm.contactId.value = id;	
	document.deleteForm.submit();	
}

/* MINIMIZE */

Array.prototype.exists = function(o) {
for(var i = 0; i < this.length; i++)
   if(this[i] === o)
     return true;
return false;
}

function setMinimizedIdsCookie(newId){
	if(readCookie('minimizedBarIds')){
		var oldIds = readCookie('minimizedBarIds');
		var newIds = oldIds + ',' + newId;
		createCookie('minimizedBarIds',newIds,2);
	}
	else{
		createCookie('minimizedBarIds',newId,2);
	}
}
function removeMinimizedCookieId(removeId){
	var newIds = '';
	
	if(readCookie('minimizedBarIds')){
		
		var oldIds = readCookie('minimizedBarIds');
		var oldIdsSPLIT = oldIds.split(',');			
		
		for(var i = 0 ; i < oldIdsSPLIT.lenght; i++){
			if(oldIdsSPLIT[i] != removeId)
				newIds = newIds + oldIdsSPLIT[i] + ',';
		}
		
	//	var newerIds = newIds.substr(0,newIds.lenght - 1);
		createCookie('minimizedBarIds',newIds,2);
	}
}

function setMinimizedBarCookie(){
	var html = document.getElementById('minimize_container').innerHTML;
	createCookie('minimizedBarHTML',html,2);
}

function makeMinimized(id,name){
	var html = document.getElementById('minimize_container').innerHTML;
	
	html += '<div id="minimized'+id+'" class="minimized"> <a href="javascript:maximize('+id+')">' + name + '</a></div>';
	
	document.getElementById('minimize_container').innerHTML = html;
	
	createCookie('minimizedBarHTML',html,2);
	
}
function minimize(id,name){
	hideDiv('editBlob' + id);
	if(!(document.getElementById('minimized'+id)))
		makeMinimized(id,name);
	else
		showDiv('minimized'+id,'inline-block');
	
	//setMinimizedIdsCookie(id);
		
}

function maximize(id){
	if(document.getElementById('editBlob'+id))
		replaceDiv('editBlob'+id,'minimized'+id,'inline-block');
	else
		hideDiv('minimized'+id);
	setMinimizedBarCookie();
	//removeMinimizedCookieId(id);
}

function mainHover(id){
	var object = document.getElementById(id);
	object.style.color = '#000';
}
function mainDeHover(id){
	var object = document.getElementById(id);
	object.style.color = '#ccc';
}

function checkTrackNum(newVal,oldVal,tot,id){
	var prevVal = '';
	for(var i=1;i<=tot;i++){
		var object = document.getElementById(id + '_' + i);
		
		if(i != oldVal){
			if(object.value == newVal && object.value != prevVal){
				object.value = oldVal;
				prevVal = newVal;
			}
			else
				prevVal = object.value;
		}
		
	}
	//document.getElementById('trackNum' + id + '_' + oldVal).id = newVal;
}

function setViewCart(queryString){
	document.location.replace('/setViewCart.php?' + queryString);
}	

function updateVisibleOn(id){
	
	var newVisible = '';
	var addId = true;

	var visibles = $('#visibleOn').val();
	
	if(visibles != ''){
		visibles = visibles.split(',');
		
		for(var i=0; i < visibles.length; i++){
			if(!(visibles[i] == id)){
				newVisible += visibles[i] + ',';
			}
			else
				addId = false;
				
		}

		if(addId)
			newVisible += id + ',';
	}
	else
		newVisible += id + ',';
		
	
	if(newVisible != ''){
		// REMOVE LAST ,
		newVisible = newVisible.substring(0,newVisible.length - 1);
		///alert(newVisible + ' -  ' +id);
		$('#visibleOn').val(newVisible);
	}
	else
		$('#visibleOn').val(' ');
	
}

