/*
 * $Author: dbibbens $
 * $Revision: 1.2 $
 * $Date: 2009/02/12 19:45:21 $ 
 * $Source: /home/cvs/blackstonepillow.com/local/etc/functions.js,v $
 */

var pop = true;

function land(uid)
{
    var aid = value('aid');
    var subid = value('sub-id');
    var c_aid = get_cookie('aid');
    
    if(subid && subid != '') {
	aid = aid + "-" + subid;
    }
    
    if(c_aid && c_aid != 'undefined') {
	aid = c_aid;
    }
    
    document.cookie = "aid=" + aid + "; path=/";
    document.cookie = "landed=" + uid + "; path=/";
}

function thankyou(uid) {
    document.cookie = "thankyou=" + uid + "; path=/";
}

function show_ssl_info(referrer)
{
    var url = "https://smarticon.geotrust.com/smarticonprofile?Referer=" + referrer;
    var features = "status=1,location=0,scrollbars=1,resizeable=yes,width=400,height=600";
    var w = window.open(url, 'ssl_info', features);
    
    w.focus();
}

function openWin(url, width, height) 
{
    var features = "toolbar=yes,location=yes,directories=no,status=yes," + 
	"menubar=yes,marginwidth=0,marginheight=0,resizable=yes,scrollbars=" +
	"yes,width=" + width + ",height=" + height;
    
    if(pop) {  
	popup_win(url, 'results', features);
    }
}

function openBareWin(url, width, height) 
{
    var features = "toolbar=no,location=no,directories=no,status=no," + 
	"menubar=no,marginwidth=0,marginheight=0,resizable=yes,scrollbars=" +
	"yes,width=" + width + ",height=" + height;
    
    if(pop) {  
	popup_win(url, 'results', features);
    }
}

function popup_win(url, name, features) 
{
    window.open(url, name, 'screenX=0,screenY=0,left=0top=0,' + features);
}

function validate() 
{
    var errors = 0;
    
    var upsell;
    var quantity = document.getElementsByName('quantity');
    var email = document.getElementById('email');
    var telephone = document.getElementById('telephone');
    var ccnumber = document.getElementById('ccnumber');
    var ccmonth = document.getElementById('ccmonth');
    var ccyear = document.getElementById('ccyear');
    var item_sum = quantity[0].selectedIndex;
    var msg = "You are about to order " + item_sum + " items. Click OK to continue or Cancel to cancel.";
    
    if(item_sum > 1) {
	if(!confirm(msg)) {
	    return false;
	}
    }
    
    if(quantity[0].selectedIndex == 0) {
	alert('Please select at least 1 pillow.');
	
	return false;
    }
    
    if(email) {
	errors = errors + validate_email(email);
	if(errors > 0) { return false; }
    }
    
    if(telephone) {
	errors = errors + validate_telephone(telephone);
	if(errors > 0) { return false; }
    }
    
    if(ccnumber) {
	errors = errors + validate_ccnumber(ccnumber);
	if(errors > 0) { return false; }
    }
    
    if(ccmonth) {
	errors = errors + validate_ccmonth(ccmonth);
	if(errors > 0) { return false; }
    }
    
    if(ccyear) {
	errors = errors + validate_ccyear(ccyear);
	if(errors > 0) { return false; }
    }
    
    if(errors == 0) {
	return true;
    } else {
	return false;
    }
}

function validate_ccnumber(ccnumber)
{
    var msg;
    
    ccnumber.value = ccnumber.value.replace(/\D/g, '');
    if(ccnumber.value.length < 13 ||
       isNaN(ccnumber.value)) {
	msg = 'Please provide a valid credit card number.';    
	alert(msg);
	
	return 1;
    }
    
    return 0;
}

function validate_ccmonth(ccmonth)
{
    var msg;
    
    if(ccmonth.selectedIndex == 0) {
	msg = 'Please select your credit card expiration month.';    
	alert(msg);
	
	return 1;
    }
    
    return 0;
}

function validate_ccyear(ccyear)
{
    var msg;
    
    if(ccyear.selectedIndex == 0) {
	msg = 'Please select your credit card expiration year.';    
	alert(msg);
	
	return 1;
    }
    
    return 0;
}

function validate_email(email)
{
    if(email.value.length == 0 ||
       email.value.indexOf('@') == -1 || 
       email.value.indexOf('.') == -1) {
	alert('Please provide your valid email address.');
	
	return 1;
    }
    
    return 0;
}

function validate_telephone(telephone)
{
    var msg;
    
    telephone.value = telephone.value.replace(/\D/g, '');
    
    if(telephone.value.length < 10 ||
       isNaN(telephone.value)) {
	msg = 'Please provide a valid Telephone number.';
	alert(msg);
	telephone.value = "";
	
	return 1;
    }
    
    return 0;
}

function set_order_id(order_id) 
{
    if(!get_cookie('order-id')) {
	document.cookie = "order-id" + "=" + order_id + "; path=/";
    }
}

function set_aid(aid) 
{
    document.cookie = "aid" + "=" + aid + "; path=/";
}

function get_cookie(name)
{
    var cookies = document.cookie.split(';');
    name = name + "=";
    for(var i = 0; i < cookies.length; i++) {
	var cookie = cookies[i];
	
	while(cookie.charAt(0) == ' ') {
	    cookie = cookie.substring(1, cookie.length);
	}
	
	if(cookie.indexOf(name) == 0) {
	    return cookie.substring(name.length, cookie.length);
	}
    }
    
    return false;
}

function value(key)
{
    keys = new Array();
    values = new Array();
    
    var query = window.location.search.substring(1);
    var pairs = query.split("&");
    
    for (var i=0; i < pairs.length; i++) {
	var pos = pairs[i].indexOf('=');
	if (pos >= 0) {
	    var argname = pairs[i].substring(0, pos);
	    var value = pairs[i].substring(pos + 1);
	    keys[keys.length] = argname;
	    values[values.length] = value;
	}
    }
    
    for (var i=0; i < keys.length; i++) {
	if (keys[i] == key) {
	    return values[i];
	}
    }
}

function delete_cookie(cookie_name)
{
    var cookie_date = new Date ();
    cookie_date.setTime(cookie_date.getTime() - 1);
    document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

