//
// javascript functions for athomewithwords.com and sharlataylor.com
//

// load the appropriate stylesheet
var agt    = navigator.userAgent.toLowerCase();
var is_mac = (agt.indexOf("mac")!=-1);
var is_nav = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('webtv')==-1));

var levelsDeepDelimeter = "";

for (i = 1; i<howManyLevelsDeep; i++) {
  levelsDeepDelimeter = levelsDeepDelimeter + "../"
}

// write the appropriate stylesheet depending on platform and browser
if (is_nav) {
  if (is_mac) {
    document.writeln("<link rel='stylesheet' type='text/css' href='" + levelsDeepDelimeter + "styles_ns_mac.css'>");
  } else {
    document.writeln("<link rel='stylesheet' type='text/css' href='" + levelsDeepDelimeter + "styles_ns_pc.css'>");
  }
} else {
  document.writeln("<link rel='stylesheet' type='text/css' href='" + levelsDeepDelimeter + "styles_ie_all.css'>");
}

// preload and instantiate button images
if (document.images) {
  btn_about_us_lo          = new Image();
  btn_about_us_lo.src      = levelsDeepDelimeter + "images/btn_about_us_lo.gif";

  btn_about_us_hi          = new Image();
  btn_about_us_hi.src      = levelsDeepDelimeter + "images/btn_about_us_hi.gif";
  
  btn_questionnaire_lo     = new Image();
  btn_questionnaire_lo.src = levelsDeepDelimeter + "images/btn_questionnaire_lo.gif";

  btn_questionnaire_hi     = new Image();
  btn_questionnaire_hi.src = levelsDeepDelimeter + "images/btn_questionnaire_hi.gif";
  
  btn_agreement_lo         = new Image();
  btn_agreement_lo.src     = levelsDeepDelimeter + "images/btn_agreement_lo.gif";

  btn_agreement_hi         = new Image();
  btn_agreement_hi.src     = levelsDeepDelimeter + "images/btn_agreement_hi.gif";
  
  btn_related_links_lo     = new Image();
  btn_related_links_lo.src = levelsDeepDelimeter + "images/btn_related_links_lo.gif";

  btn_related_links_hi     = new Image();
  btn_related_links_hi.src = levelsDeepDelimeter + "images/btn_related_links_hi.gif";
  
  btn_sharla_taylor_lo     = new Image();
  btn_sharla_taylor_lo.src = levelsDeepDelimeter + "images/btn_sharla_taylor_lo.gif";

  btn_sharla_taylor_hi     = new Image();
  btn_sharla_taylor_hi.src = levelsDeepDelimeter + "images/btn_sharla_taylor_hi.gif";
  
  btn_close_window_lo      = new Image();
  btn_close_window_lo.src  = levelsDeepDelimeter + "images/btn_close_window_lo.gif";
    
  btn_close_window_hi      = new Image();
  btn_close_window_hi.src  = levelsDeepDelimeter + "images/btn_close_window_hi.gif";
  
  btn_add_to_cart_hi       = new Image();
  btn_add_to_cart_hi.src   = levelsDeepDelimeter + "images/btn_add_to_cart_hi.gif";
  
  btn_add_to_cart_lo       = new Image();
  btn_add_to_cart_lo.src   = levelsDeepDelimeter + "images/btn_add_to_cart_lo.gif";  
  
  btn_view_cart_lo       = new Image();
  btn_view_cart_lo.src   = levelsDeepDelimeter + "images/btn_view_cart_lo.gif";  
  
  btn_view_cart_hi       = new Image();
  btn_view_cart_hi.src   = levelsDeepDelimeter + "images/btn_view_cart_hi.gif";  
}

// swap button images
function changeImages() {
  if (document.images) {
    for (var i=0; i<changeImages.arguments.length; i+=2) {
      document[changeImages.arguments[i]].src = eval(changeImages.arguments[i+1] + ".src");
    }
  }
}

// reset the order form pulldowns when loading the Agreement and Order Form page
function resetOrderForm() {
  // clear the checkbox indicating that the user agrees to the terms of service
  window.document.agreement.i_agree_to_the_terms.checked = false
}

// verify that a parameter is both indeed a valid number and that it is formatted like currency (i.e. it ends in .xx)
function checkForValidDollarAmount(xoTheForm) {
  vfDollarAmtCandidate = xoTheForm.fedex_sat_or_intl.value;  
  
  //validCurrencyFormat  = new RegExp(/\d{0,}[\.]\d{2}$/);
  validCurrencyFormat  = new RegExp(/^[0-9]*[\.]\d{2}$/);
  
  if (validCurrencyFormat.test(vfDollarAmtCandidate)) {
    return(true);
  } else {
    // issue a warning
    alert("The dollar amount you entered appears to be improperly formatted. Please enter currency in the format '$$$.cc' (i.e. 12.20 or 0.95). Do not use a dollar sign when entering amounts.");
    
    // redirect the user to the textbox where the invalid currency amount was entered
    xoTheForm.fedex_sat_or_intl.value = "";
    xoTheForm.fedex_sat_or_intl.focus();
    xoTheForm.fedex_sat_or_intl.blur();
    xoTheForm.fedex_sat_or_intl.select();
  }
}
 
// verify that user has check agreement. if yes, then error check. if no errors, send PayPal shopping cart request.
function processOrderRequest(xsItemName, xiItemNumber, xiAmount, xiShipping, xiShipping2, xiHandling) {
// verify that user has checked the user agreement
  if (window.document.agreement.i_agree_to_the_terms.checked != true) {
    alert("You must indicate by clicking the checkbox near the top of this page that you have read and agree to the terms of service before proceeding with your order. Thank You.");
  } else {
    // increment the order count var
    orderCount += 1;
    
    // send a shopping cart request to PayPal for the item
    xsPayPalFullURL =
        "'https://www.paypal.com/cart/add=1&business=staylor784%40msn.com" +
        "&item_name=" + xsItemName +
        "&item_number=" + xiItemNumber + "-" + orderCount +
        "&amount=" + parseFloat(xiAmount) +
        "&image_url=" + "https%3A//id36.securedata.net/rowellfamily/david/athomewithwords/images/hdr_athomewithwords_150x50.gif" +
        "&shipping=" + xiShipping +
        "&shipping2=" + xiShipping2 +
        "&handling=" + xiHandling +
        "&return=" + "http%3A//www.athomewithwords.com/agreement/success.html" +
        "&cancel_return=" + "http%3A//www.athomewithwords.com/agreement/cancel.html" +
        "&no_note=1','cartwin','width=700,height=400,scrollbars,location,resizable,status'"
         
    eval("window.open(" + xsPayPalFullURL + ");");
  }
}

// pop up a sized window (no scrollbar)
function popUpWindow( pageURL, winName, width, height) {
    xpos = (screen.width - width) / 2;
    ypos = (screen.height - height) / 2;
    window.open(
        pageURL,
        winName,
        "width=" + width + "," +
        "height=" + height + "," +
        "screenx=" + xpos + "," + //ns
        "screeny=" + ypos + "," + //ns
        "left=" + xpos + "," + //ie
        "top=" + ypos + "," + //ie
        "resizable=1" + "," +
        "scrollbars=1");
}

// pop up a sized window (with scrollbar)
function popUpWindowNoScroll( pageURL, winName, width, height) {
    xpos = (screen.width - width) / 2;
    ypos = (screen.height - height) / 2;
    window.open(
        pageURL,
        winName,
        "width=" + width + "," +
        "height=" + height + "," +
        "screenx=" + xpos + "," + //ns
        "screeny=" + ypos + "," + //ns
        "left=" + xpos + "," + //ie
        "top=" + ypos + "," + //ie
        "resizable=0" + "," +
        "scrollbars=0");
}

// validate form elements
function validateForm(field, validateType) {
  if (validateType == 'email') {
    if (!(isEmail(field.value))) {
      alert("Please enter a valid email address.");
      field.focus();
      field.blur();
      field.select();
    }
  } else {
    if ( (validateType == 'number') && isNaN(field.value) ) { 
      alert("Please enter a valid number.") 
      field.focus();
      field.blur();
      field.select();
    }
  }
}

// make sure an email is valid (syntactically at least)
function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

// go to some URL
function go(theURL) {
  location = eval("'"+theURL+"'");
}

// check to see if the user has already checked the 'i agree to the terms' checkbox earlier in their session
function getHasUserAlreadyAgreed() {
  theCookieState = getCookie('i_agree_to_the_terms');
  
  // user is visiting order page for the first time
  if (theCookieState == undefined) {
    //alert("undefined cookie");
    setCookie('i_agree_to_the_terms', 'false');
  // user has visited before and therefore a cookie has been set
  } else {
    switch (theCookieState){
      // cookie value is true, so set the 'i agree to the terms' checkbox to checked
      case "true" : 
        //alert("cookie value is true");
        window.document.agreement.i_agree_to_the_terms.checked = true;
        break;
      // cookie value is false, so set the 'i agree to the terms' checkbox to unchecked
      case "false" : 
        //alert("cookie value is false");
        window.document.agreement.i_agree_to_the_terms.checked = false;
        break;
    }  
  }

  //if (getCookie('i_agree_to_the_terms')) {
    //alert("value of cookie is " + getCookie('i_agree_to_the_terms') );
  //  window.document.agreement.i_agree_to_the_terms.checked = true;
  //}
}

// Set cookie when 'i agree to the terms' checkbox state changes
function theIAgreeCheckboxHasChanged() {
  var is_checked = window.document.agreement.i_agree_to_the_terms.checked;
  
  if (is_checked == true) {
    setCookie('i_agree_to_the_terms', 'true');
  } else {
    setCookie('i_agree_to_the_terms', 'false');
  }
}

// Get a cookie.
function getCookie(name){
  var cname = name + "=";               
  var dc = document.cookie;             
  if (dc.length > 0) {              
    begin = dc.indexOf(cname);       
    if (begin != -1) {           
      begin += cname.length;       
      end = dc.indexOf(";", begin);
      if (end == -1) end = dc.length;
        return unescape(dc.substring(begin, end));
    } 
  }
  return null;
}

// Set a cookie.
function setCookie(name, value, expires, path, domain, secure) {
  document.cookie = name + "=" + escape(value) + 
  ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
  ((path == null) ? "" : "; path=" + path) +
  ((domain == null) ? "" : "; domain=" + domain) +
  ((secure == null) ? "" : "; secure");
}

// Delete a cookie
function delCookie (name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain) +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}








	
