
// Copyright Babysoft, All Rights Reserved.
// This function belongs to the White Panel Library, use this function for all email form / subscription actions //


// The quick version for email subscription
// Param1: The backend action name, default: submitAction
// Return: null
function myEmail_postQuickSubscript(flag){
  var url = '';
  var tempAry = new Array();
  var param = '';
  var email = '';

  if (flag == undefined || flag == null || flag == ''){
    flag = 'submitAction';
  }

  if (document.getElementById("subscriptEmail") != null && document.getElementById("subscriptEmailMsg") != null){
    url = location.href;
    url = url.replace(/\#/g,'');
    tempAry = url.split('?');
    url = tempAry[0];
  }else{
    return false;
  }
  email = document.getElementById("subscriptEmail").value;
  if (email == '' || isValueEmail(email) === false){
    if (email == ''){
      alert("Please provide me your email address and try again.");
    }else if(isValueEmail(email) === false){
      alert("Subscribe Email format error, please try again.");
    }
    document.getElementById("subscriptEmail").focus();
    return false;
  }else{
    document.getElementById("subscriptEmail").value='';
  }
  param = flag + '=emailSubscript&subscriptEmail='+email;
  ajax_go(url, param, true,"subscriptEmailMsg");

  return;
}



// Popup a new screen with the email Contact form
// Param1: (Opt.) The action variable name in backend (default is submitAction)
// Param2: (Opt.) The ID you wish to use (Will display in subject & body message)
// Return: Null
function myEmail_showContactForm(action, id){

  uname = ssid = url = '';

  if (action == undefined || action == null || action == ''){
    action = 'submitAction';
  }

  if (id == undefined || id == null || id == ''){id = '';}


  if (document.getElementById('uname') != null){
    uname = document.getElementById('uname').value;
  }

  if (document.getElementById('ssid') != null){
    ssid = document.getElementById('ssid').value;
  }
  url = splitURLQuery();

  newWin = window.open('','contactForm','left=120,top=120,width=700,height=380,toolbar=no,resizable=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes');
  newWin.document.writeln('<center><br><br>Please wait for system loading<br><br></center>');
  newWin.document.writeln('<center><img src="./others/images/myloading2.gif"></center>');
  newWin.document.writeln('<form id="hiddenForm" method="post" action="'+url[0]+'">');
  newWin.document.writeln('<input type=hidden name="'+action+'" value="myEmail_ContactForm" />');
  newWin.document.writeln('<input type=hidden name="uname" value="'+uname+'" />');
  newWin.document.writeln('<input type=hidden name="ssid" value="'+ssid+'" />');
  newWin.document.writeln('<input type=hidden name="id" value="'+id+'" />');
  newWin.document.writeln('</form>');

  newWin.document.getElementById("hiddenForm").submit();
  newWin.focus();
  return;
}




// Popup a new screen with the "Email to friends" form
// Param1: (Opt.) The action variable name in backend (default is submitAction)
// Param2: (Opt.) The URL you wish to use (Used to display in body, shortcut link)
// Return: Null
function myEmail_showEmail2Friends(action, passurl){

  uname = ssid = url = '';

  if (action == undefined || action == null || action == ''){
    action = 'submitAction';
  }

  if (passurl == undefined || passurl == null || passurl == ''){id = '';}


  if (document.getElementById('uname') != null){
    uname = document.getElementById('uname').value;
  }

  if (document.getElementById('ssid') != null){
    ssid = document.getElementById('ssid').value;
  }
  url = splitURLQuery();

  newWin = window.open('','email2friendsForm','left=120,top=120,width=700,height=380,toolbar=no,resizable=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes');
  newWin.document.writeln('<center><br><br>Please wait for system loading<br><br></center>');
  newWin.document.writeln('<center><img src="./others/images/myloading2.gif"></center>');
  newWin.document.writeln('<form id="hiddenForm" method="post" action="'+url[0]+'">');
  newWin.document.writeln('<input type=hidden name="'+action+'" value="myEmail_Email2Friends" />');
  newWin.document.writeln('<input type=hidden name="uname" value="'+uname+'" />');
  newWin.document.writeln('<input type=hidden name="ssid" value="'+ssid+'" />');
  newWin.document.writeln('<input type=hidden name="url" value="'+passurl+'" />');
  newWin.document.writeln('</form>');

  newWin.document.getElementById("hiddenForm").submit();
  newWin.focus();
  return;
}



// The main JS submit function for all myEmail module
// Param1: (Req.) The action you wish to use
// Param2: (Opt.) The action variable name in backend (default is submitAction)
// Return: null
function myEmail_senddata(flag, action){

  var data = dataget();
  var tempAry = new Array();
  var temp = '';

  if (action==undefined || action==null || action==''){action='submitAction';}

  // Check and special handling for each enterance //
  if (flag == 'IDONTKNOW'){
    document.getElementById('submitForm').method = "GET";
  }

  else if (flag == 'myEmail_ContactForm'){
    if (document.getElementById('contact_name').value == ''){
      alert('May I have your name please ?');
      document.getElementById('contact_name').focus();
      return false;
    }
    if (document.getElementById('contact_contactInfo').value == ''){
      alert('Could you please tell me how to return your message ?');
      document.getElementById('contact_contactInfo').focus();
      return false;
    }
    else if(isEmail('contact_contactInfo') === false){return false;}

    // For UCruising.com Only, Remove in WP Library //
    if (document.getElementById('uc_deptcity').value == ''){
      alert('Could you please tell me your departure city ?');
      document.getElementById('uc_deptcity').focus();
      return false;
    }
    // END For UCruising.com Only, Remove in WP Library //

  }

  else if (flag == 'myEmail_Email2Friends'){
    if (document.getElementById('contactor_name').value == ''){
      alert('May I have your name please ?');
      document.getElementById('contactor_name').focus();
      return false;
    }
    if (document.getElementById('contactor_info').value == ''){
      alert('How about your contact ?');
      document.getElementById('contactor_info').focus();
      return false;
    }
    else if(isEmail('contactor_info') === false){return false;}

    if (document.getElementById('contactee_name').value == ''){
      alert('May I have your friends name please ?');
      document.getElementById('contactee_name').focus();
      return false;
    }
    if (document.getElementById('contactee_info').value == ''){
      alert('How about his/her contact ?');
      document.getElementById('contactee_info').focus();
      return false;
    }
    else if(isEmail('contactee_info') === false){return false;}

  }


  // Re-do the URL to make sure it post correctly //
  tempAry = splitURLQuery();

  // Submit request !! //
  data[0] += '<input type=hidden name='+action+' value="'+flag+'">';
  if (data[1]==''){data[1] += ''+action+'='+flag;}else{data[1] += '&'+action+'='+flag;}
  if (document.getElementById('submitForm').method == 'get'){
    location.href = tempAry[0] + '?' + data[1];
  }else{
    document.getElementById('submitFlag').innerHTML = data[0];
    document.getElementById("submitForm").submit();
  }
  return false;

}



