// all of the javascript for merples.com

// Function to handle rollover of images (header)
function roll(img_name, img_src, img_text) {
   document[img_name].src = "/images/"+img_src;

   if (img_text != undefined) {
  	 document.getElementById("linkname").innerHTML = img_text;
	}
   else {
	document.getElementById("linkname").innerHTML = "&nbsp;";
	}
   }

// Resize iframe
function resizeFrame(f) {
f.style.height = f.contentWindow.document.body.scrollHeight + "px";
}


// For input form, autofills the most common pairing from the fandom
function set_pairing() {
	var select_value = document.announce_form.fandom.options[document.announce_form.fandom.selectedIndex].value;

	if (select_value == 'Due South') {
		var default_pair = 'Fraser/RayK';
	}
	else if (select_value == 'The Sentinel') {
		var default_pair = 'Jim/Blair';
	}
	else if (select_value == 'Stargate: Atlantis') {
		var default_pair = 'John/Rodney';
	}
	else if (select_value == 'Other') {
		var default_pair = '';
	}

	if (default_pair != undefined) {	// Only act if something has been chosen above (i.e. not if the person chose 'other')
		//if (document.announce_form.pairing.value == '') {	// Only act if pairing field is empty - in case they filled it in first.
		document.announce_form.pairing.value = default_pair;
		//}
	}
}


// Data validation for input form
function validate_form(){
	
	counter = 0; // initialize error counter

	// Check for required fields
	things = ["title","author","type","fandom","date","url"];

	for (i in things) {

		thing = things[i];

		if (document.announce_form[thing].value == '') {
			document.getElementById("error").innerHTML += "<br>"+thing+" is required.";
			counter++;
			}
	}

	// Check for 255-or-less fields
	textlines = ["title","author","type-other","fandom-other","pairing","rating","size","url","thumburl"];

	for (i in textlines) {
		line = textlines[i];

		if (document.announce_form[line].value.length > 255) {
			document.getElementById("error").innerHTML += "<br>"+thing+" cannot exceed 255 characters.";
		}
	}		         	

	// Check date format
	var legaldate = /^\d{4}-\d{2}-\d{2}$/; // Match IntIntIntInt-IntInt-IntInt
	var datevalue = document.announce_form["date"].value;
	
	if (datevalue.search(legaldate) == -1) {	// if match failed
		document.getElementById("error").innerHTML += "<br>Date must be in the form: YYYY-MM-DD.";
		counter++;		
	}

	// Check sequence format
	var legalseq = /^\d{0,255}$/; // match any number of digits up to 255
	var seqvalue = document.announce_form["sequence"].value;

	if (seqvalue.search(legalseq) == -1) { //if match fails
		document.getElementById("error").innerHTML += "<br>Sequence must be an integer. Standard episode ID format is [season][epi epi], i.e. season 1, episode 1 is 101.";
		counter++;
	}	
	
	// once done checking, return false if error counter was incremented (so it will show all error messages, not stop when it hits one)
	if (counter > 0) {
		return false;
		} 
}
