function openWindow(URL,width,height) {day = new Date();id = day.getTime();eval("page" + id + " = window.open('" + URL + "', '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width="+width+",height="+height+",left="+((screen.width - width)/2)+",top="+((screen.height - height)/2)+"');");return false;}
function isEmail(address) {var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;if(reg.test(address) == false) {return false;} else {return true;}}
function swapImg(element, source) {document.getElementById(element).src = source;return false;}
function isInteger(nr){var patt=/(^\d+$)/;if(!patt.test(nr)){return false;}return true;}

$(document).ready(function() {
    browserDetection();
    fixIE6alphaTransparency();
	attachSwf();
    $("#news").newsTicker();
	checkboxCart();
	$('#form-cart-add').submit(validateAddCart);
	$('#form-billing-shipping').submit(validateBillingShipping);
	$('#form-method').submit(validateMethod);
	$('#form-payment-info').submit(validatePayment);
	$('.validate').blur(function () {
		if (($.trim($(this).attr('value')) == '') || ($.trim($(this).attr('value')) == '() -')) {
			$(this).next('span').css('display','inline').fadeOut(2000);
		}
	});
	$('#ship').click(function () {
		if ($(this).attr("checked") == true) {
    		$('#ship_area').slideDown();
		} else {
			$('#ship_area').slideUp();
		}
	});
});

function attachSwf() {
	if ($('#media-slide').length > 0) {
        flashembed('media-slide', {src: "resources/media/slide.swf", wmode:'transparent'});
    }
}

function checkboxCart() {
	if ($('input#checkall').length > 0) {
		$('input#checkall').click(function () {
			if ($(this).attr('checked') == true) {
				$('input.mark').attr('checked', 'checked');
			} else {
				$('input.mark').removeAttr('checked');
			}
		});
	}
}

function validateAddCart () {
	if (!isInteger($("#qty").val())) {
		alert("Please enter a valid quantity");
		$("#qty").select();
		$("#qty").focus();
		return false;
	}
	if ($("#type").val() == '0') {
		alert("Please choose item");
		$("#type").focus();
		return false;
	}
	$.each($('select.itemOption'), function (i,val){
		if ($(this).val() == '') {
			alert("Please choose all options");
			$(this).focus();
			return false;
		}
	});
	return true;
}

function validateBillingShipping() {
    if ($.trim($('#fname').attr('value')) == '') {
        alert('Please enter your first name');
        $('#fname').focus();
        return false;
    }
    if ($.trim($('#lname').attr('value')) == '') {
        alert('Please enter your last name');
        $('#lname').focus();
        return false;
    }
	if ($('#company').length > 0) {
		if ($.trim($('#company').attr('value')) == '') {
			alert('Please enter your company');
			$('#company').focus();
			return false;
		}
	}
    if ($.trim($('#address1').attr('value')) == '') {
        alert('Please enter your address');
        $('#address1').focus();
        return false;
    }
    if ($.trim($('#city').attr('value')) == '') {
        alert('Please enter city');
        $('#city').focus();
        return false;
    }
    if ($.trim($('#state').attr('value')) == '') {
        alert('Please choose state');
        $('#state').focus();
        return false;
    }
    if ($.trim($('#zip').attr('value')) == '') {
        alert('Please enter zip');
        $('#zip').focus();
        return false;
    }
    if ($.trim($('#phone').attr('value')) == '') {
        alert('Please enter your phone number');
        $('#phone').focus();
        return false;
    }
    if (!isEmail($('#email').attr('value'))) {
        alert('Please your email address');
        $('#email').focus();
        return false;
    }
    return true;
}

function validateMethod () {
	if ($('input[name="method"]:checked').length == 0) {
		alert("Please choose payment method.");
		return false;
	}
	return true;
}

function validatePayment() {
    if ($.trim($('#cc_type').attr('value')) == '') {
        alert('Please enter credit card type');
        $('#cc_type').focus();
        return false;
    }
    if ($.trim($('#cc_no').attr('value')) == '') {
        alert('Please enter credit card number');
        $('#cc_no').focus();
        return false;
    }
    if ($.trim($('#cc_month').attr('value')) == '') {
        alert('Please enter credit card expiring month');
        $('#cc_month').focus();
        return false;
    }
    if ($.trim($('#cc_year').attr('value')) == '') {
        alert('Please enter credit card expiring year');
        $('#cc_year').focus();
        return false;
    }
	if ($.trim($('#cc_ccv').attr('value')) == '') {
		alert('Please enter credit card verification');
		$('#cc_ccv').focus();
		return false;
	}
    return true;
}