var FavWidget = Class.create();
FavWidget.prototype = {
	STATES : { READY : 0, SAVING : 1, SAVED : 2 },
	setOptions : function(options) { 
		this.options = {
			widget:'fav'
		};
		Object.extend(this.options, options || {});
	},
	initialize : function(PID, TBL, options) {
		this.PID = PID;
		this.TBL = TBL;
		this.setOptions(options);
		this.favicon = $A( $(this.options.widget).getElementsByClassName('favicon') ).first();
		this.favLink = $A( $(this.options.widget).getElementsByClassName('favLink') ).first();
		this.favSavedView = $A( $(this.options.widget).getElementsByClassName('favSavedView') ).first();
		Event.observe(this.favicon, 'click', this.handleFav.bind(this));
	},
	handleFav : function() {
		if (currentUser.id == 0) {
			alert("Please login to save favourites!");
			return;
		}
		this.state = this.STATES.READY;
		this.paint();
		var params = "TBL="+this.TBL+"&PID="+this.PID;
		new Ajax.Request('/include/actions/add_favourite.asp', {parameters:params, asynchronous:true, onComplete:this.processAdd.bind(this)});
	},
	processAdd : function(response) {
		this.state = this.STATES.SAVED;
		this.favSavedView.update(response.responseText);
		this.paint();
	},
	paint : function() {
		switch(this.state) {
			case this.STATES.READY:
				Element.hide(this.favSavedView);
				Element.show(this.favLink);
				break;
			case this.STATES.SAVED:
				Element.hide(this.favLink);
				Element.show(this.favSavedView);
				break;
		}
	}
};

var FlagWidget = Class.create();
FlagWidget.prototype = {
	STATES : { READY : 0, SAVING : 1, SAVED : 2 },
	setOptions : function(options) { 
		this.options = {
			widget:'flag'
		};
		Object.extend(this.options, options || {});
	},
	initialize : function(PID, TBL, options) {
		this.PID = PID;
		this.TBL = TBL;
		this.setOptions(options);
		this.flagicon = $A( $(this.options.widget).getElementsByClassName('flagicon') ).first();
		this.flagLink = $A( $(this.options.widget).getElementsByClassName('flagLink') ).first();
		this.flagSavedView = $A( $(this.options.widget).getElementsByClassName('flagSavedView') ).first();
		Event.observe(this.flagicon, 'click', this.handleFlag.bind(this));
	},
	handleFlag : function() {
		if (currentUser.id == 0) {
			alert("Please login to flag as inappropriate!");
			return;
		}
		this.state = this.STATES.READY;
		this.paint();
		var params = "TBL="+this.TBL+"&PID="+this.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 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;
		}
	}
};

        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)
{

	//If PID = -1, user has requested to create new profile
		
	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);

		if (PID == -1) {
			//Element.hide(lsd);	
			lsd.innerHTML = "A new profile will be created for <b>" + str + "</b>.";
		} else {
			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;
		}

 
											
