/*= Sarven Capadisli, Oscar Garavito 2008 */

var DA = DOMAssistant;
var TDM = {
	Config: {
		Init: {
			Increment: 0,
			Direction: null,
			Postal_Code_Pattern: /^[a-zA-Z]{1}[0-9]{1}[a-zA-Z]{1}[0-9]{1}[a-zA-Z]{1}[0-9]{1}$/,
			Email_Pattern: /^\w+([\.\-]?\w+)*@\w+([\.\-]?\w+)*(\.\w{2,4})+$/,
			SliderCurrent: 0,
			SliderID: 0,
			SliderLength: 0,
			SliderTimeInterval: 4000,
			SliderDatas: [],
			SliderActions: [],
			OpacityStart: 20,
			CurrentTab: 0,
			CurrentView: {}
		},

		Selectors: {
			Opened: "opened",
			Closed: "closed",
			CloseText: "X",
			CloseTitle: "Close this window",
			Selected: "selected",
			Current: "current",
			Hover: "hover",
			DropDownMenu: "interaction_dd_menu",
			TypographyDecrease: "typography_decrease",
			TypographyIncrease: "typography_increase",
			RequiredField: "required_field",
			ValidateRequiredFields: "validate_required_fields",
			UserRatingSystem: "user_rating_system",
			ScriptBasedSubmit: "script_based_submit",
			ScriptBasedClose: "script_based_close",
			AverageUserRating: "average_user_rating",
			RatingSystem: "rating_system",
			OpenResourceOnNewWindow: "open_resource_on_new_window",
			Warning: "warning",
			Error: "error",
			ErrorMessage: "error_message",
			ErrorMessageGlobal: "Please fill out all the (<span class=\"required\">*</span>) required fields.",
			Validate: "validate",
			EmailAddress: "E-mail address",
			Previous: "prev",
			Next: "next"			
		},

		Messages: {	}
	},

	Utils: {
		Tabbing: function(datas, actions, init_view) {
			var is_init_set = false;
			for (var j=0; j<datas.length; j++) {
				if (j === init_view || "#"+datas[j].id === init_view) {
					TDM.Config.Init.CurrentTab = j;

					TDM.Utils.Open(DA.$(datas[j]));
					DA.$(actions[j]).addClass(TDM.Config.Selectors.Selected);
					if ("#"+datas[j].id === init_view) {
						location.hash = DA.$(actions[j]).parentNode.parentNode.id;
					}
					is_init_set = true;
				}
				else {
					TDM.Utils.Close(DA.$(datas[j]));
				}
			}

			if (!is_init_set) {
				TDM.Config.Init.CurrentTab = 0;
				TDM.Utils.ShowObject(datas, actions, TDM.Config.Init.CurrentTab);

			}

			for (var i=0; i<actions.length; i++) {
				DA.$(actions[i]).addEvent("click", function(i) {
					return function(e) {
		        		TDM.Utils.ShowObject(datas, actions, i);
		        		if ( DOMAssistant.preventDefault(e)) { DOMAssistant.preventDefault(e); }
		        	};
				}(i));
			}
		},

		ShowObject: function (datas, actions, change_id) {
			TDM.Utils.Close(datas[TDM.Config.Init.CurrentTab]);
			DA.$(actions[TDM.Config.Init.CurrentTab]).removeClass(TDM.Config.Selectors.Selected);
			TDM.Config.Init.CurrentTab = change_id;
			TDM.Utils.Open(datas[TDM.Config.Init.CurrentTab]);
			DA.$(actions[TDM.Config.Init.CurrentTab]).addClass(TDM.Config.Selectors.Selected);
		},

		ShowHide: function($datas, $actions, return_switch) {
			var counter = 0;
			$actions.each(function(){
				DA.$(this).addEvent("click", function(i) {
					return function (e) {
						if ($datas[i].hasClass(TDM.Config.Selectors.Opened)) {
							TDM.Utils.Close($datas[i]);
						}
						else {
							TDM.Utils.Open($datas[i]);
						}

						if (return_switch === false) {
		        			if ( DOMAssistant.preventDefault(e)) { DOMAssistant.preventDefault(e); }
						}
					};
				}(counter));
				counter++;
			});
		},

		Close: function (obj) {
			obj.removeClass(TDM.Config.Selectors.Opened);
			obj.addClass(TDM.Config.Selectors.Closed);
		},

		Open: function(obj) {
			obj.removeClass(TDM.Config.Selectors.Closed);
			obj.addClass(TDM.Config.Selectors.Opened);
		},

		OpenResourceOnNewWindow: function () {
			var new_window_elements = DA.$('.'+TDM.Config.Selectors.OpenResourceOnNewWindow);

			if (new_window_elements.length > 0) {
				for (var i=0; i < new_window_elements.length; i++) {
					var el = new_window_elements[i];

					DA.$(el).addEvent('click', function (_this) {
						return function (e){
							if (_this.href !== undefined) {
								window.open(_this.href);
								//_this.href = "#";
								return false;
							}
						};
					}(el));

					DA.$(el).addEvent('keypress', function (_this) {
						return function (e){
							if (_this.href !== undefined) {
								window.open(_this.href);
								//_this.href = "#";
								return false;
							}
						};
					}(el));
				}
			}
		},

		RatingSystem: function(form, rating_width) { //extend this method to do all selects
			var ul = document.createElement("ul");
			var p = document.createElement("p");
			DA.$(ul).addClass(TDM.Config.Selectors.RatingSystem);

			var select = DA.$('#'+form+' .'+TDM.Config.Selectors.UserRatingSystem+' select')[0];
			var options = DA.$(select).elmsByTag('option');
			var selected_option = '';

			var current_selection = document.createElement("li");
			DA.$(current_selection).addClass(TDM.Config.Selectors.Current);
			var current_selection_textCDATA = document.createTextNode("Current Selection");
			current_selection.appendChild(current_selection_textCDATA);
			ul.appendChild(current_selection);
			
			var node = '';
			for (var j=0; j<options.length; j++) {
				node = options[j];
				if (node.nodeName.toLowerCase() === "option") {
					var li = document.createElement("li");
					var a = document.createElement("a");
					var textCDATA = document.createTextNode(node.text);
					DA.$(li).addClass(node.id);
					li.title = node.title;

					DA.$(a).addEvent('click', function(selectElement, optionIndex, selectedOption, currentSelection) {
						return function () {
							selectElement.selectedIndex = -1;
							selectElement.selectedIndex = optionIndex;
							currentSelection.style.width = rating_width*(optionIndex+1) + 'px';

							var script_based_submit = DA.$('#'+form+'.'+TDM.Config.Selectors.ScriptBasedSubmit);

							if (script_based_submit.length > 0) {
								script_based_submit[0].submit();
							}
							else {
								return false;
							}
						};
					}(select, j, selected_option, current_selection));

					a.appendChild(textCDATA);
					li.appendChild(a);
					ul.appendChild(li);
				}
			}
			ul.id = select.id;

			DA.$(select).parentNode.insertBefore(ul, select.nextSibling);

			if (DA.$('.'+TDM.Config.Selectors.AverageUserRating)) {
				DA.$('#'+form+' .'+TDM.Config.Selectors.RatingSystem+' .'+TDM.Config.Selectors.Current)[0].style.width = DA.$('.'+TDM.Config.Selectors.AverageUserRating)[0].value*rating_width + "px";
			}
		},


		ValidateRequiredFields: function($form_element) {
			$form_element.addClass('vrf_init');
			$form_element.addEvent('submit', function(_this) {
				return function (e) {
					var r_v = true;
					var required_fields = DA.$('#'+_this.id+' *[class*='+TDM.Config.Selectors.Validate+'_]');
					var error_message_elements = DA.$('#'+_this.id+' .'+TDM.Config.Selectors.ErrorMessage);
					if (error_message_elements) {
						error_message_elements.each(function(){
							DA.$(this).remove();
						});
					}
					required_fields.each(function(){
						DA.$(this).removeClass(TDM.Config.Selectors.Error);
	
						var required_field_classes = DA.$(this).className.split(" ");
						for (var c=0; c<required_field_classes.length; c++) {
							var v_re = required_field_classes[c].match(new RegExp('^'+TDM.Config.Selectors.Validate+'_([^_]+)_(.+)'));

							if (v_re) {
								var checktype = v_re[1];
								var fieldname = v_re[2];
								for (var v=0; v<TDM.Config.ValidationFunctionList.length; v++) {
									if (TDM.Config.ValidationFunctionList[v].checktype===checktype) {
										if(!TDM.Config.ValidationFunctionList[v].func(DA.$(this).value, required_field_classes[c])){
											TDM.Utils.Set.ErrorClass(DA.$(this));
											TDM.Utils.Set.ErrorMessage(DA.$(this), checktype+'_'+fieldname, TDM.Config.ValidationFunctionList[v].error);
											r_v = false;
										}
									}
								}
							}
						}
					});
	
					location.hash = '#' + _this.id;
	
					if (!r_v) {
						TDM.Utils.Set.ErrorMessageGlobal(_this, TDM.Config.Selectors.ErrorMessageGlobal);
					}
					return r_v && true;
				};
			}($form_element));
		},

		ValidateField: {
			Isset: function(str) {
				if (str.length > 0) {
					var r_v = true;
				}
				return r_v || false;
			},
			Pattern: {
				Email: function (str) {
					if (TDM.Config.Init.Email_Pattern.test(str)) {
						var r_v = true;
					}
					return r_v || false;
				},
				EmailMultiple: function (str) {
					multiple_emails = str.split(" ");
					var r_v = true;
					for (var y=0; y<multiple_emails.length; y++) {
						if (!TDM.Config.Init.Email_Pattern.test(multiple_emails[y])) {
							r_v = false;
						}
					}
					return r_v || false;
				},

				PostalCode: function(str) {
					if (TDM.Config.Init.Postal_Code_Pattern.test(str)) {
						var r_v = true;
					}
					return r_v || false;
				}
			},
			Identical: function (str_x, str_y) {
				if (str_x.toLowerCase() === str_y.toLowerCase()) {
					var r_v = true;
				}
				return r_v || false;
			},
			IdenticalField: function (str, element_class) {
				var elements = DA.$("."+element_class);
				var init_value = '';
				var r_v = true;

				for (var i=0; i<elements.length; i++) {
					if (init_value === '') {
						init_value = elements[i].value;
					}
					else {
						if (elements[i].value.toLowerCase() != init_value.toLowerCase()) {
							r_v = false;
						}
					}
				}
				return r_v || false;
			}
		},
		Set: {
			ErrorClass: function(el) {
				el.addClass(TDM.Config.Selectors.Error);

				el.addEvent('focus', function () {
					el.removeClass(TDM.Config.Selectors.Error);
				});
			},
			ErrorMessage: function (el, error_message_value, default_error) {
				var error_message_text = TDM.Config.Messages.Errors[error_message_value] ? TDM.Config.Messages.Errors[error_message_value] : default_error;
				var error_message = document.createElement("em");
				var error_message_textCDATA = document.createTextNode(error_message_text);

				DA.$(error_message).addClass(TDM.Config.Selectors.ErrorMessage);
				error_message.appendChild(error_message_textCDATA);

				el.parentNode.insertBefore(error_message, el.nextSibling);
			},
			ErrorMessageGlobal: function(el, default_error) {
				var modifyClass = TDM.Utils.ClassName;
				var settings = TDM.Config.Selectors;

				var error_message_global_text = TDM.Config.Messages.Errors.error_message_global_value ? TDM.Config.Messages.Errors.error_message_global_value : default_error;
				var error_message_global = document.createElement("p");

				error_message_global.id = "error_message_global";
				DA.$(error_message_global).addContent(error_message_global_text);

				DA.$(error_message_global).addClass(TDM.Config.Selectors.ErrorMessage);

				var fieldset_zero = el.getElementsByTagName('fieldset')[0];

				var fcn = fieldset_zero.childNodes;
				for (var i=0; i<fcn.length; i++) {
					if (fcn[i].nodeType === 1) {
						fieldset_zero.insertBefore(error_message_global, fcn[i]);
						break;
					}
				}
			}
		},

		WindowPrint: function (actions) {
			actions.each(function() {
				DA.$(this).addEvent('click', function() {
					window.print();
				});

			});
		},

		InputValueTip: function ($input, valueText) { 
			$input.setAttributes({value : valueText});
			$input.addEvent ('focus', function(_this) {
				return function (e) {
					if (_this[0].value === valueText) {_this.setAttributes({value : ""});}
				}; 
			}($input));			
		
			$input.addEvent ('blur', function(_this) { 
				return function (e) {
					if (_this[0].value === "") {
						_this.setAttributes({value : valueText});
					}
				};
			}($input));
		},

		FigureView: {},
		PageInformation: {},

		ContentBrowser: function (parent, datas, limit) {
			TDM.Config.Init.CurrentView[parent] = 0;

			div = document.createElement("div");
			ul = document.createElement("ul");
			li_1 = document.createElement("li");
			li_2 = document.createElement("li");
			div.className = 'content_browser';
			ul.className = 'controls';
			li_1.className = TDM.Config.Selectors.Previous;
			li_2.className = TDM.Config.Selectors.Next;
			ul.appendChild(li_1);
			ul.appendChild(li_2);
			div.appendChild(ul);

			var actions='';
 			if (DA.$('#'+parent+' ul.content_view_actions li').length > 0) {
				actions =  DA.$('#'+parent+' ul.content_view_actions li');
			}
			else {
				ul = document.createElement("ul");
				ul.className = 'content_view_actions';
				for (var i=0; i<datas.length; i++) {
					var li = document.createElement("li");
					var a = document.createElement("a");
					a.href = '#'+parent+"_"+i;
					var a_textCDATA = document.createTextNode(i);
					a.appendChild(a_textCDATA);
					li.appendChild(a);
					ul.appendChild(li);

				}

				DA.$(parent).appendChild(ul);

				actions = DA.$('#'+parent+' ul.content_view_actions li');
			}

			DA.$('#'+parent).addContent(div);

			for (var j=TDM.Config.Init.CurrentView[parent]; j<datas.length; j++) {
				if (j < limit) {
					datas[j].className += ' '+TDM.Config.Selectors.Opened;
					actions[j].className += ' '+TDM.Config.Selectors.Selected;
				}
				else {
					datas[j].className += ' '+TDM.Config.Selectors.Closed;
				}
			}

			prev = DA.$('#'+parent+' ul.controls li.'+TDM.Config.Selectors.Previous);
			next = DA.$('#'+parent+' ul.controls li.'+TDM.Config.Selectors.Next);

			prev[0].style.display = 'none';

			prev.addEvent('click', function(parent, datas, limit, direction, _this) {
				return function (e) {
					TDM.Utils.ContentController(parent, datas, limit, direction, _this);

					next = DA.$('#'+parent+' ul.controls li.'+TDM.Config.Selectors.Next);
					_this[0].style.display = 'block';
					next[0].style.display = 'block';

					if (TDM.Config.Init.CurrentView[parent] <= 0) {
						_this[0].style.display = 'none';
					}
				};
			}(parent, datas, limit, -1, prev));

			next.addEvent('click', function(parent, datas, limit, direction, _this) {
				return function (e) {
					TDM.Utils.ContentController(parent, datas, limit, direction, _this);

					prev = DA.$('#'+parent+' ul.controls li.'+TDM.Config.Selectors.Previous);
					prev[0].style.display = 'block';
					_this[0].style.display = 'block';

					if (TDM.Config.Init.CurrentView[parent] >= datas.length-limit) {
						_this[0].style.display = 'none';
					}
				};
			}(parent, datas, limit, 1, next));

			prev = next = null;
		},

		ContentController: function(parent, datas, limit, direction, _this) {
			var content_view_top = TDM.Config.Init.CurrentView[parent];
			content_view_top = content_view_top+direction;

			if (content_view_top <= 0) {
				content_view_top = 0;
			}
			if (content_view_top >= datas.length-limit) {
				content_view_top = datas.length-limit;
			}

			datas.each(function() {
				DA.$(this).removeClass(TDM.Config.Selectors.Closed);
				DA.$(this).removeClass(TDM.Config.Selectors.Opened);
			});

			for (var j=0; j<datas.length; j++) {
				if (content_view_top <= j && j < content_view_top+limit) {
					datas[j].className += ' '+TDM.Config.Selectors.Opened;
				}
				else {
					datas[j].className += ' '+TDM.Config.Selectors.Closed;
				}
			}
			TDM.Config.Init.CurrentView[parent] = content_view_top;
		}
	}
};



var cards = [];

TDM.Config.ValidationFunctionList = [
	{"checktype":"isset", "func":TDM.Utils.ValidateField.Isset, "error":"error: no set"},
	{"checktype":"patternemail", "func":TDM.Utils.ValidateField.Pattern.Email, "error":"error: incorrect email format"},
	{"checktype":"patternemailmultiple", "func":TDM.Utils.ValidateField.Pattern.EmailMultiple, "error":"error: seperate by space AND check pattern"},
	{"checktype":"identicalemail", "func":TDM.Utils.ValidateField.IdenticalField, "error":"error: unidentical emails"},
	{"checktype":"identicalpassword", "func":TDM.Utils.ValidateField.IdenticalField, "error":"error: unidentical password"}
];

function Go() {
	if (DOMAssistant.$('#email_newsletter').length > 0) {
		TDM.Utils.ValidateRequiredFields(DOMAssistant.$('#email_newsletter')[0]);
	}

	if (DOMAssistant.$('#form_send_to_friend').length > 0) {
		TDM.Utils.ValidateRequiredFields(DOMAssistant.$('#form_send_to_friend')[0]);
	}

	TDM.Utils.OpenResourceOnNewWindow();
}
		
		function poll_vote(q,a) {
			if(window.XMLHttpRequest)  
			    	pollajax = new XMLHttpRequest();
			else 	{
					if(window.ActiveXObject) 
							pollajax = new ActiveXObject("Microsoft.XMLHTTP");
			}
		
			// Send command
			if(navigator.userAgent.match("Safari")){
						pollajax.open("GET","/poll/" + q + ".php?action=poll_vote&pollId="+q+"&answers[]="+a,false);
			 } else {
						pollajax.open("GET","/poll/" + q + ".php?action=poll_vote&pollId="+q+"&answers[]="+a);
			 }
			
			pollajax.onreadystatechange=function() {
				if (pollajax.readyState == 4) {
					if (document.all  && !document.getElementById) {
					   document.all['pool_hentry'].innerHTML=pollajax.responseText;
					   setListHeight('#site_sections_overview', 1);
					} else {
					   document.getElementById('pool_hentry_'+q).innerHTML=pollajax.responseText;
					   setListHeight('#site_sections_overview', 1);
					}
				}
		   }
			
			// Send output
			pollajax.send("");
		}
		
		function poll_answer(question) {
			
			var answers = new Array();
			for (var i=0; i < document.forms['form_poll_'+question].answers.length; i++) {
				if (document.forms['form_poll_'+question].answers[i].checked) {
					answers.push(document.forms['form_poll_'+question].answers[i].value);
				}
			}
			
			if(answers.length==0) {
						document.getElementById('try_again_pool_'+question).style.display="inline";
						return false;
			}	else    document.getElementById('try_again_pool_'+question).style.display="none";
			
			poll_vote(question, answers);
			return false;
		}
		
 	function showSignUp()
 	{
      document.getElementById("homegarden_network_account").className="sign_up";
      document.getElementById("homegarden_network_account").innerHTML= 
      document.getElementById("homegarden_network_account_signup").innerHTML;
      document.getElementById("homegarden_network_account").style.display="block";
  	}
	
	function signUp()
	{ 
		var xmlHttp;
		try
		  {  // Firefox, Opera 8.0+, Safari  
		  xmlHttp=new XMLHttpRequest();  
		  }
		catch (e)
		  {  // Internet Explorer  
		  try
		    {    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");   
		    }
		  catch (e)
		    {    try
		      {      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		      }
		    catch (e)
		      {      alert("Your browser does not support AJAX!");      
		      return false;      
		      }    
		     }  
		   }  
		if(navigator.userAgent.match("Safari")){
	  		xmlHttp.open("GET","/api/signup.php",false);
	    } else {
	    	xmlHttp.open("GET","/api/signup.php");
	    }
		xmlHttp.onreadystatechange=function()
	    {
		    if(xmlHttp.readyState==4)
		      {
		      		document.getElementById("homegarden_network_account").className="sign_up";
		      		document.getElementById("homegarden_network_account").innerHTML=xmlHttp.responseText;
		      }
	    }
	  	
	  	xmlHttp.send("");
	}
	
	function signIn()
	{ 
		var xmlHttp;
		try
		  {  // Firefox, Opera 8.0+, Safari  
		  xmlHttp=new XMLHttpRequest();  
		  }
		catch (e)
		  {  // Internet Explorer  
		  try
		    {    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");   
		    }
		  catch (e)
		    {    try
		      {      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		      }
		    catch (e)
		      {      alert("Your browser does not support AJAX!");      
		      return false;      
		      }    
		     }  
		   }  

		if(navigator.userAgent.match("Safari")){
	  		xmlHttp.open("GET","/api/signin.php",false);
	    } else {
	    	xmlHttp.open("GET","/api/signin.php");
	    }
	  	
		xmlHttp.onreadystatechange=function()
	    {
		    if(xmlHttp.readyState==4)
		      {
		      		document.getElementById("homegarden_network_account").className="";
		      		document.getElementById("homegarden_network_account").innerHTML=xmlHttp.responseText;
		      }
	    }
	  	
	  	xmlHttp.send("");
	}
	
	function forumCheckUserCredentials(email,password,newLink)
	{
		var email=document.getElementById(email).value; 		//"network_login_email"
		var password=document.getElementById(password).value; 	//"network_login_password"
		var xmlHttp;
		try
		  {  // Firefox, Opera 8.0+, Safari  
		  xmlHttp=new XMLHttpRequest();  
		  }
		catch (e)
		  {  // Internet Explorer  
		  try
		    {    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");   
		    }
		  catch (e)
		    {    try
		      {      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		      }
		    catch (e)
		      {      alert("Your browser does not support AJAX!");      
		      return false;      
		      }    
		     }  
		}
		
		if(navigator.userAgent.match("Safari")){ 
			xmlHttp.open("GET","/api/login.php?action=user_login&email="+email+"&password="+password,false);
	  	} else {
	  		xmlHttp.open("GET","/api/login.php?action=user_login&email="+email+"&password="+password);
	  	}
	  	
		xmlHttp.onreadystatechange=function()
	    {
		    if(xmlHttp.readyState==4)
		    {
		        if (null!=xmlHttp.responseText.match("Welcome"))
		        {
		      		document.getElementById("homegarden_network_account").className="log_out";
					tb_remove();
					window.location.href = newLink;
				} else {
		      		document.getElementById("homegarden_network_account").className="user_exist";
		      		tb_remove();
		      	}
				document.getElementById("homegarden_network_account").innerHTML=xmlHttp.responseText;
				tb_remove();
			}
	    }
	  	
	  	xmlHttp.send("");
	  	return false;
	}
	
	function forumCheckUserClose()
	{
		tb_remove();
		return false;
	}
	
	function checkUserCredentials(email,password,remember_me)
	{ 
		var email=document.getElementById(email).value; 		//"network_login_email"
		var password=document.getElementById(password).value; 	//"network_login_password"
		var remember_me=document.getElementById(remember_me).checked?1:0; 	//"remember_me"
		var xmlHttp;
		try
		  {  // Firefox, Opera 8.0+, Safari  
		  xmlHttp=new XMLHttpRequest();  
		  }
		catch (e)
		  {  // Internet Explorer  
		  try
		    {    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");   
		    }
		  catch (e)
		    {    try
		      {      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		      }
		    catch (e)
		      {      alert("Your browser does not support AJAX!");      
		      return false;      
		      }    
		     }  
		}
		
		if(navigator.userAgent.match("Safari")){ 
			xmlHttp.open("GET","/api/login.php?action=user_login&email="+email+"&password="+password+"&remember_me="+remember_me,false);
	  	} else {
	  		xmlHttp.open("GET","/api/login.php?action=user_login&email="+email+"&password="+password+"&remember_me="+remember_me);
	  	}
	  	
		xmlHttp.onreadystatechange=function()
	    {
		    if(xmlHttp.readyState==4)
		    {
		        if (null!=xmlHttp.responseText.match("Welcome"))
		        {
		      		document.getElementById("homegarden_network_account").className="log_out";
		      		window.location.reload();
				} else {
		      		document.getElementById("homegarden_network_account").className="user_exist";
		      	}
				document.getElementById("homegarden_network_account").innerHTML=xmlHttp.responseText;
			}
	    }
	  	
	  	xmlHttp.send("");
	  	return false;
	}
	
	function register()
	{ 
		var nickname=document.getElementById("email_user_name").value;
		var email=document.getElementById("email_signup").value;
		var password=document.getElementById("email_password").value;
		var email_newsletter=document.getElementById("email_newsletter").checked?1:0;
		var emailConfirmation=email;
		var passwordConfirmation=password;
		var params="";
		var firstName="";
		var lastName="";
			
		var xmlHttp;
		params=params+"&nickname="+nickname;
		params=params+"&email="+email;
		params=params+"&password="+password;
		params=params+"&emailConfirmation="+emailConfirmation;
		params=params+"&passwordConfirmation="+passwordConfirmation;
		params=params+"&firstName="+firstName;
		params=params+"&lastName="+lastName;	
		params=params+"&email_newsletter="+email_newsletter;	
		try
		  {  // Firefox, Opera 8.0+, Safari  
		  xmlHttp=new XMLHttpRequest();  
		  }
		catch (e)
		  {  // Internet Explorer  
		  try
		    {    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");   
		    }
		  catch (e)
		    {    try
		      {      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		      }
		    catch (e)
		      {      alert("Your browser does not support AJAX!");      
		      return false;      
		      }    
		     }  
		   }  
		
		if(navigator.userAgent.match("Safari")){
	  		xmlHttp.open("GET","/api/register.php?action=user_light_registration"+params,false);
	    } else {
	    	xmlHttp.open("GET","/api/register.php?action=user_light_registration"+params);
	    }
	  	
		xmlHttp.onreadystatechange=function()
	    {
		    if(xmlHttp.readyState==4)
		      {
		      	if (null!=xmlHttp.responseText.match("Welcome")){
		      		document.getElementById("homegarden_network_account").className="log_out";
		      	}
		      	else if (null!=xmlHttp.responseText.match("Username Already exists")){
		      		document.getElementById("homegarden_network_account").className="user_exist";
		      	}	
      	      	else if (null!=xmlHttp.responseText.match("E-mail Already Exists")){
		      		document.getElementById("homegarden_network_account").className="email_exist";
		      	}	
		      	else {
		      		document.getElementById("homegarden_network_account").className="network_error_message";
		      	}	
		      	
		      	document.getElementById("homegarden_network_account").innerHTML=xmlHttp.responseText;
		      }
	    }
	  	
	  	xmlHttp.send("");
	}

	function logout()
	{ 
		var xmlHttp;
		try
		  {  // Firefox, Opera 8.0+, Safari  
		  xmlHttp=new XMLHttpRequest();  
		  }
		catch (e)
		  {  // Internet Explorer  
		  try
		    {    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");   
		    }
		  catch (e)
		    {    try
		      {      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		      }
		    catch (e)
		      {      alert("Your browser does not support AJAX!");      
		      return false;      
		      }    
		     }  
		   }  

	  	if(navigator.userAgent.match("Safari")){
	  		xmlHttp.open("GET","/api/logout.php",false);
	    } else {
	    	xmlHttp.open("GET","/api/logout.php");
	    }
	    
		xmlHttp.onreadystatechange=function()
	    {
		    if(xmlHttp.readyState==4)
		      {
		      		document.getElementById("homegarden_network_account").className="";
		      		document.getElementById("homegarden_network_account").innerHTML=xmlHttp.responseText;
		      		// we need to reload after a logout, fondamental for edit my profile, ....
		      		window.location.reload( false );
		      }
	    }
	  	
	  	xmlHttp.send("");
	  	return false;
	}
	
	function forgot_password()
	{ 
		var xmlHttp;
		try
		  {  // Firefox, Opera 8.0+, Safari  
		  xmlHttp=new XMLHttpRequest();  
		  }
		catch (e)
		  {  // Internet Explorer  
		  try
		    {    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");   
		    }
		  catch (e)
		    {    try
		      {      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		      }
		    catch (e)
		      {      alert("Your browser does not support AJAX!");      
		      return false;      
		      }    
		     }  
		   }  

		if(navigator.userAgent.match("Safari")){
	  		xmlHttp.open("GET","/api/forgot_password.php",false);
		} else {
	    	xmlHttp.open("GET","/api/forgot_password.php");
		}
	  	
	  	xmlHttp.onreadystatechange=function()
	    {
		    if(xmlHttp.readyState==4)
		      {
		      		document.getElementById("homegarden_network_account").className="forgot";
		      		document.getElementById("homegarden_network_account").innerHTML=xmlHttp.responseText;
		      }
	    }
	  	
	  	xmlHttp.send("");
	}

	function resendPassword()
	{ 

		var email=document.getElementById("forgot_email").value;
		var xmlHttp;
		try
		  {  // Firefox, Opera 8.0+, Safari  
		  xmlHttp=new XMLHttpRequest();  
		  }
		catch (e)
		  {  // Internet Explorer  
		  try
		    {    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");   
		    }
		  catch (e)
		    {    try
		      {      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		      }
		    catch (e)
		      {      alert("Your browser does not support AJAX!");      
		      return false;      
		      }    
		     }  
		   }  

	  	if(navigator.userAgent.match("Safari")){
	  		xmlHttp.open("GET","/api/resendPassword.php?action=web_user_lost_password&email="+email,false);
		} else {
	    	xmlHttp.open("GET","/api/resendPassword.php?action=web_user_lost_password&email="+email);
		}
		
		xmlHttp.onreadystatechange=function()
	    {
		    if(xmlHttp.readyState==4)
		      {
		      	if (null!=xmlHttp.responseText.match("Password Sent")){
		      		document.getElementById("homegarden_network_account").className="email_exist";
		      	}
		      	else {
		      		document.getElementById("homegarden_network_account").className="network_error_message";
		      	}	
		      	document.getElementById("homegarden_network_account").innerHTML=xmlHttp.responseText;      
		      }
	    }
	  	
	  	xmlHttp.send("");
	}
	
	function myAccountLoadProvince() 
	{
		country=document.account_hgn.country.value;
		 
		province="<option value=\"OTHERS\">OTHERS</option>"; 
		if(country=='UNITED STATES')
		{
			province=LoadUSStates();
		} else	if(country=='CANADA')
				{
					   province=LoadCAProvinces();
				} 
	
		if (document.all  && !document.getElementById)
		{	
			document.all['account_province'].innerHTML=province;
		} else {
			document.getElementById('account_province').innerHTML=province;
		}
		return false;
	}
		
		function LoadUSStates()
		{
			var usa = new Array();
			
			usa[0] = "Alabama"; 
			usa[1] = "Alaska"; 
			usa[2] = "Arizona"; 
			usa[3] = "Arkansas"; 
			usa[4] = "California"; 
			usa[5] = "Colorado"; 
			usa[6] = "Connecticut"; 
			usa[7] = "Delaware"; 
			usa[8] = "District Of Columbia"; 
			usa[9] = "Florida"; 
			usa[10] = "Georgia"; 
			usa[11] = "Hawaii"; 
			usa[12] = "Idaho"; 
			usa[13] = "Illinois"; 
			usa[14] = "Indiana"; 
			usa[15] = "Iowa"; 
			usa[16] = "Kansas"; 
			usa[17] = "Kentucky"; 
			usa[18] = "Louisiana"; 
			usa[19] = "Maine"; 
			usa[20] = "Maryland"; 
			usa[21] = "Massachusetts"; 
			usa[22] = "Michigan"; 
			usa[23] = "Minnesota"; 
			usa[24] = "Mississippi"; 
			usa[25] = "Missouri"; 
			usa[26] = "Montana";
			usa[27] = "Nebraska";
			usa[28] = "Nevada";
			usa[29] = "New Hampshire";
			usa[30] = "New Jersey";
			usa[31] = "New Mexico";
			usa[32] = "New York";
			usa[33] = "North Carolina";
			usa[34] = "North Dakota",
			usa[35] = "Ohio"; 
			usa[36] = "Oklahoma"; 
			usa[37] = "Oregon"; 
			usa[38] = "Pennsylvania"; 
			usa[39] = "Rhode Island"; 
			usa[40] = "South Carolina"; 
			usa[41] = "South Dakota",
			usa[42] = "Tennessee"; 
			usa[43] = "Texas"; 
			usa[44] = "Utah"; 
			usa[45] = "Vermont"; 
			usa[46] = "Virginia"; 
			usa[47] = "Washington"; 
			usa[48] = "West Virginia"; 
			usa[49] = "Wisconsin";  
			usa[50] = "Wyoming";
			
			province="";
			for(i=0;i<usa.length;i++)
			  {
				province += "<option value=\"";
				province += usa[i];
				province += "\">" + usa[i] + "</option>";
			  }
			return province;
		}
		
		function LoadCAProvinces()
		{
			var canada=new Array();    
			
			canada[0] = "British Columbia";
			canada[1] = "Ontario";
			canada[2] = "Newfoundland and Labrador";
			canada[3] = "Nova Scotia";
			canada[4] = "Prince Edward Island";
			canada[5] = "New Brunswick";
			canada[6] = "Nunavut";
			canada[7] = "Quebec";
			canada[8] = "Manitoba";
			canada[9] = "Saskatchewan";
			canada[10] = "Alberta";
			canada[11] = "Northwest Territories";
			canada[12] = "Yukon Territory";
			province="";
			
			for(i=0;i<canada.length;i++)
			  {
				province += "<option value=\"";
				province += canada[i];
				province += "\">" + canada[i] + "</option>";
			  }
			return province;
		}
		
		function myProfileUseOurAvatar(avatar) 
		{
			var xmlHttp;
			try
			  {  // Firefox, Opera 8.0+, Safari  
			  xmlHttp=new XMLHttpRequest();  
			  }
			catch (e)
			  {  // Internet Explorer  
			  try
			    {    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");   
			    }
			  catch (e)
			    {    try
			      {      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			      }
			    catch (e)
			      {      alert("Your browser does not support AJAX!");      
			      return false;      
			      }    
			     }  
			   }  

		  	if(navigator.userAgent.match("Safari")){
		  		xmlHttp.open("GET","/member/profile.php?useOurAvatar="+avatar,false);
			} else {
		    	xmlHttp.open("GET","/member/profile.php?useOurAvatar="+avatar);
			}
			
			xmlHttp.onreadystatechange=function()
		    {
			    if(xmlHttp.readyState==4)
			      {
			      		 document.images["my_profile_avatar"].src="/media/images/avatars/ourAvatars/"+avatar;
			      		 if (document.all  && !document.getElementById)
						  {	
							document.all['error_message'].innerHTML='Your avatar is now activated !<br>';
						  } else {
							document.getElementById('error_message').innerHTML='Your avatar is now activated !<br>';
						  }
			      		 return true;
			      }
		    }
		  	
		  	xmlHttp.send("");
		}
		
		function hide(id){
		    if (document.all  && !document.getElementById)
			 {	
				document.all[id].style.display="none";
			 } else {
				document.getElementById(id).style.display="none";
			 }
      		 return true;
		}
		function emailBoxReTitle(title,link)
		{
			try
			{
				document.getElementById('emailBoxTitle').innerHTML = title ;
				document.getElementById('postTitle').value = title ;	
				document.getElementById('email_page').value	=link;		
			}
			catch(e)
			{
				return false;
			}
		}
