function NewWindow(url,winName) { 
  newwindow = window.open(url,winName,"scrollbars=yes,resizable=yes,top=100,right=200, width=650, height=440");
 if (window.focus) {newwindow.focus()}
}

function TipWindow(url,winName) { 
  newwindow = window.open(url,winName,"scrollbars=yes,top=100,right=200, width=500, height=420");
 if (window.focus) {newwindow.focus()}
}


function shipsame(form){

if(form.sameasbilling.checked){
     form.NAMETOSHIP.value = form.NAME.value;
     form.ADDRESSTOSHIP.value = form.ADDRESS.value;
     form.CITYTOSHIP.value = form.CITY.value;
      // form.order[shipping_state].value = form.order[billing_state].value;
     form.PHONETOSHIP.value = form.PHONE.value;
     
   //  if(form.billing_state.type == "--"){
   //       var bStateIdx = form.order[billing_state].selectedIndex;
   //       form.order[shipping_state].options[bStateIdx].selected = true;
   //  }
   //  else{
          form.STATETOSHIP.value = form.STATE.value;
   //  }
}
else{
     form.NAMETOSHIP.value = "";
     form.ADDRESSTOSHIP.value = "";
     form.CITYTOSHIP.value = "";
     if(form.STATETOSHIP.type == "--"){
          form.STATETOSHIP.options[0].selected = true;
     }
     else{
          form.STATETOSHIP.value = "";
     }
     form.PHONETOSHIP.value = "";
}
}

function add_item_to_order(product) {
      new Ajax.Request('/products/add_item_to_order?prod='+ escape(product), {asynchronous:true, evalScripts:true}); return false;
}

function remove_item_from_order(product) {
	new Ajax.Request('/products/remove_item_from_order?prod='+ escape(product), {asynchronous:true, evalScripts:true}); return false;
}

// not completed

function checkShippingParams(shiptype,deliverydate,zipcode) {
  // ensure order params have not been changed
  // add to next button >> onclick="return checkShippingParams(document.ship_info.shipping_method.value, document.ship_info.order_delivery_date.value, document.ship_info.zip.value);"
  new Ajax.Request('/products/check_order_params?shipping='+ escape(shiptype) +'&delivery_date='+ escape(deliverydate) +'&zip='+ escape(zipcode), {asynchronous:true, evalScripts:true});
}


function validateDeliveryDate(date) {
	// TODO - finish this script
	// add to _buy_now_button.rhtml
	// onclick="return validateDeliveryDate(document.ship_info.order_delivery_date.value);"
	if (date != "") { // check if date was submitted
	  if (isDate(date)) {
	    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	    var matchArray = date.match(datePat); // is the format ok?
        var dateobj = new Date(matchArray[5],(matchArray[1]-1),matchArray[3],00,00,00);  
          if (dateobj.getDay() == 0 || dateobj.getDay() == 1) {
	       alert("Deliveries are not available on Monday or Sunday.  Please choose another Delivery Date.");
	       return false;
          }
          else
          alert(dateobj)
          // return true; 
        }
	  else
	    return false; // date is not a valid date
	}
	else
	return true; // just keep going
}

// ******************************************************************
// This function accepts a string variable and verifies if it is a
// proper date or not. It validates format matching either
// mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
// has the proper number of days, based on which month it is.

// The function returns true if a valid date, false if not.
// ******************************************************************

function isDate(dateStr) {

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
//alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
return false;
}

month = matchArray[1]; // p@rse date into variables
day = matchArray[3];
year = matchArray[5];

if (month < 1 || month > 12) { // check month range
// alert("Month must be between 1 and 12.");
return false;
}

if (day < 1 || day > 31) {
// alert("Day must be between 1 and 31.");
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
// alert("Month "+month+" doesn`t have 31 days!")
return false;
}

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
// alert("February " + year + " doesn`t have " + day + " days!");
return false;
}
}
return true; // date is valid
}

function tabToggle(selectedTab) {
	var tabs = new Array('chowder','conch', 'lobster');
	       
	for (var i = 0; i < tabs.length; i++) {
		var tabObject = getByID(tabs[i] + '-tab');
		var contentObject = getByID(tabs[i] + '-content');
		       
		if (tabObject && contentObject) {
			if (tabs[i] == selectedTab) {
				tabObject.className = 'current';
				contentObject.style.display = 'block';
			} else {  
				tabObject.className = 'default';
				contentObject.style.display = 'none';
			}       
		}       
	}       
}       
      
function getByID (n) {
    var d = window.document;
    if (d.getElementById)
        return d.getElementById(n);
    else if (d.all)
        return d.all[n];
}





