function change_value(newvalue) {
	document.getElementById("adtype").value = newvalue;
}

// TOGGLES

function show_hide_div(divid) {
	if ( document.getElementById(divid).className == "hide" ) {
		document.getElementById(divid).className = "show"
	} else {
		document.getElementById("images").className = "hide"
	}
}

function show_hide_ul(ulid) {
	if ( document.getElementById(ulid).className == "hide" ) {
		document.getElementById(ulid).className = "show"
	} else {
		document.getElementById(ulid).className = "hide"
	}
}

function show_hide_images_div(x) {
	document.getElementById("type_option_0").style.fontWeight = "normal";
	document.getElementById("type_option_1").style.fontWeight = "normal";
	document.getElementById("type_option_2").style.fontWeight = "normal";
	document.getElementById("type_option_3").style.fontWeight = "normal";
	
	if ( x==0 ) {
		document.getElementById("images").className = "hide";
	} else {
		document.getElementById("images").className = "show";
	}
	var hiddenInput = document.getElementById('adtype');
	if(hiddenInput){
		hiddenInput.value = x;
	}
	
	document.getElementById("type_option_"+x).style.fontWeight = "bold";
	
	change_value(x);
}

function show_hide_urlblock_div(x) {
	document.getElementById("type_option_0").style.fontWeight = "normal";
	document.getElementById("type_option_1").style.fontWeight = "normal";
	
	if ( x==0 ) {
		document.getElementById("urlblock").className = "hide";
	} else {
		document.getElementById("urlblock").className = "show";
	}
	var hiddenInput = document.getElementById('adtype');
	if(hiddenInput)	
	document.getElementById("type_option_"+x).style.fontWeight = "bold";
	
	change_value(x);
}

function limitText (limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;	
	}
}

/* Functions for adding/removing event occurences */

function addEventOccurenceRow(numOccurencesId,numRowsId,firstRowDivId,occurencesDivId){
	var numOccurences = document.getElementById(numOccurencesId);
	var numRows = document.getElementById(numRowsId);
	var firstrow = document.getElementById(firstRowDivId);
	var occurences = document.getElementById(occurencesDivId);
	
	var content = firstrow.innerHTML;
	content = content.replace(/\[X\]/g,'[' + numRows.value + ']');
	
	var divId = 'row_' + numRows.value;
	
	jQuery('#'+occurencesDivId).append(
	'<div id="' + divId + '"class="input daterange onerow">' + content + '</div>'
	);
	
	numOccurences.value = parseInt(numOccurences.value) + 1;
	numRows.value = parseInt(numRows.value) + 1;
	
	if(parseInt(numOccurences.value) > 1){
		occurences.className = "morethanone";
	}else{
		occurences.className = "onlyone";
	}
	return false;
}

function removeEventOccurenceRow(numOccurencesId,numRowsId,rowId,occurencesDivId){
	var numOccurences = document.getElementById(numOccurencesId);
	var occurences = document.getElementById(occurencesDivId);
	
	rowId = rowId.replace(/\[(\d+)\]/g,'_$1');
	var eventRow = document.getElementById(rowId);
	if(eventRow){
		jQuery('#'+ rowId).remove();
		numOccurences.value = parseInt(numOccurences.value) - 1;
	}
	
	if(parseInt(numOccurences.value) > 1){
		occurences.className = "morethanone";
	}else{
		occurences.className = "onlyone";
	}

	return false;
}