function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

/*---------------------------------------------

		REPLACING THE HEADLINES

---------------------------------------------*/

function replaceHeadline(){
	if(!document.getElementById){ return; }

	//check if it's an article page or an archive page
	if(document.getElementById('article')){
		var insertInto = document.getElementById('article');
	}else{
		var insertInto = document.getElementById('archive');
	}
	var headline = document.getElementById('title'); 

	var titleImage = document.createElement('img');
	titleImage.setAttribute('src', "title.gif");
	titleImage.setAttribute('id', "title");
	titleImage.setAttribute('alt', headline.innerHTML);
	titleImage.setAttribute('title', headline.innerHTML);

	//styles
	//titleImage.setAttribute('style', 'border-bottom: 1px solid black; border-top: none;');
	titleImage.setAttribute('style', 'border-bottom: 1px solid black;');
	titleImage.id = "title"; //stupid IE
	titleImage.style.borderBottom = '1px solid black'; //stupid IE
	

	//insertInto.insertBefore(titleImage, headline);	
	
	//removing the actual HTML text
	document.body.replaceChild(titleImage, headline);
	attachNewKeyups();
}

/*---------------------------------------------

		LIVE PREVIEW FOR COMMENTING

---------------------------------------------*/
function attachNewKeyups(){
	if(!document.getElementById){return;};
	if(document.getElementById('author')){ var author = document.getElementById('author'); }
	if(document.getElementById('url')){ var url = document.getElementById('url'); }
	
	author.onkeyup = url.onkeyup = function(){
		commentPreview();
	}
	
}

function commentPreview(){
	if(!document.getElementById){ return;}

	if(document.getElementById('author')){ var author = document.getElementById('author'); }
	if(document.getElementById('preview')){ var preview = document.getElementById('preview'); }
	if(document.getElementById('text')){ var text = document.getElementById('text'); }
	if(document.getElementById('url')){ var url = document.getElementById('url'); }
	
	if(author.value == ""){
		preview.innerHTML = '<p class="commenter">You\'re saying:</p><p>' + text.value.replace(/(\n|\r)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'/p><p>')+'<'+'/p>' + '</p><p class="time">Posting now</p>';
	}else{
		if(url.value != ""){
			if(url.value.indexOf("http://") != -1){
				preview.innerHTML = '<p class="commenter"><a href="' + url.value + '" title="' + author.value + '">' + author.value + '</a></p><p>' + text.value.replace(/(\n|\r)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'/p><p>')+'<'+'/p>' + '</p><p class="time">Posting now</p>';
			}else{
				preview.innerHTML = '<p class="commenter"><a href="http://' + url.value + '" title="' + author.value + '">' + author.value + '</a></p><p>' + text.value.replace(/(\n|\r)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'/p><p>')+'<'+'/p>' + '</p><p class="time">Posting now</p>';
			}
		}else{
			preview.innerHTML = '<p class="commenter">' + author.value + '</p><p>' + text.value.replace(/(\n|\r)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'/p><p>')+'<'+'/p>' + '</p><p class="time">Posting now</p>';
		}
	}
}

/*---------------------------------------------

		ERROR CHECKING

---------------------------------------------*/

function checkForm(){
	var requiredFields, i, status, emailMessage;
	status = "none empty";
	if(document.getElementById('required')){
		requiredFields = document.getElementById('required').value.split(','); //creates an array based on the required fields specified in the "hidden" input value
	}
	
	var itemCount = 0;
	var message = "Please fill in the";
	var emailTest = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
	
	//highlighting empty fields
	for(i=0; i<requiredFields.length; i++){
		if(document.getElementById(requiredFields[i]).value == ''){
			status = "something empty";
			if(itemCount == 0){
				message = message + " " + requiredFields[i];
				itemCount++;
			}else{
				message = message + " and " + requiredFields[i];
			}
			highlightField(requiredFields[i]);
		}
	}
	
	//setting focus
	if(itemCount > 0){
		document.getElementById('author').focus();
	}
	
	//unhighlighting
	for(i=0; i<requiredFields.length; i++){
		if(document.getElementById(requiredFields[i]).value != '' && document.getElementById(requiredFields[i]).className == 'required_field'){
			unhighlight(requiredFields[i]);
		}
	}
	
	//conditions specifically for the email field
	if(emailTest.test(document.getElementById('email').value)){
		emailStatus = "valid";
	}else if(!emailTest.test(document.getElementById('email').value) && document.getElementById('email').value != ""){
		emailStatus = "invalid";
	}else if(document.getElementById('email').value == ""){
		emailStatus = "blank";
	}
	
	//alert messages conditions
	if(status == "none empty"){
		if(emailStatus == "invalid"){
			alert("Please enter a valid email address.");
			highlightField('email');
			return false;
		}else if(emailStatus == "valid"){
			return true;
		}
	}else if(status == "something empty"){
		if(emailStatus == "invalid"){
			alert(message + ". Also, please enter a valid email address.");
			highlightField('email');
		}else if(emailStatus == "valid" || emailStatus == "blank"){
			alert(message + ".");
		}
		return false;
	}
}

function highlightField(fieldID){
	document.getElementById(fieldID).className = "required_field";
	document.getElementById(fieldID).setAttribute('style', 'border: 1px solid #f60;');
	document.getElementById(fieldID).focus();
}

function unhighlight(fieldID){
	document.getElementById(fieldID).className = "";
	document.getElementById(fieldID).removeAttribute('style');
	document.getElementById(fieldID).setAttribute('style', '1px solid #acacac');
}

/*---------------------------------------------

		LOADING

---------------------------------------------*/

function init(){
	//findImage();
	replaceHeadline();
	commentPreview();
}

addLoadEvent(init);