function getNewsArticle(NID) {
	var rand = Math.floor(Math.random()*111101);
   $.ajax({
      url: "/news/getNews.php?rand=" + rand + "&articleID=" + NID,
      type: 'GET',
      error: function (xhr, status) {
         alert("Error!  Message: " + status);
      },
      success: function (data) {
         // $('#pageText').html(data);
         $("iframe").contents().find("#pageText").html(data);
         $('iframe', top.document).scrollTop();

      }
   });
}


function getDiaryEntry(NID) {
	var rand = Math.floor(Math.random()*111101);
   $.ajax({
      url: "/diary/getEvents.php?rand=" + rand + "&eventID=" + NID,
      type: 'GET',
      error: function (xhr, status) {
         alert("Error!  Message: " + status);
      },
      success: function (data) {
         // $('#pageText').html(data);
         $("iframe").contents().find("#pageText").html(data);
         $('iframe', top.document).scrollTop();

      }
   });
}


function getNewsCat(CID) {
   $.ajax({
      url: "/news/getNewsCat.php?catID=" + CID,
      type: 'GET',
      error: function (xhr, status) {
         alert("Error!  Message: " + status);
      },
      success: function (data) {
         //$('#pageText').html(data);
         $("iframe").contents().find("#pageText").html(data);
         $("iframe").scrollTop(600);
      }
   });
   $.ajax({
      url: "/news/getNews.php?catID=" + CID,
      type: 'GET',
      error: function (xhr, status) {
         alert("Error!  Message: " + status);
      },
      success: function (data) {
         //$('#pageText').html(data);
         $("iframe").contents().find("#pageText").html(data);
         $("iframe").scrollTop(600);
      }
   });
}

function checkNewArticle() {
   // Make quick references to our fields
	var shortTitle = document.getElementById('shortTitle');
	var longTitle = document.getElementById('longTitle');
	// var articleText = document.getElementById('articleText__Frame');
	
	// Check each input in the order that it appears in the form!
	if(notEmpty(shortTitle, "Please enter a short title")){
		if(notEmpty(longTitle, "Please enter a long title")){
			// if(notEmpty(articleText, "Please enter the actual article")){
				return true;
			// }
		}
	}
	return false;
}

function checkEmailArticleForm() {
	var name = document.getElementById('name');
	var email = document.getElementById('email');
	var sender = document.getElementById('sender');
	var senderemail = document.getElementById('senderemail');
		
	// Check each input in the order that it appears in the form!
	if(notEmpty(name, "Please enter your friends name")){
		if(emailValidator(email, "Please enter a valid email")){
			if(notEmpty(sender, "Please enter your name")){
				if(emailValidator(senderemail, "Please enter a valid email address")){
					return true;
				}
			}
		}
	}
	return false;
}

function formValidator(){
	// Make quick references to our fields
	var firstname = document.getElementById('firstname');
	var addr = document.getElementById('addr');
	var zip = document.getElementById('zip');
	var state = document.getElementById('state');
	var username = document.getElementById('username');
	var email = document.getElementById('email');
	
	// Check each input in the order that it appears in the form!
	if(isAlphabet(firstname, "Please enter only letters for your name")){
		if(isAlphanumeric(addr, "Numbers and Letters Only for Address")){
			if(isNumeric(zip, "Please enter a valid zip code")){
				if(madeSelection(state, "Please Choose a State")){
					if(lengthRestriction(username, 6, 8)){
						if(emailValidator(email, "Please enter a valid email address")){
							return true;
						}
					}
				}
			}
		}
	}
	
	
	return false;
	
}

function notEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	if (elem.value == '<br />'){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Please enter between " +min+ " and " +max+ " characters");
		elem.focus();
		return false;
	}
}

function madeSelection(elem, helperMsg){
	if(elem.value == "Please Choose"){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		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;
	}
}



function BrowseServer(field)
{
	// You can use the "CKFinder" class to render CKFinder in a page:
	var finder = new CKFinder() ;
	finder.BasePath = '/news/ckfinder/' ;	// The path for the installation of CKFinder (default = "/ckfinder/").
	finder.SelectFunction = SetFileField ;	
	finder.Popup() ;

	// It can also be done in a single line, calling the "static"
	// Popup( basePath, width, height, selectFunction ) function:
	// CKFinder.Popup( '../../', null, null, SetFileField ) ;
	//
	// The "Popup" function can also accept an object as the only argument.
	// CKFinder.Popup( { BasePath : '../../', SelectFunction : SetFileField } ) ;
}

// This is a sample function which is called when a file is selected in CKFinder.
function SetFileField( fileUrl )
{
	document.getElementById( "filelink" ).value = fileUrl ;
}

