$(document).ready(function()
{
  $("#country").change(function()
  {    
    $("#states").empty();
    country = $(this).val();    
    if (country !== "")
    {
      $.post("index.php?page=ajax_functions", {mode: "get_states", country: country}, onAjaxGetStatesSuccess);
    }
  });
  
  function onAjaxGetStatesSuccess(data)
  {
    if ($("error_msg", data).text() == "")
    {
      options = "<option value=\"\">- Select " + $("st_title", data).text() + " -</option>";
      $("state", data).each(function()
      {
        st = $(this).text();
        options = options + "<option value=\"" + st + "\">" + st + "</option>";
      });
      $("#states").append(options);
    }
  }
  
  $("#select_member").change(function()
  {
    id_val = $(this).val();
    if (id_val !== "") $.post("index.php?page=ajax_functions", {mode: "get_user_info", id: id_val}, onAjaxGetUserinfoSuccess);
    else 
    {
      $("#psw").val("");    
      $("#psw-label").hide();
      $("#psw-field").hide();  
      $("#type option").each(function()
      {
        if ($(this).val() == "none") $(this).attr("selected", "selected");
      });      
      $("#is_usa").attr("checked", false);      
      $("#is_deleted").attr("checked", false); 
    }
  });
  
  function onAjaxGetUserinfoSuccess(data)
  {
    if ($("error_msg", data).text() == "")
    {            
      var member_id = $("member", data).attr("id");
      if ($("member_id", data).text() != member_id)   
      {  
//        $("#psw-label").hide();
//        $("#psw-field").hide();
        for (i=1; i<=5; i++)        
        {                  
          if ($("#member-"+i)) $("#member-"+i).show();
        }
        
        $("#type option").each(function()
        {
          if ($(this).val() == $("type", data).text()) $(this).attr("selected", "selected");
        });
        if ($("is_usa", data).text() == 1) is_usa = "checked";
        else is_usa = false;
        $("#is_usa").attr("checked", is_usa);
        if ($("is_deleted", data).text() == 1) is_del = "checked";
        else is_del = false;
        $("#is_deleted").attr("checked", is_del);
        $("#del-button").attr({disabled: "", onclick: ""}).click(function(){
          if (confirm("Are you sure?"))
            location.href = "index.php?page=manage_account&cmd=delete&id="+member_id;
        });
      }
      else 
      {        
        for (i=1; i<=5; i++)        
        {          
          $("#member-"+i).hide();
        }
//        $("#psw").val($("password", data).text());
//        $("#psw-label").show();
//        $("#psw-field").show();
      }
    }
  }
  
  var cancel_comment = function ()
  {
    $(this).nextAll("div[id^='div-comment-']:first").hide();
    $(this).val("ADD");
  };
  var add_comment = function ()
  {    
    $(this).nextAll("div[id^='div-comment-']:first").show();
    $(this).val("CANCEL");
  }
  $("input[id^='add-comment-']").toggle(add_comment, cancel_comment);
  
  $("input[id^='submit-comment-']").click(function()
  {
    var c;
    var n;
    $(this).prevAll("input[type='hidden']").each(function()
    {      
      if ($(this).attr("name") == "c") c = $(this).val();
      if ($(this).attr("name") == "n") n = $(this).val();     
    });
    var note = $(this).prevAll("textarea[name='note_comment']").val();    
    $.post("index.php?page=contact_record", {cmd: "add_comment", note_comment: note, c: c, n: n}, function(){window.location.href="index.php?page=contact_record&c="+c;})    
  });
});

/*
	Text highlighter.	
	* Copyright Stas Davydov & Outcorp (c) 2007
		http://davidovsv.narod.ru/
		http://outcorp-ru.blogspot.com/
*/
function mark(el, text, rec, minLen) 
{
	var replaced = false;
	
	for(var child = el.firstChild; child != null; child = child.nextSibling) 
	{
		if (child.nodeType == 3) 
		{	// TEXT_NODE					  
			var idx = -1;
			while ((idx = child.nodeValue.toLowerCase().indexOf(text.toLowerCase(), idx + 1)) != -1) 
			{
				if (idx > 0) 
				{
					var prefix = child.nodeValue.substr(0, idx);
					child.parentNode.insertBefore(document.createTextNode(prefix), child);					
				}
				var found = document.createElement("span");
				found.setAttribute("class", "found");
				found.setAttribute("className", "found");
				found.appendChild(document.createTextNode(child.nodeValue.substr(idx, text.length)));
				var suffix = document.createTextNode(child.nodeValue.substr(idx + text.length));
				child.parentNode.insertBefore(found, child);
				child.parentNode.insertBefore(suffix, child);
				child.parentNode.removeChild(child);
				child = found;
				var replaced = true;
				break;
		  }
		}
	}
	
  if (! rec && ! replaced && text.match(/[\s!@#$%^&*()_\-+={}\[\];:\"\'\`~\.\,\/]/gi)) 
	{
		var words = text.replace(/[\s!@#$%^&*()_\-+={}\[\];:\"\'\`~\.\,\/]/gi, " ").split(" ");
		for(var i = 0; i < words.length; i++)
			if (minLen ? words[i].length >= minLen : words[i] != "")
				replaced |= mark(el, words[i], true, minLen);
	}
	return replaced;
}
