////////////////
// SEND TO FRIEND DIALOG
var gErrInput;
var sendWin;
function initSend() {
  sendWin = new YAHOO.widget.Panel("popup-send",
                                   { fixedcenter:true, close:false, underlay: 'none', autofillheight:'body',
                                     draggable:false, zIndex:10, modal:true, visible:false, constraintoviewport:true
                                   });
  sendWin.render();
}
function showSend(){
  sendWin.show();
}
function hideSend() {
  sendWin.hide();
  clearError();
}
initSend();
// END SEND DIALOG

// COMMON
function eventOnSuccess(){
  alert('Your message was sent!');
}
var responseFailure = function(o){
  alert('ERROR: ' + o.responseText);
};
function validateValue(value){
  if( (value == undefined) || (value == '') ){
    return false;
  }else{
    return true;
  }
}
function validateEmailSyntax(email){
  var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  if(reg.test(email) == false) {
    return false;
  }
  return true;
}

// SEND
var CBSend = {
  cache: false,
  success: function(o) {
    var resp = o.responseText;
    var myObj = eval( '(' + resp + ')' );
    var status = myObj.status;
    var status_msg = myObj.status_msg;
    if(status == 'fail'){
      alert(status_msg);
    }else{
      eventOnSuccess();
      hideSend();
    }
    return false;
  },
  failure: responseFailure
}
function aSend(aptid){
  var formObj = document.getElementById('send_form');
  YAHOO.util.Connect.setForm(formObj);
  var myUrl = '/ajax/sendFriend';
  YAHOO.util.Connect.asyncRequest('GET', myUrl, CBSend);
}
function ajaxSend(){
  var emailFrom = document.getElementById('emailFrom');
  var emailTo = document.getElementById('emailTo');
  var nameFrom = document.getElementById('nameFrom');
  var nameTo = document.getElementById('nameTo');
  if(validateValue(nameFrom.value)){
    if(validateValue(emailFrom.value)){
      if(validateEmailSyntax(emailFrom.value)){
        if(validateValue(nameTo.value)){
          if(validateValue(emailTo.value)){
            if(validateEmailSyntax(emailTo.value)){
              clearError();
              aSend();
              emailFrom.value = '';
              nameFrom.value = '';
              emailTo.value = '';
              nameTo.value = '';
            }else{
              showLoginError('The email address for your friend does not appear valid',emailTo);
	    }
          }else{
            showLoginError('Please enter a value for the your friend\'s email',emailTo);
          }
        }else{
          showLoginError('Please enter a value for the name of your friend',nameTo);
        }
      }else{
        showLoginError('Your email address appears invalid',emailFrom);
      }
    }else{
      showLoginError('Please enter your email address',emailFrom);
    }
  }else{
    showLoginError('Please enter your name',nameFrom);
  }
  return false;
}
function showLoginError(msg,curInput){
  clearError();
  var errBox = document.getElementById('modal-login-error');
  errBox.style.display = 'block';
  errBox.innerHTML = msg;
  if(curInput != undefined){
    gErrInput = curInput;
    gErrInput.style.border = '2px solid red';
    gErrInput.focus();
  }
}
function clearError(){
  document.getElementById('modal-login-error').innerHTML = '';
  if(gErrInput != undefined){
    gErrInput.style.border = 'solid 1px #aacfe4';
  }
}

