/**
* @author Cristian Ciobanu
*/
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 = 50,top = 50');");
	return false;
}

function swapPIC(id, source) {
	document.getElementById(id).src = source;
	return false;
}

function trim(a) {
	a = a.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
}

function isEmail(str) {
	var at = "@";
	var dot = ".";
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);

	if (str.indexOf(at)==-1){
		return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	}
	if (str.indexOf(at,(lat+1))!=-1){
		return false;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	}
	if (str.indexOf(dot,(lat+2))==-1){
		return false;
	}
	if (str.indexOf(" ")!=-1){
		return false;
	}
	return true;
}

function followLink(url) {
	window.location = url;
}



function IsNumeric(sText, accept) {
	var ValidChars = accept;
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++) {Char = sText.charAt(i);if (ValidChars.indexOf(Char) == -1) {IsNumber = false;}}
	return IsNumber;
}

function isInteger(nr){
	var patt=/(^\d+$)/;
	if(!patt.test(nr)){return false;}
	return true;
}
function isDouble(nr){
	var patt=/(^\d+$)|(^\d+\.\d+$)/;
	if(!patt.test(nr)){return false;}
	return true;
}

function isPhoneNumber(nr){
	var patt = /^[0-9]{3}[-/.]*[0-9]{3}[-/.][0-9]{4}$/;
	if(!patt.test(nr)){return false;}
	return true;
}

function validateQty(frm, qty, max) {
	var myFrm = document.getElementById(frm);
	var myQty = document.getElementById(qty).value;
	if (!IsNumeric(myQty, '0123456789') || myQty.length == 0) {alert('Please enter a valid quantity.');document.getElementById(qty).focus();document.getElementById(qty).select();return false;}
	if (parseInt(myQty) > parseInt(max)) {alert("We're sorry, but your quantity selected exceeds our current on hand stock of " + max + ". We have reduced your order accordingly.");document.getElementById(qty).value=max;return false;}
	myFrm.submit();return false;
}

function updateQty(val, qty) {
	document.getElementById(qty).value = document.getElementById(qty).value * document.getElementById(val).value;
}

function checkML() {
	var name = document.getElementById('mail_name');
	var email = document.getElementById('mail_email');
	
	if (trim(name.value).length == 0) {
		alert('Please enter your name.');name.focus();name.select();return false;
	}
	if (trim(email.value).length == 0) {
		alert('Please enter your e-mail.');email.focus();email.select();return false;
	}
	if (!isEmail(email.value)) {
		alert('Please enter a valid e-mail address.');email.focus();email.select();return false;
	}
	return true;
}

function submitFrm(element) {
	document.getElementById(element).submit();
	return true;
}

function checkGift() {
	if (document.getElementById('amount').value == '') {
		alert('Please choose gift card amount.');document.getElementById('amount').focus();return false;
	}
	if (!IsNumeric(document.getElementById('quantity').value, '0123456789') || document.getElementById('quantity').value.length == 0) {
		alert('Please enter a valid quantity.');document.getElementById('quantity').focus();document.getElementById('quantity').select();return false;
	}
	if (document.getElementById('recipient').value == '') {
		alert('Please enter recipient.');document.getElementById('recipient').focus();document.getElementById('recipient').select();return false;
	}
	if ((document.getElementById('send1').checked == true) && (document.getElementById('to_email').value == '')) {
		alert('Please enter recipient e-mail.');document.getElementById('to_email').focus();document.getElementById('to_email').select();return false;
	}
	if ((document.getElementById('send2').checked == true) && (document.getElementById('to_address').value == '')) {
		alert('Please enter recipient address.');document.getElementById('to_address').focus();document.getElementById('to_address').select();return false;
	}
	return true;
}

function checkRecipient() {
	if (document.getElementById('send1').checked == true) {
		document.getElementById('emailDiv').style.display = "block";
	} else {
		document.getElementById('emailDiv').style.display = "none";
	}
}

$(document).ready(function() {
    if ($('div.tweet').length > 0) {
        $(".tweet").tweet({
            username: "berkeleywineco",
            join_text: "auto",
            avatar_size: 32,
            count: 4,
            auto_join_text_default: "",
            auto_join_text_ed: "",
            auto_join_text_ing: "",
            auto_join_text_reply: "",
            auto_join_text_url: "",
            loading_text: "loading tweets..."
        });
    }
});