function resetCategories(){
	document.getElementById("subsubcategory_id_container").innerHTML = "";
}

function trim(str){
	return( (""+str).replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') );
}

function flag(pageID, type){
	var msg = type;
	if(type == 'adult'){
		msg = 'adult content';
	}
	if(confirm("Are you sure you want to flag this " + PAGE_NAME + " as " + msg + "?")){
		alert("Thank you, a site moderator will review this " + PAGE_NAME + " shortly.");
		loadURL("/page/flag/" + pageID + "?type=" + type);
	}
}

function tagIt(e,text){
	showToolTip(e,text);
	document.getElementById("tags").value = document.getElementById("hiddenTags").value
	document.getElementById("tags").focus();
}

function convertToWiki(pageID){
	if(confirm("Are you sure you want to convert this " + PAGE_NAME + " to a wiki-style Community  " + PAGE_NAME_CAPS + "?\n\nThis change can not be undone.")){
		var url = "/page/convert/?page_id=" + pageID + "&type=wiki";
		location.href = url;
	}
}

function addFavoritePage(pageID, userID){
	if(userID == null){
		alert("You must be logged in to add this " + PAGE_NAME + " to your favorites.  Please log in and try again!");
	}else{
		loadURL("/user/addFavoritePage/" + pageID);
		alert(PAGE_NAME_CAPS + " successfully added to your favorites!");
	}
}

function removeFavoritePage(pageID, userID){
	if(userID == null){
		alert("You must be logged in to remove this " + PAGE_NAME + " from your favorites.  Please log in and try again!");
	}else{
		if(confirm("Are you sure you want to remove this " + PAGE_NAME + " from your favorites?")){
			loadURL("/user/removeFavoritePage/" + pageID);
			dijit.byId('FavoritePages').refresh();
		}
	}
}

function removeSubscription(pageID, userID){
	if(userID == null){
		alert("You must be logged in to remove this " + PAGE_NAME + " from your favorites.  Please log in and try again!");
	}else{
		if(confirm("Are you sure you want to unsubscribe from this " + PAGE_NAME + "?")){
			loadURL("/user/removeSubscription/" + pageID);
			dijit.byId('Subscriptions').refresh();
		}
	}
}

function saveTags(pageID){
	var tags = document.getElementById("tags").value;
	//check if the tags have changed... if so, save them
	if(tags != document.getElementById("hiddenTags").value){
		tags = trim(tags);
		tags = tags.replace("'","");
		tags = tags.replace("\"","");
		tags = tags.replace(";","");
		tags = tags.replace("&","");
		tags = tags.replace("+","");
		if(tags.length > 0){
			loadURL("/page/tagIt?page_id=" + pageID + "&tags=" + tags);
		}
		document.getElementById("hiddenTags").value = tags;
	}
	document.getElementById("bubble_tooltip_content").innerHTML = "<center>Your tags have been saved... Cheers!</center>";
	setTimeout("hideTags()", 2000);
}

function hideTags(){
	hideToolTip();
}

function vote(rating, pageID){
	loadURL("/page/vote?type=page&page_id=" + pageID + "&rating=" + rating);
	alert("Thanks for your vote!");
}

function addMsg(){
	var area = document.getElementById("message");
	if(area.style.display == "none"){
 		area.style.display = "block";
	}else{
		area.style.display = "none";
	}
}

function validatePage(){
	var category = document.getElementById("page_category_reference_category_id").value;
	if(category == ''){
		alert("ERROR: You Must Select a Category!");
		return false;
	}else{
	    changed = false; //this field is used by the onchange handler of tiny_mce but since the user hit the save button, we can ignore changes as they are being saved
		document.getElementById("createBtn").disabled = true;
		return true;
	}
}

function validateEmail(email) {
   var regEx = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(regEx.test(email) == false) {
      alert("Please enter a valid email address!");
      return false;
   }
}

function validateSignUp() {
   var email = document.getElementById('email').value;
   var eula_agreed = document.getElementById('user_eula').checked;
   var regEx = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(regEx.test(email) == false) {
      alert("Please enter a valid email address!");
      return false;
   }
   if(!eula_agreed){
      alert("In order to publish your " + PAGE_NAME + ", you must agree to the Terms of Use!");
      return false;
   }
   return true;
}

var oldHTML;
function resetEmailDIV(){
	document.getElementById("EMAILDIV").innerHTML = oldHTML;
	if(document.getElementById("email_to").value = ""){
		document.getElementById("email_to").style.color = "#c0c0c0";
		document.getElementById("email_to").value = "Type Email Address Here";
	}
	if(document.getElementById("name"))
		document.getElementById("name").value = "";
		
	document.getElementById("msg").value = "";
}

function changeEmail(){
	var everyone = document.getElementById("emailall").value;
	var area = document.getElementById("emailaddress");
	if(area.style.display == "none"){
 		area.style.display = "block";
	}else{
		area.style.display = "none";
	}
}

function focusEmail(){
	if(document.getElementById("email_to").value == "Type Email Address Here"){
	document.getElementById("email_to").value = "";
	document.getElementById("email_to").style.color = "#000000";
	}
}

function resetEmail(){
	var value = document.getElementById("email_to").value;
	if(value.length < 1){
		document.getElementById("email_to").style.color = "#c0c0c0";
		document.getElementById("email_to").value = "Type Email Address Here";
	}
}

function emailPage(title, username, page_id){
	var contacts = false;
	try{
		if(document.getElementById("emailall").checked)
			contacts = true;		
	}catch(err){
		contacts = false;
	}
	if(contacts){
		if(confirm("Are you sure you want to email this " + PAGE_NAME + " to all your contacts?")){
			var msg = document.getElementById("msg").value;
			var url = "/page/email_contacts/?page_id=" + page_id + "&msg=" + msg;
			location.href = url;
		}
	}else{
		var email = document.getElementById("email_to").value;
		var name = username;
		if(username == null)
			name = document.getElementById("name").value;
		var message = document.getElementById("msg").value;
		name = encodeURIComponent(name);
		message = encodeURIComponent(message);
		var body;
		if(name.length > 0){
			body = name + " sent you " + APP_ARTICLE + " " + APP_NAME + "  " + PAGE_NAME_CAPS + " ('" + title + "'):";
		}else{
			body = "You've received " + APP_ARTICLE + " " + APP_NAME + "  " + PAGE_NAME_CAPS + " ('" + title + "'):";
		}
		var emails = email.split(",");
		var emailsOK = true;
		var regEx = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		for(var i=0; i<emails.length; i++){
			if(regEx.test(trim(emails[i])) == false) {
				alert(trim(emails[i]) + " is not a valid email address.  Please enter a valid email address and try again.  If you have multiple recipients, please separate each email address with a comma.");
				emailsOK = false;
			}		
		}
		if(emailsOK){
			title = "You've received " + APP_ARTICLE + " " + APP_NAME + " " + PAGE_NAME + " - '" + title + "'";
			loadURL("/page/email?page_action=email_page&url=" + encodeURI(location.href) + "&sub=" + title + "&to=" + email + "&message=" + message + "&body=" + body);
			oldHTML = document.getElementById("EMAILDIV").innerHTML;
			document.getElementById("EMAILDIV").innerHTML = "<BR/><center><font class='SIDEBARTEXT'>You've successfully emailed this " + PAGE_NAME + ".</font><BR/><BR/><font onclick=\"resetEmailDIV()\" class=\"LINK\">Email This " + PAGE_NAME_CAPS + " Again</font><BR/><BR/></center>";
		}
	}
}

function subscribe(title, id){
	var email = document.getElementById("email").value;
	var body = "You've successfully subscribed to ";
	if(email.length < 3){
		alert("Please enter a valid email address!");
	}else{
		loadURL("/page/email?id=" + id + "&page_action=subscribe&sub=" + APP_NAME + " - You've Successfully Subscribed to '" + title + "'&to=" + email + "&body=" + body);
		document.getElementById("subscribeDIV").innerHTML = "<BR/><center><font class='SIDEBARTEXT'>You've been successfully subscribed<BR/><BR/>Thanks!</font></center><BR/><BR/>";
	}
}
