//this function is used to snhow and hide elements of the DOM in the content admin page
function menuOperations(showDiv) {
	//hide all divs then show showDiv
	document.getElementById('images').style.display = 'none';
	document.getElementById('music').style.display = 'none';
	document.getElementById('media').style.display = 'none';
	document.getElementById('urls').style.display = 'none';	
	document.getElementById(showDiv).style.display = '';
	
	//create selected div id
	if (showDiv == 'images') {
		selectedDiv = 'imagesMenu';		
	} else if (showDiv == 'music') {
		selectedDiv = 'musicMenu';		
	} else if (showDiv == 'media') {
		selectedDiv = 'mediaMenu';
	} else if (showDiv == 'urls') {
		selectedDiv = 'urlsMenu';				
	}
	
	//deselect all divs then select selectedDiv
	document.getElementById('imagesMenu').className = 'menu';
	document.getElementById('musicMenu').className = 'menu';
	document.getElementById('mediaMenu').className = 'menu';
	document.getElementById('urlsMenu').className = 'menu';	
	document.getElementById(selectedDiv).className = 'menu menuSelected';
}

//removes content inputs boxes from the dom
function removeContent(contentType, contentCount, contentDOM) {				
	var removeDIV = document.getElementById(contentType + contentCount); //div to remove
	var contentDIV = document.getElementById(contentDOM); //div to remove from		
	contentDIV.removeChild(removeDIV); //remove the div				

	//if there is only one image left 			
	if (contentCount == '-1') {
		document.getElementById('addFirst_' + contentType).style.display = ''; //displays add content button
		document.getElementById('showHide_' + contentType).style.display = 'none'; //hides show hide button
	}
	else {
		var lastCount = parseInt(contentCount.substring(2,1)) - 1; //finds the count of the last item
		
		document.getElementById('add_' + contentType + '-' + lastCount).style.display = ''; //sets last add content button to display
		document.getElementById('remove_' + contentType + '-' + lastCount).style.display = ''; //sets last remove content button to display			
	}
}	
	
//adds content input boxes to the dom
function addContent(contentType, contentCount, contentDOM) {		
	var nextCount = parseInt(contentCount.substring(contentCount.length,1)) + 1; //finds the count of the next item
	
	//if (contentType == 'image' && contentCount.substring(2,1) == '10') {
		//alert('maximum of 10 images per feed item')	;
	//}
	//else {				
		var contentHTML = document.getElementById(contentType + contentCount).innerHTML; //grabs html
		var replaceCount = new RegExp(contentCount,"g"); //create regular expression to use in replace
		contentHTML = contentHTML.replace(replaceCount, '-' + nextCount); //replaces all names and ids etc with the next count					
		contentHTML = contentHTML.replace(/selected="selected"/g, ''); //unselects all boxes
		contentHTML = contentHTML.replace(/margin-bottom: 10px;/g, ''); //unselects all boxes		
		var newDIV = document.createElement('div'); //creates new div
		newDIV.setAttribute('id', contentType + '-' + nextCount) //gives the new div an id
		newDIV.innerHTML = contentHTML //adds html grabbed earlier to div
		document.getElementById(contentDOM).appendChild(newDIV); //append new div to contents dom	
	
		if (contentCount == '-0') {
			document.getElementById('addFirst_' + contentType).style.display = 'none'; //hides add content button
			document.getElementById('showHide_' + contentType).style.display = ''; //displays show hide button
		}
		else {	
			document.getElementById(contentType + 'Preview-' + nextCount).src = 'http://jtbanddjchuck-e.co.uk/images/blankthumb.gif';
			document.getElementById('add_' + contentType + contentCount).style.display = 'none'; //set last add content button to hidden
			document.getElementById('remove_' + contentType + contentCount).style.display = 'none'; //set last remove content button to hidden			
		}
	//}
}

//shows and hides content groups
function showHideContent(contentType, showImg, hideImg) {
	if (document.getElementById(contentType).style.display == 'none') {
		document.getElementById(contentType).style.display = '';
		document.getElementById('showHideImg_' + contentType).src = hideImg;
	}
	else {
		document.getElementById(contentType).style.display = 'none';
		document.getElementById('showHideImg_' + contentType).src = showImg;
	}
}

//ajax function to return the thumbnail image when selected in a select box
	function showImagePreview(imageFormNum) {
	var imageID = document.getElementById('imageList' + imageFormNum).value; //holds image id of the selected image
	var xmlHttp; //ajax variable
	
	//browser compatibility check
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} 
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}	
		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	
	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4) {
			//xmlHttp.responseText.substring(2,1) add update of .href to give image enlarge
			document.getElementById('imagePreview' + imageFormNum).src = xmlHttp.responseText; //change source of img tag to the filepath returned by ajax
			var siteDomain = xmlHttp.responseText.substring(0, 39);
			var previewLarge = xmlHttp.responseText.substring(39);
			previewLarge = previewLarge.replace(/_thumb/, '');
			
			document.getElementById('previewLarge' + imageFormNum).href = siteDomain + previewLarge; //change source of img tag to the filepath returned by ajax
			document.getElementById('loadingImage' + imageFormNum).style.display = 'none'; //set loading image to hidden
							
			//add padding to the css if there is an image selected
			if (xmlHttp.responseText == '') {
				document.getElementById('imagePreview' + imageFormNum).style.marginBottom = 'http://jtbanddjchuck-e.co.uk/images/blankthumb.gif';								
			}
			else {
				document.getElementById('imagePreview' + imageFormNum).style.marginBottom = '10px';								
			}
		}
	}
	
	xmlHttp.open("GET","http://jtbanddjchuck-e.co.uk/includes/returnimage.php?width=20&height=20&imageID=" + imageID,true);
	xmlHttp.send(null);
}

//form validation
//submit form to here and call validation methods into this function
function validateFormOnSubmit(theForm, valFields) {
	var reason = "";
	
	if (valFields == 'contentImage') {
		reason += validateEmpty(theForm.imageDescription);
	}
	else if (valFields == 'contentMusic') {
		reason += validateEmpty(theForm.musicDescription);	
	}
	else if (valFields == 'contentMedia') {
		reason += validateEmpty(theForm.mediaDescription);
	}
	else if (valFields == 'contentURL') { 
		reason += validateEmpty(theForm.url);
		reason += validateEmpty(theForm.urlDescription);
	}
	else if (valFields == 'booking') { 
		reason += validateEmpty(theForm.date);
		reason += validateEmpty(theForm.event);
		reason += validateEmpty(theForm.venue);		
		reason += validateEmpty(theForm.address);		
	}
	else if (valFields == 'biography') {
		reason += validateEmpty(theForm.title);			
	}
	else if (valFields == 'discography') {
		reason += validateEmpty(theForm.artist);			
		reason += validateEmpty(theForm.title);			
		reason += validateEmpty(theForm.label);			
	}
	else if (valFields == 'galleries') {
		reason += validateEmpty(theForm.title);			
		reason += validateEmpty(theForm.description);				
	}
	else if (valFields == 'news') {
		reason += validateEmpty(theForm.author);			
		reason += validateEmpty(theForm.title);
	}
	      
  	if (reason != "") {
    	alert("Please check the form details. " + reason);
    	return false;
  	}
	
	if (valFields.substring(0,7) == 'content' && valFields != 'contentURL') {
		var id = 'loading' + valFields.substring(7);
		document.getElementById(id).style.display = '';	
	}
	
  	return true;
}

//validate fields for data
function validateEmpty(fld) {
    var error = "";
	 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "You must complete the yellow fields."
    } else {
        fld.style.background = 'White';
    }
    return error;  
}