function err() {
$("invalid").style.visibility  = "visible";
$("invalid").innerHTML = '出现未知错误,请稍后再试!'; 
}

function showLoadingContacts() {
  $('btn').focus();
  ds = $('dim-screen');
  ds.innerHTML = "<div style='width:300px; background:#FFFFFF; border:10px solid black;padding:30px 10px 30px 10px;margin: 0 auto;margin-top:180px;font-weight:bold;font-size:1.1em;'><img src='/images/petal_spinner.gif' /><br /><br />正在获取联系人,请稍等!</div>";
  ds.style.display = 'block';
}

function reset_page_state() {
  $('dim-screen').style.display = 'none';
  $("btn").disabled = false;
  $("emailaddress").select();
}

function fetchContacts() {
  $('invalid').style.visibility = 'hidden';
  showLoadingContacts();
  $('btn').disabled = true;
	
  var emailaddress = $("emailaddress").value;
  var password = $("password").value;
  //var service = $('service').value;
  var pras = "account="+emailaddress+"&password="+password+"&type="+$("service").value;
  var url = "/Handler/EmailLogin.ashx";
  new Ajax.Request(url, {
    method:'post', 
    asynchronous:true, 
    parameters:pras,
    onSuccess: function(t) {
        var result = t.responseText;
        var array = result.split(":");
        if(array[0] == "0")
        {
            $("invalid").style.visibility  = "visible";
            $("invalid").innerHTML = array[1];
  
            reset_page_state();
            return;
        }
        else
        {
            poll_contact_cache(0);
        }
    
     },
    onFailure: function(t) { reset_page_state(); err(); }
  });
	return false;
}

function poll_contact_cache(tries) {
  var url = '/Handler/GetContact.ashx';
  new Ajax.Request(url, {
    method:'post', 
    asynchronous:true, 
    onSuccess: function(t) {
      var result = t.responseText;
      
      if(result.indexOf('nocontact') != -1) {
        redirect_with_host('/invitations/invitecontacts');
        return;
      }
      else if(result.indexOf('hascontact') != -1) {
        redirect_with_host('/invitations/followcontacts');
        return;
      }
      if(tries < 20) {
        setTimeout(function() { poll_contact_cache(tries+1) }, 5000);
      }
      else {
        reset_page_state();
        err();
      }
    },
    onFailure: function(t) { reset_page_state(); err(); }
  });
}

function sendContactInvites() {
	ifrm = $("invite_form");
	addresses = "";
	for(i=0; i < ifrm.elements['email_addresses'].length; i++) {
		f = ifrm.elements["email_addresses"][i];
		if(f.checked) { 
      addresses += f.value + ",";
    }
	}
	if(addresses == "") {
		alert("至少选择一个联系人.")
		return;
	}

	Element.toggle("invite_button");
	Element.toggle("invite_candy");
	var url = "/Handler/SendInvitationEmail.ashx";
	var pras = "emaillist="+addresses;
	new Ajax.Request(url, {
    method:'post', 
    parameters:pras,
    onSuccess: function() {
      Element.toggle("invite");
      Element.toggle("invites_sent");
    },
    onFailure: err
  });
	return false;
}

function follow_or_remove_all(follow) {
	var personCount = $("personCount").innerHTML;
	if(follow && personCount > 20 && !confirm("Are you sure you want to start following " + personCount + " people?")) return;

  peoples = '';
	for (i = 1; i <= personCount; i++) {
    id = $("u" + i).value;
    peoples += id + ' ';
    highlightPerson(id, follow);
	}
  follow_or_remove_multiple_from_string(peoples, follow);
}

function toggleEmails(select) {
	ifrm = document.getElementById("invite_form");
	for (x=0; x< ifrm.elements.length; x++) {
		ifrm.elements[x].checked=select;
	}
}

function followClick(user_id) {
	if( isFollowing(user_id)=="true" ) return;
	//if( isFollowing(user_id)=="false" ) { email = false; } else { email = true; }
	$("f" + user_id).value = true;
	highlightPerson(user_id, true);
	follow(user_id);
}
function follow(user_id)
{
    var url = '/Handler/Friendship.ashx';
    var pras = 'userid='+user_id+'&action=following';
    new Ajax.Request(url,{
    parameters:pras
    });
}

function highlightPerson(user_id, on_off) {
  p = $('p' + user_id);
  url = /url\(.*\)/.exec(p.style.background);

  on = document.getElementById('on' + user_id);
  off = document.getElementById('off' + user_id);

	if(on_off == true) {
		p.style.background = "#D9F4F5 " + url + " no-repeat 4px 4px";
		p.style.borderColor = "#84C2D2";
		$("n" + user_id).style.color = "#000";

		if(on != null && on.checked) {
      notify(true, user_id);
    } 
    else if(off != null) {
      off.checked = true;
		}

    $("c" + user_id).style.display = "none";
    $("n" + user_id).style.display = "block";
    $("r" + user_id).style.display = "block";
	} else {
		p.style.background = "#FFFFFF " + url + " no-repeat 4px 4px";
		p.style.borderColor = "#E5E5E5";
		$("n" + user_id).style.color = "#999";
    if( off != null ) off.checked = false;		
    if( on != null ) on.checked = false;		

    $("c" + user_id).style.display = "block";
    $("n" + user_id).style.display = "none";
    $("r" + user_id).style.display = "none";
	}
}

function removeClick(user_id) {
	removeUser(user_id);
	$("f" + user_id).value = "false";
	highlightPerson(user_id, false);
}

function removeUser(user_id)
{
    var url = '/Handler/Friendship.ashx';
    var pras = "userid="+user_id+"&action=delete";
    new Ajax.Request(url,{
    parameters:pras
    });
}

function overPerson(user_id, on) {
	if(isFollowing(user_id) == "true") return;
	if(on) { $("p" + user_id).style.borderColor='#84C2D2'; }
	else { $("p" + user_id).style.borderColor='#E5E5E5'; }
}

function isFollowing(user_id) {
  return $("f" + user_id).value;
}

function notifyClick(radio, user_id) {
	if(isFollowing(user_id) != "true") return;
	notify(radio.value, user_id);
}
