var FriendsLink = Class.create();
FriendsLink.prototype = {
	STATES : { READY : 0, SAVING : 1, SAVED : 2 },
	initialize : function(FriendsLink, FriendsSavingView, FriendsSavedView) {
		this.state = this.STATES.READY;
		this.FriendsLink = FriendsLink;
		this.FriendsSavingView = FriendsSavingView;
		this.FriendsSavedView = FriendsSavedView;
	},
	handleAdd : function(MDR,PID) {
		if (currentUser.id == '') {
			alert("Please login to save Friends."); 					
		}
		this.state = this.STATES.SAVING;
		this.paint();
		var params = "MDR="+MDR+"&PID="+PID;
		new Ajax.Request('/include/actions/add_friend.asp', {parameters:params, asynchronous:true, onComplete:this.processAdd.bind(this)});
	},
	processAdd : function(response) {
		this.state = this.STATES.SAVED;
		$(this.FriendsSavedView).update(response.responseText);
		this.paint();
	},
	paint : function() {
		switch(this.state) {
			case this.STATES.READY:
				Element.hide($(this.FriendsSavingView));
				Element.hide($(this.FriendsSavedView));
				Element.show($(this.FriendsLink));
				break;
			case this.STATES.SAVING:
				Element.hide($(this.FriendsLink));
				Element.hide($(this.FriendsSavedView));
				Element.show($(this.FriendsSavingView));
				break;
			case this.STATES.SAVED:
				Element.hide($(this.FriendsLink));
				Element.hide($(this.FriendsSavingView));
				Element.show($(this.FriendsSavedView));
				break;
		}
	}
};

var FavoritesLink = Class.create();
FavoritesLink.prototype = {
	STATES : { READY : 0, SAVING : 1, SAVED : 2 },
	initialize : function(favoritesLink, favoritesSavingView, favoritesSavedView) {
		this.state = this.STATES.READY;
		this.favoritesLink = favoritesLink;
		this.favoritesSavingView = favoritesSavingView;
		this.favoritesSavedView = favoritesSavedView;
	},
	handleAdd : function(TBL,PID) {
		if (currentUser.id == '') {
			alert("Please login to save favorites."); 					
		}
		this.state = this.STATES.SAVING;
		this.paint();
		var params = "TBL="+TBL+"&PID="+PID;
		new Ajax.Request('/include/actions/add_favourite.asp', {parameters:params, asynchronous:true, onComplete:this.processAdd.bind(this)});
		//if (TBL == 1){
		//	var params = "CT=12&TBL="+TBL+"&PID="+ID;
		//	new Ajax.Request('/include/actions/add_content_topic.asp', {parameters:params, asynchronous:true, onComplete:this.processAdd.bind(this)});
		//	}
		//else
		//if (TBL == 2){
		//	var params = "CT2=12&TBL="+TBL+"&PID="+ID;
		//	new Ajax.Request('/include/actions/add_content_relationship.asp', {parameters:params, asynchronous:true, onComplete:this.processAdd.bind(this)});
		//	}
		//else{
		//}
	},
	processAdd : function(response) {
		this.state = this.STATES.SAVED;
		$(this.favoritesSavedView).update(response.responseText);
		this.paint();
	},
	paint : function() {
		switch(this.state) {
			case this.STATES.READY:
				Element.hide($(this.favoritesSavingView));
				Element.hide($(this.favoritesSavedView));
				Element.show($(this.favoritesLink));
				break;
			case this.STATES.SAVING:
				Element.hide($(this.favoritesLink));
				Element.hide($(this.favoritesSavedView));
				Element.show($(this.favoritesSavingView));
				break;
			case this.STATES.SAVED:
				Element.hide($(this.favoritesLink));
				Element.hide($(this.favoritesSavingView));
				Element.show($(this.favoritesSavedView));
				break;
		}
	}
};

var FlagLink = Class.create();
FlagLink.prototype = {
	STATES : { READY : 0, SAVING : 1, SAVED : 2 },
	initialize : function(flagLink, flagSavedView) {
		this.state = this.STATES.READY;
		this.flagLink = flagLink;
		this.flagSavedView = flagSavedView;
	},
	handleAdd : function(TBL,PID) {
		if (currentUser.id == '') {
			alert("Please login to save flag."); 					
		}
		this.state = this.STATES.READY;
		this.paint();
		var params = "TBL="+TBL+"&PID="+PID;
		new Ajax.Request('/include/actions/flag_entry.asp', {parameters:params, asynchronous:true, onComplete:this.processAdd.bind(this)});
	},
	processAdd : function(response) {
		this.state = this.STATES.SAVED;
		$(this.flagSavedView).update(response.responseText);
		this.paint();
	},
	paint : function() {
		switch(this.state) {
			case this.STATES.READY:
				Element.hide($(this.flagSavedView));
				Element.show($(this.flagLink));
				break;
			case this.STATES.SAVED:
				Element.hide($(this.flagLink));
				Element.show($(this.flagSavedView));
				break;
		}
	}
};


//var processAdd = Class.create();
//processAdd.prototype = {
	//initialize : function(TBL) {
		//this.TBL = TBL;
	//}
//};


var ModerateLink = Class.create();
ModerateLink.prototype = {
	STATES : { READY : 0, APPROVED : 1, REMOVED : 2, POSTPONED : 3 },
	initialize : function(ModerateLink, ModerateApproveView, ModerateRemoveView, ModeratePostponeView, ModerateRefreshView) {
		this.state = this.STATES.READY;
		this.ModerateLink = ModerateLink;
		this.ModerateApproveView = ModerateApproveView;
		this.ModerateRemoveView = ModerateRemoveView;
		this.ModeratePostponeView = ModeratePostponeView;
		this.ModerateRefreshView = ModerateRefreshView;
	},
	handleAdd : function(MDR,TBL,PID) {
		if (currentUser.id == '') {
			alert("Please login to moderate."); 					
		}
		this.state = this.STATES.READY;
		this.paint();		
		var params = "MDR="+MDR+"&TBL="+TBL+"&PID="+PID;
		new Ajax.Request('/include/actions/add_revision_action.asp', {parameters:params, asynchronous:true, onComplete:this.processAdd(MDR)});			},
	processAdd : function(MDR) {

		if (MDR == 1){
			this.state = this.STATES.APPROVED;
		}
		if (MDR == 2){
			this.state = this.STATES.REMOVED;
		}
		if (MDR == 3){
			this.state = this.STATES.POSTPONED;
		}

		//this.state = this.STATES.POSTPONED;
		//$(this.favoritesApproveView).update(response.responseText);
		this.paint();
	},
	paint : function() {
		switch(this.state) {
			case this.STATES.READY:
				Element.hide($(this.ModerateApproveView));
				Element.hide($(this.ModerateRemoveView));
				Element.hide($(this.ModeratePostponeView));
				Element.hide($(this.ModerateRefreshView));
				Element.show($(this.ModerateLink));
				break;
			case this.STATES.APPROVED:
				Element.hide($(this.ModerateLink));
				Element.hide($(this.ModerateRemoveView));
				Element.hide($(this.ModeratePostponeView));
				Element.show($(this.ModerateApproveView));
				Element.show($(this.ModerateRefreshView));
				break;
			case this.STATES.REMOVED:
				Element.hide($(this.ModerateLink));
				Element.hide($(this.ModerateApproveView));
				Element.hide($(this.ModeratePostponeView));
				Element.show($(this.ModerateRemoveView));
				Element.show($(this.ModerateRefreshView));
				break;
			case this.STATES.POSTPONED:
				Element.hide($(this.ModerateLink));
				Element.hide($(this.ModerateApproveView));
				Element.hide($(this.ModerateRemoveView));
				Element.show($(this.ModeratePostponeView));
				Element.show($(this.ModerateRefreshView));
				break;
		}
	}
};

                	var addView;
			function addView(TBL,PID,TYP){                        
				//this.TBL = TBL;
				//this.PID = PID;
				var params = "TBL="+TBL+"&PID="+PID+"&type="+TYP;
			 	new Ajax.Request('/include/actions/add_view.asp', {parameters:params});	
				return true;
                    	} 

        function addRow(tblData,txtRow,rownumber,fieldlength)
        {
          //var tbl = document.getElementById("tblData");
		  var tbl = document.getElementById(tblData);
          var lastRow = tbl.rows.length;
          // if there's no header row in the table, then iteration = lastRow + 1
          var iteration = lastRow;
          var row = tbl.insertRow(lastRow);
          
          // left cell
          var cellLeft = row.insertCell(0);
          var textNode = document.createTextNode(iteration);
          //cellLeft.appendChild(textNode);
          
          // right cell
          var cellRight = row.insertCell(1);
          var el = document.createElement("input");
          el.type = "text";
          el.className += "comments";
          //el.name = "txtRow" + iteration;
          //el.id = "txtRow" + iteration;
          el.name = txtRow + iteration;
          el.id = txtRow + iteration;
          el.size = fieldlength;   
	  cellRight.className += "padb4";                
          cellRight.appendChild(el);
          
          //var rownumber = document.getElementById("rownumber");
		  var rownumber = document.getElementById(rownumber);
		  rownumber.value ++;
        }

        function removeRowFromTable(tblData,txtRow,rownumber)
        {
          //var tbl = document.getElementById("tblData");
		  var tbl = document.getElementById(tblData);
          var lastRow = tbl.rows.length;
          if (lastRow > 2) tbl.deleteRow(lastRow - 1);

          //var rownumber = document.getElementById("rownumber");
          var rownumber = document.getElementById(rownumber);
		  if (lastRow > 2) rownumber.value --;

        }

var showResult;

//LIVE SEARCH
//http://www.w3schools.com/php/php_ajax_livesearch.asp
	
var xmlhttp;

function showResult(str,field_counter,TBL,TBT,PID)
{
livesearch_div = "livesearch" + field_counter
Element.show(livesearch_div);	
if (str.length==0)
  {
  document.getElementById(livesearch_div).innerHTML="";
  document.getElementById(livesearch_div).style.border="0px";
  return;
  }
xmlhttp=GetXmlHttpObject()
if (xmlhttp==null)
  {
  alert ("Your browser does not support XML HTTP Request");
  return;
  }
var url="/include/process/process_live_search.asp";
url=url+"?q="+str;
url=url+"&field_counter="+field_counter;
url=url+"&TBL="+TBL;
url=url+"&TBT="+TBT;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged ;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
  {
  document.getElementById(livesearch_div).innerHTML=xmlhttp.responseText;
  //document.getElementById(livesearch_div).style.border="1px solid #A5ACB2";
  }
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

var postResult;
function postResult(str,field_counter,TBL,TBT,PID)
{
	livesearch_div = "livesearch" + field_counter;
	livetext_div = "livetext" + field_counter;
	liveid_div = "liveid" + field_counter;
	var ltd = document.getElementById(livetext_div);
	ltd.value = str;
	var lid = document.getElementById(liveid_div);
	lid.value = PID;
	var lsd = document.getElementById(livesearch_div);
	Element.hide(lsd);		
	//alert ("You have selected " + str);

}
		var Trim;
		function Trim(str)
		{
			while(str.charAt(0) == (" ") )
			{ 
			   str = str.substring(1);
			}	
		  	while(str.charAt(str.length-1) == " " )
  		 	{ 
	   		   str = str.substring(0,str.length-1);
	 		}		  
		  	return str;
		}