/******************************
  SHARED JAVASCRIPT FUNCTIONS
******************************/


function toggleUlogin(toggle){
  if (toggle){
    document.loginform.ulogin.className = 'input_field_disabled';
    document.loginform.ulogin.disabled  = true;
    document.loginform.upw.className = 'input_field_disabled';
    document.loginform.upw.disabled  = true;
    document.loginform.autoLogin.checked = false;
    document.loginform.autoLogin.disabled = true;
  } else {
    document.loginform.ulogin.className = 'input_field';
    document.loginform.ulogin.disabled  = false;
    document.loginform.upw.className = 'input_field';
    document.loginform.upw.disabled  = false;
    document.loginform.autoLogin.disabled = false;
  }
}

function popHelp(name,path,width,height){
  window.open('help.php?name=' + name + '&path=' + path + '&width=' + width + '&height=' + height,name,'width=700,height=500,menubar=0,scrollbars=yes,resizable=yes');
}

function launchRegister(id){
  window.open('register.php?useid='+id,'register','menubar=0,width=500,height=500,scrollbars=yes,resizable=yes');
}

function popAbout(){
  window.open('about.php','about','menubar=0,width=750,height=700,scrollbars=yes,resizable=yes');
}

function popFeedback(){
  window.open('feedback.php','feedback','menubar=0,width=500,height=500,scrollbars=yes');
}

function poke(uid,username,gender){
  var he = 'they'; var his = 'their'; var him = 'them';
  if (!parseInt(uid)){ return; }
  if (gender == 'Male'){
    he = 'he'; var his = 'his'; var him = 'him';
  } else if (gender == 'Female') {
    he = 'she'; var his = 'her'; var him = 'her';
  }
  if (confirm("You have decided to poke "+username+".\n\nPoking "+username+" means sending "+him+" an anonymous email reminding "+him+" to update "+his+" wish list because you want to see some fresh gift ideas.\n\nOnce you poke "+username+" you will not be able to poke "+him+" again for a while. Continue?")){
    req = getAJAX();
    req.open("GET","ajax_poke.php?uid="+uid,true);
    req.send(null);
    req.onreadystatechange = function() {
      if(req.readyState==4) {
        if (req.responseText == 'success'){
          alert("Success!\n\n"+username+' has been poked. Hopefully '+he+' will update '+his+' wish list soon.');
        } else {
          alert("Hold on!\n\n"+'You are not able to poke '+username+' at this time. You may have poked '+him+' recently or an error may have occurred. Please wait a while and try again.');
        }
      }
    }
  }
}

function notifyme(){
  if (document.list.elements['check_notifyme'].checked){
    document.list.elements['check_notifyme'].checked = false;
  } else {
    document.list.elements['check_notifyme'].checked = true
  }
  /*
  if (!parseInt(uid)){ return; }
  req = getAJAX();
  req.open("GET","ajax_notify.php?uid="+uid,true);
  req.send(null);
  req.onreadystatechange = function() {
    if(req.readyState==4) {
      result = req.responseText.split("\n");
      if (result[0] == 'success'){
	document.list.elements['check_notifyme'].checked = ((result[1]==1)?true:false);
        return;
      } else {
        alert('An error has occurred and you were not able to sign up for a notification for changes to this wish list. Please try again later.');
      }
    }
  }
  */
}

function getAJAX(){
  var xmlHttp;
  try {
    xmlHttp=new XMLHttpRequest();
  } catch (e) {
    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;
      }
    }
  }
  return xmlHttp;
}


function parseAjaxResult(result){
  result_array = result.split("\n");
  ret = new Object();
  for (i = 0; i < result_array.length; i++){
    if (result_array[i].length){
      key = result_array[i].slice(0,result_array[i].indexOf(':'));
      val = result_array[i].slice(result_array[i].indexOf(':')+1);
      ret[key] = val.replace(/\r/g,"\n");
    }
  }
  return ret;
}


function urlEncode(str) {
  return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}


function confirmRelease(childId,childName) {
  emailAddress = document.getElementById('release_email').value;
  if (!emailAddress.length) {
    alert("You forgot to enter "+childName+"'s email address!");
  } else if (confirm("Are you sure you want to release "+childName+"? This action cannot be undone!")){
    document.location = 'myaccount.php?childid='+childId+'&release='+emailAddress;
  }
}
