$(document).ready(function(e){
  $('#find-contacts').bind('click', findContacts);
});

function delEmail(id) {
  if(confirm("Are you sure you want to delete this email?")){
    location.href='emails.php?act=del&id=' + id;
  }
}

function del_affiliate(id){
  if(confirm("Are you sure you wish to delete this affiliate?")){
    location.href = "ams.php?act=del&id=" + id;
  }
}

function MM_jumpMenu(targ,selObj,restore){
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function MM_jumpMenu2(targ,selObj,restore){
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function addNote(){
  $.get("add_note.php", $('#notes_form').formSerialize(), function (data) {
    $("#new_note").disabled = true;
    $("#add_note").disabled = true;
    $("#notes").html(data);
  });
}

function addContact(){
  $.post("ajax_functions.php?act=add_contact", $("#aff_form").formSerialize(), function(data){ 
    $("#cfirstname").attr('disabled', 'disabled');
    $("#clastname").attr('disabled', 'disabled');
    $("#cemail").attr('disabled', 'disabled');
    $("#cphone").attr('disabled', 'disabled');
    $("#cposition").attr('disabled', 'disabled');
  });
}

function delContact(id){
  $.post('ajax_functions.php?act=dc', 'id=' + id, function(){
    $('#contact-' + id).fadeOut();
  });
}

function findContacts(evt){
  $('#hunter-progress').show();
  $.post("ajax_functions.php?act=gc", $("#aff_form" ).formSerialize(),
  function(data){
    $('#emails_toadd').html(data).fadeIn('slow', $('#hunter-progress').hide());
  });
}

function addContacts(evt){
  formData = $('#aff_form').formSerialize();
  $.post("ajax_functions.php?act=ac", formData, function(data){
    $('#emails_toadd').fadeOut('slow', function(){
      $('#emails_toadd').html('Done');
      $('#emails_toadd').fadeIn(500, function(){
        location.reload(true);
      });
    }); 
  });
}

function updateRatings(evt){
  formData = $("#aff_form").formSerialize();
  $.post("ajax_functions.php?act=update_ypr", formData, function(data){
    $('#yahoopr')[0].value = data;
  });

  $.post("ajax_functions.php?act=update_gpr", formData, function(data) {
    $('#googlepr')[0].value = data;
  });
  
  $.post("ajax_functions.php?act=update_gbl", formData, function(data){
    $('#googlebl')[0].value = data;
  });
}

var xmlhttp

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 try {
  xmlhttp = new XMLHttpRequest();
 } catch (e) {
  xmlhttp=false
 }
}
function myXMLHttpRequest() {
  var xmlhttplocal;
  try {
    xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
 } catch (e) {
  try {
    xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
  } catch (E) {
    xmlhttplocal=false;
  }
 }

if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
 try {
  var xmlhttplocal = new XMLHttpRequest();
 } catch (e) {
  var xmlhttplocal=false;
  alert('couldn\'t create xmlhttp object');
 }
}
return(xmlhttplocal);
}


function sndReq(vote,id_num,ip_num) {
	
	var element = document.getElementById('unit_long'+id_num);
	//new Effect.Fade(element);
    element.innerHTML = '<div style="height: 30px;"><em>Loading ...</em></div>';
	
    xmlhttp.open('get', 'rpc.php?j='+vote+'&q='+id_num+'&t='+ip_num);
    xmlhttp.onreadystatechange = handleResponse;
    xmlhttp.send(null);
	
}

function handleResponse() {
    if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
       	
        var response = xmlhttp.responseText;
        var update = new Array();

        if(response.indexOf('|') != -1) {
            update = response.split('|');
            changeText(update[0], update[1]);
        }
		}
    }
}

function changeText( div2show, text ) {
    // Detect Browser
    var IE = (document.all) ? 1 : 0;
    var DOM = 0; 
    if (parseInt(navigator.appVersion) >=5) {DOM=1};

    // Grab the content from the requested "div" and show it in the "container"

    if (DOM) {
        var viewer = document.getElementById(div2show)
        viewer.innerHTML=text
    }
    else if(IE) {
        document.all[div2show].innerHTML=text
    }
}
