// JavaScript Document

//Set and forget command that will allow ALL ampersands to be replaced.
String.prototype.replaceAll=function(s1, s2) {return this.split(s1).join(s2)}

//A function to trim off all unnecessary spaces.
function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
	sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
	sString = sString.substring(0,sString.length-1);
	}
	return sString;
}


// Publishers, ComicsTitles etc section, checking that the new record name (i.e. Comic name) is not blank or is not equal to the default value.
function verifyNewName()
{
	if(trimAll(document.getElementById("NewName").value)=="")
	{
		alert("Please type in a comic title.");
		document.getElementById('NewName').focus()
		return false;
	}
}



function hideButtons()
{
	document.getElementById('ID').value = '';
	document.getElementById('FileTagID').value = 0;
	document.getElementById('txtHint').style.visibility='hidden'; // hide the Text Hint box	
	document.getElementById('OtherTagRow').style.display = 'none';	//Hide the Target textbox row	
	
	document.getElementById('SubmitBtn').style.visibility='hidden'; // hide the Submit button
	document.getElementById('ResetBtn').style.visibility='hidden'; // hide the Reset button
	document.getElementById('DeleteBtn').style.visibility='hidden'; // hide the Delete button
	
	document.getElementById("duplicationCheckDiv").style.visibility = "hidden";	// hide the Duplicate Details DIV
}


function resetGalleryForm()
{
	// Clearing the file input field
	//clearFileInputField('uploadFile_div')
	
	document.getElementById('GalleryTitle').value = 0;	
	document.getElementById('ID').value = '';	
	document.getElementById('ComicTitleDetails').value = 0;	
	document.getElementById('PublisherID').value = 0;		
	document.getElementById('Date').value = '';		
	document.getElementById('FrameNotes').value = '';	
	document.getElementById('ThanksTo').value = '';
	document.getElementById("BetterImageRequired").checked = false;
	CKEDITOR.instances.ContentDetails.setData('');	// Clear the Contents Details textarea box.
	document.getElementById('largeImage').innerHTML = "";

	document.getElementById('SubmitBtn').style.visibility='hidden'; // hide the Submit button
	document.getElementById('ResetBtn').style.visibility='hidden'; // hide the Reset button
	document.getElementById('DeleteBtn').style.visibility='hidden'; // hide the Delete button	
}



// Show the form buttons after something has been typed into the YEAR box
function showButtons()
{
	// Ensure that something is contained within the 'Year' box.
	if(trimAll(document.getElementById("Year").value) == "")
	{
		document.getElementById('SubmitBtn').style.visibility='hidden'; // hide the Submit button
		document.getElementById('ResetBtn').style.visibility='hidden'; // hide the Reset button
		document.getElementById('DeleteBtn').style.visibility='hidden'; // hide the Delete button
	}
	else
	{
		document.getElementById('SubmitBtn').style.visibility='visible'; // show the Submit button
		document.getElementById('ResetBtn').style.visibility='visible'; // show the Reset button
	}
}


function showButtonsGallery()
{	
	// Ensure that something has been selected from the Comic Title, Frame Notes, Publisher and Gallery Image DDL's.
	if(document.getElementById("ComicTitleDetails").value != "" && document.getElementById("ComicTitleDetails").value != 0 && trimAll(document.getElementById("FrameNotes").value) != "")
	{
		document.getElementById('SubmitBtn').style.visibility='visible'; // show the Submit button
		document.getElementById('ResetBtn').style.visibility='visible'; // show the Reset button
		document.getElementById('DeleteBtn').style.visibility='visible'; // show the Delete button
	}
	else
	{
		document.getElementById('SubmitBtn').style.visibility='hidden'; // hide the Submit button
		document.getElementById('ResetBtn').style.visibility='hidden'; // hide the Reset button
		document.getElementById('DeleteBtn').style.visibility='hidden'; // hide the Delete button
	}
}



// Validate the Comment form on the Annuals and Specials section
function ValidateCommentForm()
{	
	var CommentBox = document.getElementById('CommentBox');
	var EmailAddress = document.getElementById('EmailAddress');
	
	if(isEmpty(CommentBox, "Comments box is empty."))
	{
		if(emailValidator(EmailAddress, "Please enter a valid e-mail address"))
		{
			return true;
		}
	}	
	return false;
}

function isEmpty(elem, helperMsg)
{
	if(trimAll(elem.value) == "")
	{
		alert(helperMsg);
		elem.focus();
		return false;
	}
	return true;
}

function emailValidator(elem, helperMsg)
{
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}
	else
	{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}


// Reset the First, Last, Only radio buttons on the Admin Annuals page.
function resetFLO()
{
	document.getElementById("FirstLastOnly1").checked = false;
	document.getElementById("FirstLastOnly2").checked = false;
	document.getElementById("FirstLastOnly3").checked = false;
}



// Reset the Image Required and Better Image Required radio buttons on the Admin Annuals page.
function resetIR()
{
	document.getElementById("ImageRequired1").checked = false;
	document.getElementById("ImageRequired2").checked = false;
}



// Reset the Image Required and Better Image Required radio buttons on the Admin Annuals page.
function resetCR()
{
	if(document.getElementById("ClarificationRequired2").checked)
	{
		document.getElementById("FrameNotes").value = "";
	}
	
	document.getElementById("ClarificationRequired1").checked = false;
	document.getElementById("ClarificationRequired2").checked = false;
}


// Will clear the contents of a File Input Textbox
function clearFileInputField(tagId) 
{
    document.getElementById(tagId).innerHTML = document.getElementById(tagId).innerHTML;
}




function closeIKnowValidation()
{
	document.getElementById("IKnowValidation").style.visibility = "hidden"
}



function formValidationAdminAS(TableName)
{
	// Calling the AJAX routine to test for Duplicate Record
	duplicationCheck(TableName);
	
	if(document.getElementById("FileTagID").value == 999 && trimAll(document.getElementById("OtherTag").value) == "")
	{
		alert("Because you have selected 'Other' in the File Tag dropdown list, you must type in a new unique File Tag name. As you type, you will be guided as to the availability of your new tag.");	
		document.getElementById("OtherTag").focus();
		return false;
	}
	return true;
}







//Ensuring that only numbers (and '-'s, 'v' and 'V', 'n' and 'N', 'a' and 'A', 'f' and 'F', 's' and 'S' are accepted) within the date field boxes
function isNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : evt.keyCode
	//alert( "charCode: " + charCode);
	// 65-90 = a-z;    97-122 = A-Z;   32 = space;    45 = -;  48-57 = 0-9; 8 = Backspace Key; 9 = Tab Key; 13 = Enter Key;  46 = Delete;  37 = Left Arrow;   39 = Right Arrow;
	if ((charCode >= 48 && charCode <= 57) || charCode == 45 || charCode == 8 || charCode == 9  || charCode == 13 || charCode == 46 || charCode == 37 || charCode == 39)
	{
		// Number or space etc.
		return true;					
	}
	else
	{
		// Letter
		return false;
	}
}




function dateValidation(evt, textLength, boxName, contents)
{		
	var charCode = (evt.which) ? evt.which : evt.keyCode

	// Ensuring that the 3rd and 6th characters are '/' (JavaScript is Zero-based) 
	if((textLength == 2 || textLength == 5) && charCode != 47 && charCode != 8 && charCode != 37 && charCode != 39)
	{
		var dateBoxContents = document.getElementById(boxName).value;
		dateBoxContents = dateBoxContents.substring(0,dateBoxContents.length) + "/";
		document.getElementById(boxName).value = dateBoxContents;
		return false;
	}

	if(textLength != 2 && textLength != 5)
	{	
		//  48-57 = 0-9;  65-90 = a-z;  97-122 = A-Z; 32 = space;  47=/;  8 = Backspace Key; 9 = Tab Key; 13 = Enter Key;  46 = Delete; 35=End; 36 = Home 37 = Left Arrow;   39 = Right Arrow; 109 = -; 190 = .;
		
		if ((charCode >= 48 && charCode <= 57) || charCode == 8 || charCode == 9  || charCode == 13 || charCode == 35 || charCode == 36 || charCode == 37 || charCode == 39 || charCode == 46 || charCode == 47)
		{	
			// Valid character
			return true;					
		}
		else
		{		
			// Illegal character
			return false;
		}
	}
}


function validateGalleryForm()
{
	// Date verification
	textLength = document.getElementById('Date').value.length;
	
	// checking that the date is <= 31.
	var dateBox = document.getElementById('Date').value;
	
	theDate = dateBox.substring(0, 2);
	if(theDate > 31)
	{
		alert ("Invalid Date");
		highlightText(document.getElementById('Date'),0,2)
		document.getElementById("Date").focus();
		return false;
	}			

	// checking that the month is <= 12.
	theMonth = dateBox.substring(3, 5);
	if(theMonth > 12)
	{
		alert ("Invalid Month");
		highlightText(document.getElementById('Date'),3,5)
		document.getElementById("Date").focus();
		return false;
	}	

	// checking that the year is not in the future.
	theYear = dateBox.substring(6);
	
	// Getting the current year
	var d = new Date();                                               
	var curr_year = d.getFullYear();     
	
	if(theYear > curr_year || (theYear.length > 0 && theYear.length < 4))
	{
		alert ("Invalid Year");
		highlightText(document.getElementById('Date'),6,10)
		document.getElementById("Date").focus();
		return false;
	}	
	
	return true
}


// Highlight text in a Textbox (doesn't work to well in IE)
function highlightText(element,begin,end)
{
	if (element.setSelectionRange)
	{
		// Firefox browser
		element.setSelectionRange(begin,end);
	}
	else if (element.createTextRange)
	{
		// IE Browser
		var range = element.createTextRange();
		range.moveStart("character",begin);
		range.moveEnd("character",end);
		range.select();
	}
}


// Populate the Frame Notes box with some default settings.
function populateFrameNotes(ComicTitle, AdminName)
{
	var preText;
	preText = "";
	if(document.getElementById("Year").value != ""){preText = document.getElementById("Year").value + ": ";}
	document.getElementById("FrameNotes").value = preText + "Clarification is required for the year of this " + ComicTitle + " Annual. If you can positively identify it, please press the 'Add Details' button below and send me the information. Thank you, " + AdminName + ".";
}


function populateThumbnailNotes1()
{
	document.getElementById("ThumbnailNotes").value = document.getElementById("Year").value;
}

function populateThumbnailNotes2()
{
	var w = document.theForm.FileTagID.selectedIndex;
	var selected_text = document.theForm.FileTagID.options[w].text;
	
	if(document.getElementById("Year").value != "")
	{
		document.getElementById("ThumbnailNotes").value = document.getElementById("Year").value + " " + selected_text;
	}
}

function populateThumbnailNotes3()
{
	if(document.getElementById("Year").value != "")
	{	
		document.getElementById("ThumbnailNotes").value = document.getElementById("Year").value + " " + document.getElementById("OtherTag").value;
	}
	return true
}




function FileTagBox(choice)
{
	if(choice==0)	// clearOtherTagBox
	{
		document.getElementById("OtherTagRow").style.display = 'none';		// Hide the Target textbox row
		document.getElementById("OtherTag").value = "";	
		populateThumbnailNotes1();
	}
	else
	{
		if(choice==999)	// showOtherTagBox
		{
			document.getElementById("txtHint").innerHTML = "";				// Clearing the contents of the DIV box before anything has been entered.
			document.getElementById("OtherTagRow").style.display = '';		// Show the Target textbox row
			document.getElementById("OtherTag").focus()						// Giving the focus to the Target textbox			
		}
		else
		{
			// Toggle of the OtherTag row
			{
				document.getElementById('OtherTagRow').style.display = 'none';	//Hide the Target textbox row
				document.getElementById('OtherTag').value = "";
				populateThumbnailNotes2();
			}			
		}
	}
}




