
// ----------------------------------------------------------------
// scripts/ValidateFormData.js
// Updated 1.5 2010-03-06
// Version 1.4 2010-01-06
// ----------------------------------------------------------------

/*
------------------------------------------------
Coookie Time Multipliers
------------------------------------------------
  * 1000                              // milliseconds
  * 1000 * 60                         // seconds
  * 1000 * 60 * 60                    // minutes
  * 1000 * 60 * 60 * 24               // hours
  * 1000 * 60 * 60 * 24 * 30          // days
  * 1000 * 60 * 60 * 24 * 30 * 12     // months
  * 1000 * 60 * 60 * 24 * 30 * 12 * 1 // years
------------------------------------------------
*/

var userRecord = '';
var userSecure = 0;
var userPathID = "/";
var userCookie = "USRSUB";
var userTelphn = "USRPHN";
var userFormID = "USRFRM";
var userAfflID = "USRAFF";
var cartFormID = "UCCOneForm";
var userTrmCnd = "riskFreeTerms";
var userDomain = ".ultimatecoloncleanse.com";
var userAgrees = "agreeToTerms";
var userDateID = new Date();
var userExpire = new Date(userDateID.getTime() + (1000 * 60 * 60 * 24 * 30) ); // 2592000000 30 days
var afflExpire = new Date(userDateID.getTime() + (1000 * 60 * 10) );           //     600000 10 minutes
var userPayPal = "payPalExpress";
var userGoogle = "googleExpress";
var userAmazon = "amazonExpress";

function getUrlPath(theUrl) {
    endStop = theUrl.lastIndexOf ('/', theUrl.length - 1);
    urlPath = theUrl.substring (0, endStop + 1);
    return (urlPath);
}

function getTheDate()
{
    var theDate = new Date();
    var theMnth = theDate.getMonth() + 1; // return month
    var theDay  = theDate.getDate();      // return day of month
    var theYear = theDate.getFullYear();  // return four digit year

    if (theMnth < 10) {
        theMnth = '0' + theMnth;
    }

    if (theDay < 10) {
        theDay = '0' + theDay;
    }

    return theYear + '-' + theMnth + '-' + theDay;
}

function getTheTime()
{
    var theDate = new Date();
    var theHour = theDate.getHours();    // return hours
    var theMins = theDate.getMinutes();  // return minutes
    var theSecs = theDate.getSeconds();  // return seconds

    return theHour + ':' + theMins + ':' + theSecs;
}

function recordThisOrder()
{
    var orderData = '';
    var orderMark = new Image();
    var orderForm = document.getElementById(cartFormID);

    var theList = '';
    var theDelm = '&';
    var theInit =  0;
    var theExit =  orderForm.length;
    var theDate = "?date=" + getTheDate();
    var theTime = "&time=" + getTheTime();
    var thePath = getUrlPath(window.location.href) + "sense/order.gif";

    for (theInit; theInit <  theExit; theInit++) {

        theList += theDelm + orderForm[theInit].name + '=' + orderForm[theInit].value;
    }

    orderMark.src = thePath + theDate + theTime + theList;
}

function routeToPayment(userPayment)
{
    var orderItem = '';
    var orderName = userPayment + "Pay";
    var orderData = eval(userPayment + "Key");
    var orderPost = eval(userPayment + "Sys");

    var PAPID = "pap_dx8vc2s5";
    var DATID = "custom_data_0002";

    document.getElementById(userPayment).value = true;

    if (document.getElementById("UCCProductID[0]").checked ) { orderItem = 3; }
    if (document.getElementById("UCCProductID[1]").checked ) { orderItem = 4; }
    if (document.getElementById("UCCProductID[2]").checked ) { orderItem = 5; }

    if (document.getElementById(orderName)) {
        document.getElementById(orderName).value = orderData[orderItem];
    }

    if (document.getElementById(PAPID)) {
        document.getElementById(DATID).value = document.getElementById(PAPID).value;
    }

    if (orderPost) {
        document.getElementById(cartFormID).action = orderPost;
    }

    updateThisOrder(orderItem);

    return checkUserAgreement();
}

function submitOrder()
{
    recordThisOrder();

    //document.getElementById(cartFormID).submit();
    document.UTMOneForm.submit();
}

function checkUserAgreement()
{
    if (document.getElementById(userAgrees)) {

        if(document.getElementById(userAgrees).checked) {

            submitOrder();

        } else {

            alert("You must agree to the Terms and Conditions of this FREE 30-DAY TRIAL OFFER in order for us to process your request.");

            document.getElementById(userAgrees).focus();

            return false;
        }
    }

    submitOrder();
}

function removeElement(elementID)
{
    var targetElement = document.getElementById(elementID);

    if ( targetElement.parentNode && targetElement.parentNode.removeChild ) {
        targetElement.parentNode.removeChild(targetElement);
    }
}

function checkUserLocation(formID)
{
    var subscribe = document.getElementById(formID);
    if(subscribe.user_name && subscribe.user_name.value.length == 0){
        alert('Please provide us your full name.');
        subscribe.user_first.focus();
        return false;
     }

    if(subscribe.user_first && subscribe.user_first.value.length == 0){
        alert('Please provide your first name. If entering a company name, you may enter the full company name in the field labeled First Name.');
        subscribe.user_first.focus();
        return false;
     }

    if(subscribe.user_first && subscribe.user_first.value.length == 0 && subscribe.user_last.value.length == 0) {
        alert('Please, you must provide us a name so we may process your request.');
        subscribe.user_last.focus();
        return false;
    }

    if(subscribe.user_address && subscribe.user_address.value.length == 0){
        alert('Please enter your street address, and suite or apartment number if appropriate, so we know where to ship your FREE TRIAL request.');
        subscribe.user_address.focus();
        return false;
    }

    if(subscribe.user_address1 && subscribe.user_address1.value.length == 0){
        alert('Please enter your street address.');
        subscribe.user_address1.focus();
        return false;
    }

    if(subscribe.user_address2 && subscribe.user_address1.value.length == 0 && subscribe.user_address2.value.length == 0) {
        alert('Please, you must provide us an address so we may process your request.');
        subscribe.user_address1.focus();
        return false;
    }

    if(subscribe.user_city && subscribe.user_city.value.length == 0){
        alert('Please enter your city.');
        subscribe.user_city.focus();
        return false;
    }

    if(subscribe.user_country && subscribe.user_country.value < 0){
        alert('Please select your state or province if US or CA, or country if UK.');
        subscribe.user_state.focus();
        return false;
    }

    if(subscribe.user_state && subscribe.user_country.value == 'US') {
        if(subscribe.user_state.value == -1){
            alert('Please select your state');
            subscribe.user_state.focus();
            return false;
        }
    }

    if(subscribe.user_state && subscribe.user_country.value == 'CA') {
        if(subscribe.user_state.value == -1){
            alert('Please select your province');
            subscribe.user_state.focus();
            return false;
        }
    }

    if(subscribe.user_zip && subscribe.user_zip.value.length == 0){
        alert('Please enter your zip/postal code.');
        subscribe.user_zip.focus();
        return false;
    }

    if(subscribe.user_phone && subscribe.user_phone.value.length == 0){
        alert('Please provide us your phone number for verification only.');
        subscribe.user_phone.focus();
        return false;
    }

    if(subscribe.user_email && subscribe.user_email.value.length == 0){
        alert('Please enter your email address so we may confirm your FREE TRIAL request.');
        subscribe.user_email.focus();
        return false;
     }

    // Check email validity
    if(subscribe.user_email && subscribe.user_email.value.search(/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i)){
        alert('Oops! The email address you entered is not valid. Please correct it.');
        subscribe.user_email.focus();
        return false;

    }

    // build a string with the user data and bind to cookie
    setUserRecord(formID);

    // send form data to server
    subscribe.submit();

}

// ----------------------------------------------------------------

var codeList = "10OFFONALL|10OFFOR|10OFFUCC|10OFFUT|15OFF|415|420|421|423|425|426|433|434|439|441|443|445|7DAYSPECIAL|ABS15|ATLAST40|BARTON|BARTON10|CLEAN7|FREETRIAL2|HEALTH|HNETWORK|KCC|KENNEDYCAMPBELL|LAP15|MARCJARJOUR|SAVE15|SPA15|SPECIAL15|THANKS|THANKS25|THANKS30|THANKS415|Thanks420|THANKS421|THANKS423|THANKS425|THANKS426|THANKS433|THANKS434|THANKS439|THANKS441|THANKS443|THANKS445|THANKS50|THANKYOU10|THANKYOU20|UCC15|UCC5|UCCTEN|UT7DAYDISC|UT7DAYSPEC|VIP7318|VIPTEN";

function checkCouponCode(formID)
{
    var orderForm = document.getElementById(formID);

    if (orderForm.coupon) {

        if (orderForm.coupon.value.length != 0) {
            if (codeList.indexOf(orderForm.coupon.value) == -1) {
                document.getElementById("badCode").innerHTML = "Oops! We can&#39;t find the code you entered. Check the spelling and try again, or leave the field blank.";
                orderForm.coupon.focus();
                return false;
            } else {
                document.getElementById("badCode").innerHTML = '';
                appendCouponReminder();
            }
        } else {
            removeElement("coupon");
        }
    }
}

function checkCompletedForm(formID)
{
    var orderForm = '';

    if (typeof(formID) == 'object') {
        orderForm = formID;
    } else {
        orderForm = document.getElementById(formID);
    }

    if (document.getElementById("UCCProductID[9]") && document.getElementById("UCCProductID[9]").checked) {
        alert('Please select a Product option.');
        document.getElementById("UCCProductID[2]").focus();
        return false;
    }

    if (orderForm.creditCardType && orderForm.creditCardType.selectedIndex < 1) {
        alert('Please select your Credit Card Type.');
        orderForm.creditCardType.focus();
        return false;
    }

    if (orderForm.creditCardNumber && orderForm.creditCardNumber.value.length == 0) {
        alert('Please provide your Credit Card Number.');
        orderForm.creditCardNumber.focus();
        return false;
    }

    if (orderForm.creditCardExpMonth && orderForm.creditCardExpMonth.selectedIndex < 1) {
        alert('Please provide your Credit Card Expiration Month.');
        orderForm.creditCardExpMonth.focus();
        return false;
    }

    if (orderForm.creditCardExpYear && orderForm.creditCardExpYear.selectedIndex < 1) {
        alert('Please provide your Credit Card Expiration Year.');
        orderForm.creditCardExpYear.focus();
        return false;
    }

    if (orderForm.creditCardCVV2 && orderForm.creditCardCVV2.value.length == 0) {
        alert('Please provide your Credit Card Verification Number.');
        orderForm.creditCardCVV2.focus();
        return false;
    }

    if (orderForm.shippingFirstName && orderForm.shippingFirstName.value.length == 0) {
        alert('Please provide your Shipping First Name.');
        orderForm.shippingFirstName.focus();
        return false;
    }

    if (orderForm.shippingLastName && orderForm.shippingLastName.value.length == 0) {
        alert('Please provide your Shipping Last Name.');
        orderForm.shippingLastName.focus();
        return false;
    }

    if (orderForm.shippingAddress1 && orderForm.shippingAddress1.value.length == 0) {
        alert('Please provide your Shipping Address.');
        orderForm.shippingAddress1.focus();
        return false;
    }

    if (orderForm.shippingCity && orderForm.shippingCity.value.length == 0) {
        alert('Please provide your Shipping City.');
        orderForm.shippingCity.focus();
        return false;
    }

    if (orderForm.shippingState && orderForm.shippingState.value < 0) {

        if (orderForm.shippingCountry && orderForm.shippingCountry.value == "United Kingdom") {
            orderForm.shippingState.value = ' ';
        } else {
            alert('Please provide your Shipping State.');
            orderForm.shippingState.focus();
            return false;
        }
    }

    if (orderForm.shippingPostalCode && orderForm.shippingPostalCode.value.length == 0) {
        alert('Please provide your Shipping Zip or Postal Code.');
        orderForm.shippingPostalCode.focus();
        return false;
    }

    if (orderForm.shippingCountry && orderForm.shippingCountry.value < 0) {
        alert('Please provide your Shipping Country.');
        orderForm.shippingCountry.focus();
        return false;
    }

    if (orderForm.shippingDayPhone && orderForm.shippingDayPhone.value.length == 0) {
        alert('Please provide your Shipping Day Phone.');
        orderForm.shippingDayPhone.focus();
        return false;
    }

    if (orderForm.billingEmail && orderForm.billingEmail.length == 0) {
        alert('Please provide your Shipping Email Address.');
        orderForm.billingEmail.focus();
        return false;
    }

    if (orderForm.billingSameAsShipping && orderForm.billingSameAsShipping.checked) {

        if (orderForm.billingFirstName && orderForm.billingFirstName.value.length == 0) {
            alert('Please provide your Billing First Name.');
            orderForm.billingFirstName.focus();
            return false;
        }

        if (orderForm.billingLastName && orderForm.billingLastName.value.length == 0) {
            alert('Please provide your Billing Last Name.');
            orderForm.billingLastName.focus();
            return false;
        }

        if (orderForm.billingAddress1 && orderForm.billingAddress1.value.length == 0) {
            alert('Please provide your Billing Address.');
            orderForm.billingAddress1.focus();
            return false;
        }

        if (orderForm.billingCity && orderForm.billingCity.value.length == 0) {
            alert('Please provide your Billing City.');
            orderForm.billingCity.focus();
            return false;
        }

        if (orderForm.billingState && orderForm.billingState.value < 0 && orderForm.billingCountry.value != "United Kingdom") {
            alert('Please provide your Billing State.');
            orderForm.billingState.focus();
            return false;
        }

        if (orderForm.billingState && orderForm.billingState.value < 0) {

            if (orderForm.billingCountry && orderForm.billingCountry.value == "United Kingdom") {
                orderForm.billingState.value = ' ';
            } else {
                alert('Please provide your Billing State.');
                orderForm.billingState.focus();
                return false;
            }
        }

        if (orderForm.billingPostalCode && orderForm.billingPostalCode.value.length == 0) {
            alert('Please provide your Billing Postal Code.');
            orderForm.billingPostalCode.focus();
            return false;
        }

        if (orderForm.billingCountry && orderForm.billingCountry.value < 0) {
            alert('Please provide your Billing Country.');
            orderForm.billingCountry.focus();
            return false;
        }

        if (orderForm.billingDayPhone && orderForm.billingDayPhone.value.length == 0) {
            alert('Please provide your Billing Day Phone.');
            orderForm.billingDayPhone.focus();
            return false;
        }

        if (orderForm.billingEmail && orderForm.billingEmail.value.length == 0) {
            alert('Please provide your Billing Email.');
            orderForm.billingEmail.focus();
            return false;
        }
    }

    checkCouponCode(formID);

    if (orderForm.userGoal && orderForm.userGoal.value < 0) {
        alert('Please tell us your goal for selecting this Program.');
        orderForm.userGoal.focus();
        return false;
    }

    if (orderForm.userAge && orderForm.userAge.value < 0) {
        alert('Please select your Age Range.');
        orderForm.userAge.focus();
        return false;
    }

    if (orderForm.userGender && orderForm.userGender.value < 0) {
        alert('Please select your Gender.');
        orderForm.userGender.focus();
        return false;
    }

    document.getElementById(userPayPal).value = false;

    return checkUserAgreement();

}

// ----------------------------------------------------------------

function setUserRecord(formID)
{
    var subscribe = document.getElementById(formID);

    // assign user email
    var userEmail = '' + subscribe.user_email.value;
    setCookie(userCookie, userEmail, userExpire, userPathID, userDomain, userSecure);

    // assign user phone
    var userPhone = '' + subscribe.user_phone.value;
    setCookie(userTelphn, userPhone, userExpire, userPathID, userDomain, userSecure);

    // assign user form
    var userForm = '' + subscribe.user_other01.value;
    setCookie(userFormID, userForm,  userExpire, userPathID, userDomain, userSecure);

    // assign user referring Affiliate ID
    var userAffl = '' + getAffiliateID();
    setCookie(userAfflID, userAffl,  afflExpire, userPathID, userDomain, userSecure);

    return true;
}



/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
  document.cookie= name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path)    ? "; path="    + path                  : "") +
    ((domain)  ? "; domain="  + domain                : "") +
    ((secure)  ? "; secure"                           : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else {
    begin += 2;
  }
  var end = document.cookie.indexOf(";", begin);
  if (end == -1) {
    end = dc.length;
  }
  return unescape(decodeURI(dc.substring(begin + prefix.length, end))).split("+").join(" ");
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function hideDiv(blockID)
{
    document.getElementById(blockID).style.visibility = "hidden";
    document.getElementById(blockID).style.display    = "none";
}

function showDiv(blockID)
{
    document.getElementById(blockID).style.visibility = "visible";
    document.getElementById(blockID).style.display    = "block";
}

function showHide(checkbox, blockID, labelID)
{
    if (checkbox.checked) {
        showDiv(labelID);
        showDiv(blockID);
        checkbox.value = "false";
        document.getElementById("billToIsShipTo").value = false;
    } else {
        hideDiv(labelID);
        hideDiv(blockID);
        checkbox.value = "true";
        document.getElementById("billToIsShipTo").value = true;
    }
}


/* -- END -- */
