function clearDependencies(e, detype) {
	var f = $('theForm');
	if (e.type.indexOf('select') == 0) {
		if (e.selectedIndex == 0) {
			var e2 = f[e.name + ' Value'];
			if (detype == 'select') {
				e2.selectedIndex = 0;
			}
			else {
				e2.value = e2.getAttribute('defaultValue') || "";
			}
		}
	}
	else if (!e.checked) {
		var e2 = f[e.value + ' Value'];
		if (detype == 'select') {
			e2.selectedIndex = 0;
		}
		else {
			e2.value = e2.getAttribute('defaultValue') || "";
		}
	}
}
function iconClearDependencies(e) {
	var f = $('theForm');
	var radios = f['Icon'];
	for (var i=0; i<radios.length; i++) {
		var r = radios[i];
		if (!r.checked) {
			var d;
			if (d = f[r.value + ' Value']) {
				if (d.type.indexOf('select') == 0) {
					d.selectedIndex = 0;
				}
				else {
					d.value = "";
				}
			}
			if (d = f[r.value + ' Type']) {
				if (d.type.indexOf('select') == 0) {
					d.selectedIndex = 0;
				}
				else {
					d.value = "";
				}
			}
		}
	}
}
function checkform() {
	var f = $('theForm');
	
	var r = $('required').value.split(/,\s*/);

	var missing = "";
	var dv;
	for (var i=0; i<r.length; i++) {
		if (!f.elements[r[i]].value) missing += '  - '+r[i]+'\n';
	}
	if (missing) {
		alert('The following fields are required:\n\n'+missing);
		return false;
	}
	
	// edit ccno
	
	// edit email

	return !missing;
}
function elFocus(el) {
	if (!el) return;
	
	var d = el.getAttribute('defaultValue');
	
	if (d && el.value == d) el.value = "";
}
function elBlur(el) {
	if (!el) return;

	var d = el.getAttribute('defaultValue');
	
	if (!el.value && d) el.value = d;
}

var putFormHere = 'orderformContainer';

function getForm(s)
{
	// s is a select element
	if (!s) return;
	if (!s.selectedIndex) {
		$(putFormHere).innerHTML = '';
		return;
	}
    if (ajaxSupported()) {
      $(putFormHere).innerHTML = '<div style="font-style:italic;margin-top:10px;">Loading form...</div>';
      url = '/orders-' + s.options[s.selectedIndex].value.replace("/","_") + '.html';
      loadDoc(url);
      return false;
    }
    else {
      return true;
    }
}

function processReqChange() 
{
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            $(putFormHere).innerHTML = req.responseText;
            useCookieOnLoad();
        } else {
            $(putFormHere).innerHTML = '';
            alert("There was a problem retrieving the data: " + req.status);
        }
    }
}

function useCookieOnLoad() {
	var fe = $('theForm').elements;
	for (i=0; i<fe.length; i++) {
		var e = fe[i];
		if (e.name == 'required') continue;
		
		if (e.type == 'radio' || e.type == 'checkbox') {
			if(co[e.name]) {
				coe = co[e.name].split('^');
				for (o=0; o<coe.length; o++) {
					if (coe[o] == e.value) {
						e.checked = true;
						break;
					}
				}
			}
		}
		else if (e.type.match(/select/i)) {
			val = e.options[e.selectedIndex].value;
			if (val.match(/^\d+$/) && parseInt(val) == 0) val = "";
			for (o=0; o<e.options.length; o++) {
				if (co[e.name] == e.options[o].value) {
					e.options[o].selected = true;
					break;
				}
			}
		}
		else if (co[e.name] == undefined) {
		}
		else {
			e.value = co[e.name];
		}
	}
}

function adjustPieces(pcs) {
	// pcs is a select element
	if (!pcs) return;
	
	// decide which Pieces to show
	var pieces2Array = new Array('Gingham', 'Petite', 'Patchwork');
	var showPieces2 = false;
	for (var i=0; i< pieces2Array.length; i++) {
		if (pcs.options[pcs.selectedIndex].value.indexOf(pieces2Array[i]) != -1) {
			showPieces2 = true;
			break;
		}
	}
	if (showPieces2) {
		$('piecesTable1').style.display = 'none';
		$('piecesTable2').style.display = 'block';
	}
	else {
		$('piecesTable1').style.display = 'block';
		$('piecesTable2').style.display = 'none';
		if ($('verticalCardsSelect')) {
			// decide whether to show vertical cards
			var verticalCardsArray = new Array('Avery', 'Ella', 'Jackie', 'Jane');
			var showVerticalCards = false;
			for (var i=0; i< verticalCardsArray.length; i++) {
				if (pcs.options[pcs.selectedIndex].value.indexOf(verticalCardsArray[i]) != -1) {
					showVerticalCards = true;
					break;
				}
			}
			if (showVerticalCards) {
				$('verticalCardsSelect').style.display = 'block';
				$('verticalCardsLabel').style.display = 'block';
			}
			else {
				$('verticalCardsSelect').style.display = 'none';
				$('verticalCardsLabel').style.display = 'none';
			}
		}
	}
	adjustQtys(pcs);
}
function adjustQtys(pcs) {
	var s = document.getElementsByTagName('SELECT');
	
	// if 'Jane', remove lower two non-zero quantity options from all 'pieces' selects, unless they're already removed.
	if (pcs.options[pcs.selectedIndex].value == 'Jane') {
		for (i=0; i<s.length; i++) {
			if (s[i].className == 'pieces' && s[i].options[1].value == 50) {
				s[i].options[1] = s[i].options[2] = null;
			}
		}
	}
	// not 'Jane'; add 50 and 75 quantity options to all 'pieces' selects, unless they're already there.
	else {
		for (i=0; i<s.length; i++) {
			if (s[i].className == 'pieces' && s[i].options[1].value != 50) {
				var sl = s[i].options.length;
				s[i].options.length += 2;
				for (j=(sl-1); j>=0; j--) {
					s[i].options[j+2].value = s[i].options[j].value;
					s[i].options[j+2].text  = s[i].options[j].text;
				}
				s[i].options[1].value = s[i].options[1].text = 50;
				s[i].options[2].value = s[i].options[2].text = 75;
			}
		}
	}
}

function adjustBlackInkColor(pcs, ics) {
	// pcs is a select element
	if (!pcs) return;
	// ics is a select element
	if (!ics) return;
	
	var blackColorOptionIndex = 1;
	if (pcs.options[pcs.selectedIndex].value.match(/Brigitte/i)) {
		// add Black color unless already there
		if (!ics.options[blackColorOptionIndex].value.match(/^black$/i)) {
			var o = new Option();
			o.value = 'Black';
			o.text  = 'Black';
			if (isIE) {
				ics.add(o, blackColorOptionIndex);
			}
			else {
				ics.add(o, ics.options[blackColorOptionIndex]);
			}
		}
	}
	else {
		// remove Black color if there
		if (ics.options[blackColorOptionIndex].value.match(/^black$/i)) {
			ics.remove(blackColorOptionIndex);
		}
	}
}

function adjustBorders(pcs, bs, bsc) {
	// pcs is a select element
	if (!pcs) return;
	// bs is a select element
	if (!bs) return;
	// bsc is a div element
	if (!bsc) return;
	
	// decide whether to show the Borders dropdown at all
	var noBordersArray = new Array('Brigitte', 'Coco', 'Diane', 'Ingrid', 'Jackie', 'Kelly', 'Gingham', 'Patchwork', 'Petite Prints');
	var noBorders = false;
	for (var i=0; i< noBordersArray.length; i++) {
		if (pcs.options[pcs.selectedIndex].value.indexOf(noBordersArray[i]) != -1) {
			noBorders = true;
			break;
		}
	}
	if (noBorders) {
		bs.selectedIndex = 0;
		bsc.style.display = 'none';
	}
	else {
		bsc.style.display = 'block';
		
		// decide whether to include Floral Overprint in Borders options
		var showFloralOverprintArray = new Array('Audrey', 'Avery', 'Ella', 'Grace', 'Jane');
		var showFloralOverprint = false;
		for (var i=0; i< showFloralOverprintArray.length; i++) {
			if (pcs.options[pcs.selectedIndex].value.indexOf(showFloralOverprintArray[i]) != -1) {
				showFloralOverprint = true;
				break;
			}
		}
		var floralOverprintOptionIndex = 7;
		if (showFloralOverprint) {
			// add Floral Overprint unless already there
			if (!bs.options[floralOverprintOptionIndex].value.match(/floral/i)) {
				var o = new Option();
				o.value = 'Floral Overprint';
				o.text  = 'Floral Overprint';
				if (isIE) {
					bs.add(o, floralOverprintOptionIndex);
				}
				else {
					bs.add(o, bs.options[floralOverprintOptionIndex]);
				}
			}
		}
		else {
			// remove Floral Overprint if there
			if (bs.options[floralOverprintOptionIndex].value.match(/floral/i)) {
				bs.remove(floralOverprintOptionIndex);
			}
		}
	}
}

function adjustEnvelopes(pcs, es, esu, esus, eu4bar) {
	// pcs is a select element
	if (!pcs) return;
	// es is a select element
	if (!es) return;
	
// FOR COMPATIBILITY WITH IE, TRY USING OPTGROUP INSTEAD
	if (pcs.options[pcs.selectedIndex].value.match(/Brigitte/i)) {
		// add Brigitte envelope options unless already there
		if (!es.options[es.options.length-1].value.match(/Brigitte/i)) {
			var bOpts = new Array('Solid - Brigitte Blanc', 'Solid - Brigitte Glace', 'Solid - Brigitte Rosé');
			for (var i=0; i<bOpts.length; i++) {
				var o = new Option();
				o.value = bOpts[i];
				o.text  = bOpts[i];
				if (isIE) {
					es.add(o, es.options.length);
				}
				else {
					es.add(o, null);
				}
			}
		}
		// hide square envelope upgrade checkbox & dropdown,
		// and reword remaining envelope upgrade checkbox text
		if (esu) {
			esu.style.display  = 'none';
			esus.selectedIndex = 0;
		}
		if (eu4bar) eu4bar.style.display = 'none';
	}
	else {
		// remove Brigitte envelope options if there
		if (es.options[es.options.length-1].value.match(/Brigitte/i)) {
			if (es.selectedIndex >= es.options.length-3) es.selectedIndex = 0;
			es.options.length = es.options.length - 3;
		}
		// show additional envelope upgrade dropdown as necessary
		var squareEnvelopeUpgradeArray = new Array('Gingham', 'Petite', 'Patchwork');
		var squareEnvelopeUpgrade = false;
		for (var i=0; i< squareEnvelopeUpgradeArray.length; i++) {
			if (pcs.options[pcs.selectedIndex].value.indexOf(squareEnvelopeUpgradeArray[i]) != -1) {
				squareEnvelopeUpgrade = true;
				break;
			}
		}
		if (squareEnvelopeUpgrade) {
			if (esu) {
				esu.style.display = 'block';
			}
			if (eu4bar) eu4bar.style.display = 'inline';
		}
		else {
			if (esu) {
				esu.style.display  = 'none';
				esus.selectedIndex = 0;
			}
			if (eu4bar) eu4bar.style.display = 'none';
		}
	}
}

function adjustSashes(pcs, ss, ssc) {
	// pcs is a select element
	if (!pcs) return;
	// ss is a select element
	if (!ss) return;
	// ssc is a div element
	if (!ssc) return;
	
	// decide whether to show the Sashes dropdown at all
	var noSashesArray = new Array('Gingham', 'Petite', 'Patchwork');
	var noSashes = false;
	for (var i=0; i< noSashesArray.length; i++) {
		if (pcs.options[pcs.selectedIndex].value.indexOf(noSashesArray[i]) != -1) {
			noSashes = true;
			break;
		}
	}
	if (noSashes) {
		ss.selectedIndex = 0;
		ssc.style.display = 'none';
	}
	else {
		ssc.style.display = 'block';
	}
}

function getOrderCookie() {
	ck = document.cookie.split('=');
	return unescape(ck[1]);
}

var co = new Object();

function useOrderCookie() {
      if (cookie = getOrderCookie()) {
	      kvs = cookie.split('&');
	      for (i=0; i<kvs.length; i++) {
	      	kv = kvs[i].split(':');
	      	if (co[kv[0]] == undefined) {
	      		co[kv[0]] = "";
	      	}
	      	else {
	      		co[kv[0]] += '^';
	      	}
	      	co[kv[0]] += kv[1];
	      }
	      if (co['occasionIndex']) {
	      	$('occasion').selectedIndex = co['occasionIndex'];
	      	getForm($('occasion'));
	      	$('occasion').blur();
	      }
      }
}

function setOrderCookie() {
	var cookie = "";
	var fe = $('theForm').elements;
	for (i=0; i<fe.length; i++) {
		var e = fe[i];
		var name = e.name;
		if (name == 'required') continue;
		var val = "";
		if (e.type == 'radio' || e.type == 'checkbox') {
			if (e.checked) val = e.value;
		}
		else if (e.type.match(/select/i)) {
			val = e.options[e.selectedIndex].value;
			if (val.match(/^\d+$/) && parseInt(val) == 0) val = "";
			if (name == 'occasion') {
				name = 'occasionIndex';
				val = e.selectedIndex;
			}
		}
		else if (e.type == 'textarea') {
			if (e.value != e.getAttribute('defaultvalue')) val = e.value;
		}
		else {
			val = e.value;
		}
		if (val) {
			if (cookie) cookie += '&';
			cookie += name + ':' + escape(val);
		}
	}
	document.cookie = 'order=' + cookie;
	//alert(document.cookie);
}

function clearOrderCookie() {
	var today = new Date();
	today.setTime( today.getTime() );
	expires = -(1000 * 60 * 60 * 24);
	var expires_date = new Date( today.getTime() + expires );
	
	//document.cookie += ";expires=" + expires_date.toGMTString();
}


function copyAddress() {
	var els = $('theForm').elements;
	
	var frfields = new Array('Address', 'City', 'State', 'Zip Code');
	
	for (var f=0; f<frfields.length; f++) {
		els['Billing '+frfields[f]].value = els[frfields[f]].value;
	}
}



