/*
* Comment Javascript Functions
* Copyright 2009 Kris Pizer
*/

function get(url, div, request) {
	var maindiv = document.getElementById(div);	
	jQuery.ajax({
			type: request,
			url: url,
			cache: false,
			success: function(output){
				maindiv.innerHTML = output;
			}
	});
}

function comments(id) {
		var myEle = document.getElementById(id+'_comments');
		if (myEle.style.display == 'none' ) {	
				myEle.style.display = 'block';
				get("comment.php?id="+id, id+"_comments", "GET");				
		}
		else {
			myEle.style.display = 'none';
		}		
}

function add_comment(id) {	
	showDialog("CommentPop","700","535","","comment_form");
	var form = document.forms['comment_form'];  
	var el = document.createElement("input");
	el.type = "hidden";
	el.name = "id";
	el.value = id;
	form.appendChild(el);
}


function validate_form() {
	var form = document.forms['comment_form']; 
	newsid = form.id;
	name = form.username;
	email = form.email;
	message = form.message;	
	error_box = document.getElementById('error');
	error_message = document.getElementById('error_message');
	
	if(name.value == "" || email.value == "" || message.value == "") {
		error_box.style.display = "block";
		error_message.innerHTML = "You have left a field blank please correct this problem. <a href='javascript:close_error();'>[Close]</a>";
	}
	else if(!isValidEmail(email.value)) {
		error_box.style.display = "block";
		error_message.innerHTML = "The email address you entered is not valid. <a href='javascript:close_error();'>[Close]</a>";
	}
	else {		
		submit_comment(true);
	}
}

function close_error() {
	 document.getElementById('error').style.display = "none";	
}

function submit_comment(submit_form) {	
	var form = document.forms['comment_form']; 
	newsid = form.id.value;
	name = form.username.value;
	email = form.email.value;
	message = form.message.value;	
	
	if(submit_form) {
		jQuery.ajax({
			type: "GET",
			url: "new_comment.php?newsid="+newsid+"&username="+name+"&email="+email+"&message="+message,
			cache: false,
			success: function(output){
				get("comment.php?id="+newsid, newsid+"_comments", "GET");	
				$('#CommentPop').dialog("close");
			}
		});	
		
	}	
}

function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;    
   if (strEmail.search(validRegExp) == -1) 
   {      
      return false;
   } 
   return true; 
}


function showDialog(diaID, diaWidth, diaHeight, btntype, diaForm)
{
	diaWidth = diaWidth - 0;
	diaHeight = diaHeight - 0;
	// Create the dialog
	if(btntype == "none")
	{
		$('#'+diaID).dialog({
			autoOpen: false,
			width: diaWidth,
			height: diaHeight,
			resizable: false,
			modal: true
		});
	}
	else
	{
		$('#'+diaID).dialog({
			autoOpen: false,
			width: diaWidth,
			height: diaHeight,
			resizable: false,
			buttons: {
				"Okay": function() { 
					try
					{
						document.getElementById(diaForm).submit();
					}
					catch(err)
					{ }
					//$(this).dialog("close");
				}, 
				"Cancel": function() { 
					$(this).dialog("close");
				} 
			},
			modal: true
		});
	}
	
	// Now show it
	$('#'+diaID).dialog('open');
	return false;
}
