// JavaScript Document
var winSrc;
// self.name = "mainWindow";
function openNewWindow(winSrc) {
    window.open(winSrc,'pathWindow','menubar=no,toolbar=no,location=no,status=no,directories=no,copyhistory=no,resizable=yes,scrollbars=no')
}


function toggle_visibility(id) {
	var e = document.getElementById(id);
	if(e.style.display == 'none')
		e.style.display = '';
	else
		e.style.display = 'none';
}

function validateShipping(form){
	
	
	if(!form.agreeToTerms.checked){
		alert("You Must Agree to Terms and Conditions");
		return false;
	}
	if(!form.billingFirstName.value){
		alert("You Must Enter a First Name");
		return false;
	}
	else if(!form.billingLastName.value){
		alert("You Must Enter a Last Name");
		return false;
	}
	else if(!form.billingAddress1.value){
		alert("You Must Enter an Adress");
		return false;
	}
	else if(!form.billingCity.value){
		alert("You Must Enter a City Name");
		return false;
	}
	else if((!form.billingState.value) && (form.billingCountry.value !== "GB")){
		alert("You Must Select a State");
		return false;
	}
	else if(!form.billingPostalCode.value){
		alert("You Must Enter a Postal Code");
		return false;
	}
	else if(!validateEmail(form.billingEmail1.value)){
		alert("You Must Enter a Valid Email");
		return false;
	}
	else if(!form.billingPhone1.value){
		alert("You Must Enter an Phone Number");
		return false;
	}
	else if(form.deliveryUseBilling.checked == true){
		return validateDelivery(form);
	}
	else return true;
	
}

function validateDelivery(form){
	if(!form.deliveryFirstName.value){
		alert("You Must Enter a Delivery First Name");
		return false;
	}
	else if(!form.deliveryLastName.value){
		alert("You Must Enter a Delivery Last Name");
		return false;
	}
	else if(!form.deliveryAddress1.value){
		alert("You Must Enter a Delivery Address");
		return false;
	}
	else if(!form.deliveryCity.value){
		alert("You Must Enter a Delivery City Name");
		return false;
	}
	else if((!form.deliveryState.value) && (form.deliveryCountry.value !== "GB")){
		alert("You Must Select a Delivery State");
		return false;
	}
	else if(!form.deliveryPostalCode.value){
		alert("You Must Enter a Delivery Postal Code");
		return false;
	}
	else if(!form.deliveryPhone1.value){
		alert("You Must Enter an Delivery Phone Number");
		return false;
	}
	else return true;
	
}

function validateEmail(email) {
    reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return reg.test(email);
   }



function getEstimatedShippingDate() {

    var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

    var todaysDate = new Date();
    // todaysDate.setDate(todaysDate.getDate() - 6); // adjust today's date, for debugging purposes

    var estimatedShippingDate = new Date();

    // examine the day of the week to determine a more accurate estimated shipping date.  for instance, packages
    // don't ship (or move) on sundays, so we need to account for that.
    if (todaysDate.getDay() < 4) {
        // if today's day falls on the interval [sun, wed], add 3 days to get est. shipping date.
        estimatedShippingDate.setDate(todaysDate.getDate() + 3);
    } else {
        // else today's day falls on the interval [thur, sat], add 4 days to get est. shipping date.
        estimatedShippingDate.setDate(todaysDate.getDate() + 4);
    }

    var monthTxt = months[estimatedShippingDate.getMonth()];
    var dayTxt = estimatedShippingDate.getDate();
    var yearTxt = estimatedShippingDate.getFullYear();

    return document.write(monthTxt + " " + dayTxt + ", " + yearTxt);
}


// For the extra options cart (basket upsell)


function toggleInsertNewDiv(divID,product,price)
{
	if (!document.getElementById(divID)) {
		var newdiv = document.createElement('tr');
		
		var tbodycontainer = document.getElementById("faketable");
		var row = document.createElement("tr");
		row.id = divID;
		
		var cell1 = document.createElement("td");
		cell1.className = "tRow";
		cell1.innerHTML = product;
		
		var cell2 = document.createElement("td");
		cell2.className = "tRow";
		cell2.innerHTML = price;
		
		var cell3 = document.createElement("td");
		cell3.className = "tRow";
		cell3.innerHTML = "1";
		
		var cell4 = document.createElement("td");
		cell4.className = "tRow";
		cell4.innerHTML = price;
		
		row.appendChild(cell1);
		row.appendChild(cell2);
		row.appendChild(cell3);
		row.appendChild(cell4);
		tbodycontainer.appendChild(row);
		
	} else {
		var d = document.getElementById("faketable");
		d.removeChild(document.getElementById(divID));
	}
	newBasketHideorShow("newDivContainer","faketable")
}
function newBasketHideorShow(containerdiv,tablecheck)
{
	var theContainer = document.getElementById(containerdiv)
	var tablechecker = document.getElementById(tablecheck)
	if(!tablechecker.hasChildNodes())
	{
		theContainer.className= "iwanttohide";
	} else {
		theContainer.className= "iwanttoshow";
	}
}