  // mmagic.js
  // Revision History
  // --------------------------------------------------
  // See bottom of file.

  var domainName1 = 'www.moonrisemagic.com';
  var domainName2 = 'moonrisemagic.websitesource.net';

  function MM_reloadPage(init) {  //reloads the window if Nav4 resized
    if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
      document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
    else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
  }
  
  function bakecookieID() {
    var expireDate = new Date();
    expireDate.setDate(expireDate.getDate()+7);
    var curTime = new Date();
    curTime = curTime.getTime();
    document.cookie = "MoonriseMagicID=mm"+curTime.toString()+
        ";expires="+expireDate.toGMTString()+";path=/";
    return "mm"+curTime.toString();
  }
  
  function getcookieID() {
    var i;
    if (document.cookie ==  "") {  //no visitorID
      document.home_form.visitorIDField.value = bakecookieID();
    } else {
      var cookieArray = document.cookie.split("; ");
      for (i=0; i<cookieArray.length; i++) {
	    if (cookieArray[i].split("=")[0] == "MoonriseMagicID") {
          document.home_form.visitorIDField.value = cookieArray[i].split("=")[1];
	      return;
	    } //end if
      } // end for
    } // end if else
  }
  
  function deletecookieID() {
    var expireDate = new Date();
    expireDate.setDate(expireDate.getDate()-1);
    var thisCookie = document.cookie.split("; ");
    for (i=0; i<thisCookie.length; i++) {
        cookieName = thisCookie[i].split("=")[0];
        document.cookie = cookieName+"=;expires="+expireDate.toGMTString();
    }
  }
  
  function submitMyForm(optNo) {
    document.home_form.opt.value = optNo;
    if (optNo == 6) { bakecookieID(); }
    document.home_form.submit();
  }

  //verify form element
  var msg = "";
  var empty_fields = "";

  function verifyElement(e) {
    if (e.value == "") { empty_fields += '\n\n    ' + e.name;	}
  }
  
  function verifyEmail(e) {
    if (e.value.search(/^\w[\-\.\w]*\@\w[\-\.\w]*\.[a-zA-Z]{2,3}$/) != 0) { 
      empty_fields += '\n\n    Accepted Email Format: Mailbox@DomainName\n    Only alphanumeric, underline, hyphen and period characters are allowed.'; }
  }

  function verifyCC(e) {
    if (e.value.search(/^[456]\d{3}([\-\s]?\d{4}){3}/) != 0) { 
      empty_fields += '\n\n    Credit Card: We accept Visa, MC and Discover'; }
  }

  function verifyExpireDate(e1, e2) {
    if ((e1.value.search(/^\d{1,2}/) != 0) && (e2.value.search(/^\d{1,2}/) != 0)) {
      empty_fields += '\n    ' + e1.name + '\n    ' + e2.name; }
  }

  function verifyCCVer(e) {
    if (e.value.search(/^\d{3}/) != 0) { 
      empty_fields += "\n    Enter 3-digit verification number"; }
  }

  //verify form
  function verifyForm() {
    verifyElement(document.home_form.Bill_Name);
    verifyElement(document.home_form.Bill_Address1);
    verifyElement(document.home_form.Bill_City);
    verifyElement(document.home_form.Bill_State);
    verifyElement(document.home_form.Bill_ZipCode);
    verifyElement(document.home_form.Bill_Country);
    verifyElement(document.home_form.Bill_Phone);
    if (document.home_form.Same_As_Bill.value == "No") {
      verifyElement(document.home_form.Ship_Name);
      verifyElement(document.home_form.Ship_Address1);
      verifyElement(document.home_form.Ship_City);
      verifyElement(document.home_form.Ship_State);
      verifyElement(document.home_form.Ship_ZipCode);
      verifyElement(document.home_form.Ship_Country);
      verifyElement(document.home_form.Ship_Phone);
    }
    verifyEmail(document.home_form.Email);
    verifyElement(document.home_form.Name_on_Card);
    verifyCC(document.home_form.CreditCard);
    verifyExpireDate(document.home_form.Expire1, document.home_form.Expire2);
    verifyCCVer(document.home_form.CCVerify);

    msg = 'There is a problem with one or more fields.\n ';
    msg += 'Please fill in or correct the following fields:';
	
    if (empty_fields) {
      msg += empty_fields + '\n ';
      alert(msg);
    }
  }

  //complete order, thank you
  function completeOrder() {
    msg = "";
    empty_fields = "";
    verifyForm();
    if (empty_fields) { return; }
    deletecookieID();
    submitMyForm(6);
  }

  function myOpenMediaFile(optNo, fileName) {
    document.home_form.opt.value = optNo;
    document.home_form.mediaFile.value = fileName;
    document.home_form.submit();
  }

  // Revision History
  // --------------------------------------------------
  // Revsion 1.00
  // 1. Add email syntax verification.
  // Revsion 1.01
  // 1. Added CC verification check.
  // Revsion 1.02
  // 1. Added second street address.
  // Revsion 1.03
  // 1. Added function myOpenMediaFile.
