$(function(){	
	var content = $('#content');	
	$('.form-button', content)
	.mouseover(function() {
		$(this).attr('class', 'form-button-over');
	})
	.mouseout(function() {
		$(this).attr('class', 'form-button');
	});
	
	$('.datepicker').datepicker({
		numberOfMonths: 2,
		showButtonPanel: true,
		dateFormat: 'mm/dd/y'
	});
	
	$('a', content)
	.click(function(e) {
		var link_href = $(this).attr('href');
		if (link_href.indexOf('rose-panel2.html') != -1) {
			e.preventDefault();
			openIframeDialog('dialog-roses', 580, 500, '/rose-panel2.html');
		} else if (link_href.indexOf('view_upgrades.php') != -1) {
			e.preventDefault();
			openIframeDialog('dialog-upgrades', 400, 200, link_href);
		}
	});
});

function openBox(qs, tb_width, tb_height) {
	if (tb_width == undefined) {
		var win_width = $(window).width();
		tb_width = win_width * 0.9;	
	}
	if (tb_height == undefined) {
		var win_height = $(window).height();
		tb_height = win_height * 0.8;		
	}
	var tb_url = 'index_tb.php?' + qs + '&placeValuesBeforeTB_=savedValues&TB_iframe=true&height=' + tb_height + '&width=' + tb_width;
	tb_show('', tb_url, '');
}

function viewAddons(cat_id, addon_type, category, selections) {
	jQuery('<div/>', {
		id: 'catid' + cat_id,
		title: category
	}).appendTo('body');
	
	var selected_addons = $('#selected_addons').val();	
	var iframe_id = 'catid' + cat_id;
	var iframe_src = 'view_addons.php?id=' + cat_id + '&type=' + addon_type + '&selected_addons=' + selected_addons;
	openIframeDialog(iframe_id, 580, 500, iframe_src);
}

function setSelectedAddons(selected_addons) {
	$('#selected_addons').val(selected_addons);	
	setAddonsHTML(selected_addons);
}

function removeAddon(products_id) {
	var selected_addons = $('#selected_addons').val();
	var selected_addons_array = selected_addons.split(',');
	var selected_addons_updated = '';
	$('#selected_addons').val();
	
	jQuery.each(selected_addons_array, function() {
		if (this != products_id && this != '') {
			selected_addons_updated = selected_addons_updated + ',' + this;
			$('#selected_addons').val(selected_addons_updated);
		}
	});
	
	//will add or remove html from appropriate category add-on div and update overall price
	setAddonsHTML(selected_addons_updated);
}

function setAddonsHTML(selected_addons) {
	var url = '/shop/skshop_phpcode/addons_code.php?ajax=1&seladdons=' + selected_addons;
	$.get(url, function(data) {
		var data_array = data.split('<!--DIVIDER-->');
		var addons_price = $('#addons_price').val();
		var addons_price_updated = data_array[0];
		var addons_html = data_array[1];
		var addons_change = (addons_price_updated * 1) - (addons_price * 1);
		var addons_price_html = '$' + addons_price_updated.toString();		
		$('#addons_price').val(addons_price_updated);
		$('#addons-price').html(addons_price_html);
		
		var sub_total = $('#sub_total').val();
		var sub_total_updated = (sub_total * 1) + addons_change;
		updateTotals(sub_total_updated);
		
		$('#cat-addons-selected').html(addons_html);
	});	
}

function setUpgradePrice(upgrade_price_updated) {
	var upgrade_price = $('#upgrade_price').val();
	var upgrade_change = (upgrade_price_updated * 1) - (upgrade_price * 1);
	var upgrade_price_html = '$' + upgrade_price_updated.toString();		
	$('#upgrade_price').val(upgrade_price_updated);
	$('#upgrade-price').html(upgrade_price_html);	
	
	var sub_total = $('#sub_total').val();
	var sub_total_updated = (sub_total * 1) + upgrade_change;
	updateTotals(sub_total_updated);
}

function setProductPrice() {
	var product_option = $('#option_option_value_id').children(':selected').attr('id');
	var product_price = $('#product_price').val();
	
	var option_array = product_option.split('|');
	var product_price_updated = option_array[1];	
	var product_change = (product_price_updated * 1) - (product_price * 1);
	var product_price_html = '$' + product_price_updated.toString();		
	$('#product_price').val(product_price_updated);
	$('#product-price').html(product_price_html);	
	
	var sub_total = $('#sub_total').val();
	var sub_total_updated = (sub_total * 1) + product_change;
	updateTotals(sub_total_updated);
}

function setDelivery(delivery_type, default_price) {
	$('#recipient-fields').removeClass();
	$('#pickup_disclaimer').removeClass();
	$('#delivery_disclaimer').removeClass();
	
	if (delivery_type == 2) {
		$('#recipient-fields').addClass('hidden');
		$('#pickup_disclaimer').addClass('visible');
		$('#delivery_disclaimer').addClass('hidden');
		var disclaimer = $('#pickup_disclaimer').html();
		default_price = 0;
	} else {
		$('#recipient-fields').addClass('visible');	
		$('#pickup_disclaimer').addClass('hidden');
		$('#delivery_disclaimer').addClass('visible');
		var disclaimer = $('#delivery_disclaimer').html();
	}	
	$('#seldisclaimer').val(disclaimer);
	
	//update totals
	var delivery_price = $('#delivery_price').val();
	var delivery_change = (default_price * 1) - (delivery_price * 1);
	default_price = default_price.toFixed(2);
	var delivery_price_html = '$' + default_price.toString();		
	$('#delivery_price').val(default_price);
	$('#delivery-price').html(delivery_price_html);	
	
	var sub_total = $('#sub_total').val();
	var sub_total_updated = (sub_total * 1) + delivery_change;
	updateTotals(sub_total_updated);
}

function setRecipient(recipient_id) {
	if (recipient_id > 0) {
		var url = '/shop/skshop_phpcode/productdetail_code.php?ajax=1&recipient_id=' + recipient_id;
		$.get(url, function(data) {		
			//alert(data);
			var stringArray = data.split('|');
			$('#s_firstname').val(stringArray[0]);
			$('#s_lastname').val(stringArray[1]);
			$('#s_phone1').val(stringArray[2].substr(0,3));
			$('#s_phone2').val(stringArray[2].substr(3,3));
			$('#s_phone3').val(stringArray[2].substr(6,4));
			$('#s_ophone1').val(stringArray[3].substr(0,3));
			$('#s_ophone2').val(stringArray[3].substr(3,3));
			$('#s_ophone3').val(stringArray[3].substr(6,4));
			$('#s_address1').val(stringArray[4]);
			$('#s_address2').val(stringArray[5]);
			$('#s_city').val(stringArray[6]);
			$('#s_state').val(stringArray[7]);
			$('#s_zip').val(stringArray[8]);
			$('#s_company').val(stringArray[9]);
		});
	} else {
		$('#s_firstname').val('');
		$('#s_lastname').val('');
		$('#s_phone1').val('');
		$('#s_phone2').val('');
		$('#s_phone3').val('');
		$('#s_ophone1').val('');
		$('#s_ophone2').val('');
		$('#s_ophone3').val('');
		$('#s_company').val('');
		$('#s_address1').val('');
		$('#s_address2').val('');
		$('#s_city').val('');
		$('#s_state').val('');
		$('#s_zip').val('');
		$('#s_company').val('');	
	}	
}

function setCreditCard(cc_id) {
	if (cc_id > 0) {
		var url = '/shop/skshop_phpcode/productdetail_code.php?ajax=1&cc_id=' + cc_id;
		$.get(url, function(data) {		
			var stringArray = data.split('|');
			$('#cc_num').val(stringArray[0]);
			$('#cc_num').attr('disabled', 'disabled');
			$('#cc_exp_date1').val(stringArray[1]);
			$('#cc_exp_date2').val(stringArray[2]);
		});
	} else {
		$('#cc_num').val('');
		$('#cc_num').attr('disabled', '');
		$('#cc_exp_date1').val('');
		$('#cc_exp_date2').val('');
	}	
}

function setPaymentMethod() {
	var payment_method = $('input:radio[name=payment_method]:checked').val();
	$('#cc-fields').removeClass();
	$('#acct-fields').removeClass();	
	
	if (payment_method == 'cc') {		
		$('#cc-fields').addClass('visible');
		$('#acct-fields').addClass('hidden');
	} else {	
		$('#cc-fields').addClass('hidden');
		$('#acct-fields').addClass('visible');					
	}
}

function updateTotals(sub_total_updated) {
	sub_total_updated = sub_total_updated.toFixed(2);
	var sub_total_html = '$' + sub_total_updated.toString();		
	$('#sub_total').val(sub_total_updated);
	$('#sub-total').html(sub_total_html);
	
	var tax_total_updated = 0;
	var charge_tax = $('#charge_tax').val();
	if (charge_tax == '1') {
		var tax_total_updated = ((sub_total_updated * 1) * 0.07);
		tax_total_updated = tax_total_updated.toFixed(2);
		var tax_total_html = '$' + tax_total_updated.toString();		
		$('#tax_total').val(tax_total_updated);
		$('#tax-total').html(tax_total_html);
		
		var total_updated = ((sub_total_updated * 1) + (tax_total_updated * 1));
		total_updated = total_updated.toFixed(2);
		var total_html = '$' + total_updated.toString();		
		$('#order_total').val(total_updated);
		$('#order-total').html(total_html);
	}	
}

function showModalImg(dialog_width, dialog_height, img_src) {
	var img_tag = '<img src="/shop/skshop_images/products/thumbnails/' + img_src + '" />';
	$("#dialog-image").html(img_tag);
	if (dialog_width > 660) {
		dialog_width = 660;	
	}
	if (dialog_height > 540) {
		dialog_width = 540;	
	}
	
	$("#dialog-image").dialog({
		width: dialog_width,
		height: dialog_height,
		modal: true
	});	
}

function openIframeDialog(id, dialog_width, dialog_height, iframe_src) {
	var selector = '#' + id;
	var iframe_id = 'iframeId' + id;
	var iframe_selector = '#' + iframe_id;
	
	var iframe_html = '<iframe id="' + iframe_id + '" width="100%" height="100%" marginwidth="0" marginheight="0" frameborder="0" scrolling="auto" />';
	$(selector).html(iframe_html);
	$(iframe_selector).attr('src', iframe_src);	
	
	$(selector).dialog({
		width: dialog_width,
		height: dialog_height,
		modal: true
	});	
}

function toggleDiv(id) {
	var selector = '#' + id;
	$(selector).slideToggle();
}

function showMsg(no_hide) {	
	if (no_hide == null) {
		$('#display-msg').fadeIn();
		setTimeout(hideMsg, 5000);
	} else {
		$('#error-msg').slideToggle('fast');
	}
}

function hideMsg() {
	$('#display-msg').fadeOut('slow');
}

function displayErrors(errors, focus_selector) {
	var error_msg = '<strong>PLEASE FIX THE FOLLOWING ISSUES TO CONTINUE:</strong><br />';
	$.each(errors, function() {
		error_msg = error_msg + this + '<br />';
	});
	$('#error-msg').html(error_msg);
	$('#error-msg').show();
	var offset = $('#error-msg').offset();
	var error_ht = $('#error-msg').height();
	var scrollto = offset.top - error_ht;
	$(document).scrollTop(scrollto);	
	$(focus_selector).focus();
}

function printDirections() {
	var from_address = $('#from_address').val();
	var to_address = $('#to_address').val();
	var locale = $('#locale').val();
	var url = 'directions-print.php?from=' + encodeURIComponent(from_address) + '&to=' + encodeURIComponent(to_address) + '&locale=' + locale;
	window.location = url;
}

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18479536-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

//functions below are old shop functions that should be replaced when time permits

function popup(pagename,height,width){
	var h = parseInt(height);
	var w = parseInt(width);
	var hw = "HEIGHT="+h+",WIDTH="+w+",scrollbars=1,resizable=1"
	myWin = window.open(pagename,"",hw)
}

function recipientChange(myform,id) {
	var string = cust[id];
	var separator = '|';
	
	var stringArray = string.split(separator);
	if ( id > 0 ) {
		myform.s_firstname.value = stringArray[0];
		myform.s_lastname.value = stringArray[1];
		myform.s_phone1.value = stringArray[2].substr(0,3);
		myform.s_phone2.value = stringArray[2].substr(3,3);
		myform.s_phone3.value = stringArray[2].substr(6,4);
		myform.s_company1.value = stringArray[3].substr(0,3);
		myform.s_company2.value = stringArray[3].substr(3,3);
		myform.s_company3.value = stringArray[3].substr(6,4);
		myform.s_address1.value = stringArray[4];
		myform.s_address2.value = stringArray[5];
		myform.s_city.value = stringArray[6];
		myform.s_state.value = stringArray[7];
		myform.s_zip.value = stringArray[8];	
	} else {
		myform.s_firstname.value = '';
		myform.s_lastname.value = '';
		myform.s_phone1.value = '';
		myform.s_phone2.value = '';
		myform.s_phone3.value = '';
		myform.s_company.value = '';
		myform.s_address1.value = '';
		myform.s_address2.value = '';
		myform.s_city.value = '';
		myform.s_state.value = '';
		myform.s_zip.value = '';		
	}
}

var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
	input.value = input.value.slice(0, len);
	input.form[(getIndex(input)+1) % input.form.length].focus();
}

function containsElement(arr, ele) {
	var found = false, index = 0;
	while(!found && index < arr.length)
		if(arr[index] == ele)
			found = true;
		else
			index++;
	return found;
}

function getIndex(input) {
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1)
		if (input.form[i] == input)index = i;
		else i++;
		return index;
	}
	return true;
}

var nav4 = window.Event ? true : false;
function acceptNum(evt){	
	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57	
	var key = nav4 ? evt.which : evt.keyCode;	
	return (key <= 13 || (key >= 48 && key <= 57));
}

function NoAcceptEnter(evt){	
	var key = nav4 ? evt.which : evt.keyCode;	
	
	if(key==13){
		return false;  
	}
}

function acceptDes(evt){	
	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57	
	var key = nav4 ? evt.which : evt.keyCode;	
	return (key <= 13 || (key >= 48 && key <= 57));
}
function textLimit(field, maxlen) {
	if (field.value.length > maxlen + 1)
	//alert('your input has been truncated!');
	if (field.value.length > maxlen)
	field.value = field.value.substring(0, maxlen);
}

