//Ing '09 - Bunches Blog Javascript doc

//=========
//* Widgets */
//=========

//Onload
window.onload = function() {
	//prepareLightbox();
}

//Get images ready for lightbox script
function prepareLightbox() {
	//Get all images
	var imageElements = document.getElementsByTagName('img');
	
	//Go through each image
	for (var i = 0; i < imageElements.length; i++) {
		if (imageElements[i].getAttribute('class') == 'lightbox') { //All lightbox images
			imageElements[i].onclick = function() {goLightbox(this.getAttribute('src'), this.getAttribute('alt')); return false;} //Set lightbox function to onclick
		}
	}
}

function goLightbox(src, alt) {
	
	//Edit src and alt to correct strings
	src = src.substring(src.indexOf('image/'), src.indexOf('w=100') - 1);
	alt = alt.substring(alt.indexOf('/') + 1, alt.indexOf('.jpg'));
	
	//Make image
	var theImage = document.createElement('img');
	theImage.setAttribute('src', src);
	theImage.setAttribute('alt', alt);
	
	//Make Paragraph
	var thePara = document.createElement('p');
	thePara.setAttribute('align', 'center');
	var theText = document.createTextNode(alt);
	thePara.appendChild(theText);

	//Put the image and text in the white div
	var theBox = document.createElement('div');
	theBox.setAttribute('id', 'lightbox');
	theBox.appendChild(theImage);
	theBox.appendChild(thePara);
	
	//Add a button for closing the lightbox
	var closeForm = document.createElement('form');
	var closeButton = document.createElement('input');
	closeButton.setAttribute('type', 'submit');
	closeButton.setAttribute('value', 'Close');
	closeButton.onclick = function() {closeLightbox();}
	theBox.appendChild(closeButton);
	
	//Put the white div in the fader div
	var theFade = document.createElement('div');
	theFade.setAttribute('id', 'fade');
	theFade.appendChild(theBox);
	
	//Insert everything
	document.body.appendChild(theFade);
	
	//Needs close button and function
	//Scroll functionality?
	//Image to large issues?
	
}

function closeLightbox() {
	
	//Find the fade div and remove it from DOM
	var removeThis = document.getElementById('fade');
	document.body.removeChild(removeThis);
	
}

//Login form @ admin.php validation
function checkLogin() {
	if (document.logi.name.value == '') {
		alert('Please Enter A Username');
		document.logi.name.focus();
	} else if (document.logi.pass.value == '') {
		alert('Please Enter A Password');
		document.logi.pass.focus();
	} else {
		document.logi.submit();
	}
}

//Function to force form submittal on enter key press (thieved)
function checkEnter(f, e) { //e is event object passed from function invocation, f = validation function
	var characterCode; //literal character code will be stored in this variable
	if(e && e.which) { //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	} else {
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}
	if (characterCode == 13) { //if generated character code is equal to ascii 13 (if enter key)
		eval(f);
		return false;
	} else {
		return true;
	}
}

//=============
//* Form Validation */
//=============

//Username Form @ admin.php validation
function checkUsername() {
	if ((document.un.un.value == '') || (document.un.un.value.length > 15)) {
		alert('Please Enter A Username of Between 1 and 15 Characters In Length');
		document.un.un.focus();
	} else {
		document.un.submit();
	}
}
//Password Form * admin.php validation
function checkPassword() {
	if ((document.pw.pass.value == '') || (document.pw.pass.value.length > 15)) {
		alert('Please Enter A Password fo Between 1 and 15 Characters In Length');
		document.pw.pass.focus();
	} else {
		document.pw.submit();
	}
}
//Add new link form @ admin.php validation
function checkLinkAdd(x) {
	if ((x.title.value == '') || (x.title.value.length > 25)) {
		alert('Please Enter A Title of Between 1 and 25 Characters In Length');
		x.title.focus();
	} else if ((x.address.value == '') || (x.address.value.length > 60)) {
		alert('Please Enter An Address of Between 1 and 60 Characters In Length');
		x.address.focus();
	} else {
		x.submit();
	}
}
//Update link form @ admin.php validation
function checkLinkEdit(x) {
	if ((x.title.value == '') || (x.title.value.length > 25)) {
		alert('Please Enter A Title of Between 1 and 25 Characters In Length');
		x.title.focus();
	} else if ((x.address.value == '') || (x.address.value.length > 60)) {
		alert('Please Enter An Address of Between 1 and 60 Characters In Length');
		x.address.focus();
	} else {
		x.submit();
	}
}
//Add pic to gallery @ admin.php form validation
function checkGallery() {
	var Reg = new RegExp('^[a-zA-Z0-9 ]+$');
	if ((document.galleryadd.title.value == '') || (!Reg.test(document.galleryadd.title.value)) || (document.galleryadd.title.value.length > 30)) {
		alert('Please Enter A Title Consisiting of Letters And Spaces Only, With a Max of 30 Characters');
		document.galleryadd.title.focus();
	} else if (document.galleryadd.uploadedfile.value == '') {
		alert('Please Enter An Image To Upload');
		document.galleryadd.uploadedfile.focus();
	} else if ((document.galleryadd.uploadedfile.value != '') && (document.galleryadd.uploadedfile.value.indexOf('.jpg') <= 0)) {
		alert('Images Must Be In Jpg Format');
		document.galleryadd.uploadedfile.focus();
	} else {
		document.galleryadd.submit();
	}
}
//Change photo * admin.php form validation
function checkPhoto() {
	if ((document.photoup.uploadedfile.value == '') || (document.photoup.uploadedfile.value.indexOf('.jpg') <= 0)) {
		alert('Please Enter An Image To Upload');
		document.photoup.uploadedfile.focus();
	} else {
		document.photoup.submit();
	}
}
//function for preview on add post screen
function goPreview() {
	if ((document.posta.title.value == '') || (document.posta.title.value.length > 50)) {
		alert('Please Enter A Title of Between 1 and 50 Characters In Length');
		document.posta.title.focus();
	} else {
		document.posta.action = 'admin.php?p=post2';
		WYSIWYG.updateTextArea('wys');
		document.posta.submit();
	}
}
//function for submit on add post screen
function goSubmit() {
	if ((document.posta.title.value == '') || (document.posta.title.value.length > 50)) {
		alert('Please Enter A Title of Between 1 and 50 Characters In Length');
		document.posta.title.focus();
	} else {
		document.posta.action = 'admin.php?p=post4';
		WYSIWYG.updateTextArea('wys');
		document.posta.submit();
	}
}
///function for preview on edit post screen
function goEditPreview() {
	if ((document.poste.title.value == '') || (document.poste.title.value.length > 50)) {
		alert('Please Enter A Title of Between 1 and 50 Characters In Length');
		document.poste.title.focus();
	} else {
		document.poste.action = 'admin.php?p=post2';
		WYSIWYG.updateTextArea('wys2');
		document.poste.submit();
	}
}
//function for submit on edit post screen
function goEditSubmit() {
	if ((document.poste.title.value == '') || (document.poste.title.value.length > 50)) {
		alert('Please Enter A Title of Between 1 and 50 Characters In Length');
		document.poste.title.focus();
	} else {
		document.poste.action = 'admin.php?p=post4';
		WYSIWYG.updateTextArea('wys2');
		document.poste.submit();
	}
}
//function to avoid blank newname on rename
function checkRename() {
	var Reg = new RegExp('^[a-zA-Z ]+$');
	if ((document.renamepic.newname.value == '') || (!Reg.test(document.renamepic.newname.value)) || (document.renamepic.newname.value.length > 30)) {
		alert('Please Enter A New Name Cotaining Characters and Spaces Only of No More Than 30 Characters In Length');
		document.renamepic.newname.focus();
	} else {
		document.renamepic.submit();
	}
}
//function to avoid no titles on post preview / attach page
function checkImages() {
	var Reg = new RegExp('^[a-zA-Z0-9 ]+$');
	if ((document.post2submit.pic1.value != '') && ((document.post2submit.title1.value == '') || (!Reg.test(document.post2submit.title1.value)) || (document.post2submit.title1.value.length > 30))) {
		alert('Please Enter A Title For Image 1, Containing Letters And Spaces Only, With A Max Of 30 Characters');
		document.post2submit.title1.focus();
	} else if ((document.post2submit.pic2.value != '') && ((document.post2submit.title2.value == '') || (!Reg.test(document.post2submit.title2.value)) || (document.post2submit.title2.value.length > 30))) {
		alert('Please Enter A Title For Image 2, Containing Letters And Spaces Only, With A Max Of 30 Characters');
		document.post2submit.title2.focus();
	} else if ((document.post2submit.pic3.value != '') && ((document.post2submit.title3.value == '') || (!Reg.test(document.post2submit.title3.value)) || (document.post2submit.title3.value.length > 30))) {
		alert('Please Enter A Title For Image 3, Containing Letters And Spaces Only, With A Max Of 30 Characters');
		document.post2submit.title3.focus();
	} else if ((document.post2submit.pic4.value != '') && ((document.post2submit.title4.value == '') || (!Reg.test(document.post2submit.title4.value)) || (document.post2submit.title4.value.length > 30))) {
		alert('Please Enter A Title For Image 4, Containing Letters And Spaces Only, With A Max Of 30 Characters');
		document.post2submit.title4.focus();
	} else if ((document.post2submit.pic5.value != '') && ((document.post2submit.title5.value == '') || (!Reg.test(document.post2submit.title5.value)) || (document.post2submit.title5.value.length > 30))) {
		alert('Please Enter A Title For Image 5, Containing Letters And Spaces Only, With A Max Of 30 Characters');
		document.post2submit.title1.focus();
	} else if ((document.post2submit.pic1.value != '') && (document.post2submit.pic1.value.indexOf('.jpg') <= 0) && (document.post2submit.pic1.value.indexOf('.JPG') <= 0)) {
		alert('Images Must Be In Jpg Format');
		document.post2submit.pic1.focus();
	} else if ((document.post2submit.pic2.value != '') && (document.post2submit.pic2.value.indexOf('.jpg') <= 0) && (document.post2submit.pic2.value.indexOf('.JPG') <= 0)) {
		alert('Images Must Be In Jpg Format');
		document.post2submit.pic2.focus();
	} else if ((document.post2submit.pic3.value != '') && (document.post2submit.pic3.value.indexOf('.jpg') <= 0) && (document.post2submit.pic3.value.indexOf('.JPG') <= 0)) {
		alert('Images Must Be In Jpg Format');
		document.post2submit.pic3.focus();
	} else if ((document.post2submit.pic4.value != '') && (document.post2submit.pic4.value.indexOf('.jpg') <= 0) && (document.post2submit.pic4.value.indexOf('.JPG') <= 0)) {
		alert('Images Must Be In Jpg Format');
		document.post2submit.pic4.focus();
	} else if ((document.post2submit.pic5.value != '') && (document.post2submit.pic5.value.indexOf('.jpg') <= 0) && (document.post2submit.pic5.value.indexOf('.JPG') <= 0)) {
		alert('Images Must Be In Jpg Format');
		document.post2submit.pic5.focus();
	} else {
		document.post2submit.submit();
	}
}

//function to submit updated welcome text
function checkWelcomeText() {
	WYSIWYG.updateTextArea('wys');
	document.welcomeedit.submit();
}