
function flightchangenumber(){
 var select1 = document.getElementById('adults');
 var adultselecttotal = select1.item(select1.selectedIndex).value * document.getElementById('adultprice').value;
 var select2 = document.getElementById('children');
 var childselecttotal = select2.item(select2.selectedIndex).value * document.getElementById('childprice').value;
 var select3 = document.getElementById('infants');
 var infantselecttotal = select3.item(select3.selectedIndex).value * document.getElementById('infantprice').value;
 var flighttotal = adultselecttotal + childselecttotal + infantselecttotal ;
	
document.getElementById("flttot").value = flighttotal 
}

function sochangenumber(){
 var select1 = document.getElementById('adults');
 var adultselecttotal = select1.item(select1.selectedIndex).value * document.getElementById('adultprice').value;
 var flighttotal = adultselecttotal 
	
document.getElementById("flttot").value = flighttotal 
}

function tourchangenumber(){

 var select1 = document.getElementById('singles');
 var adultselecttotal = select1.item(select1.selectedIndex).value * document.getElementById('singleprice').value;
 var select2 = document.getElementById('doubles');
 var childselecttotal = select2.item(select2.selectedIndex).value * document.getElementById('doubleprice').value;
 var select3 = document.getElementById('triples');
 var infantselecttotal = select3.item(select3.selectedIndex).value * document.getElementById('tripleprice').value;
  
 var etourtotal = adultselecttotal + childselecttotal + infantselecttotal ;
	
 document.getElementById("etourtot").value = etourtotal  
 
}


function amendcookie () {

	var basketids = readCookie('basketIds');
	var selectedproductpriceband = document.getElementById('adultpriceband').value;
	var selectedproduct = document.getElementById('productid').value;

	// so i need to split the existing cookie and find the value that relates to the current product.

	var arrProducts = basketids.split(",");
	// now search for product that match
	var i=0;
	while (i < arrProducts.length) {

		var arrProductDetails = arrProducts[i].split("|");

		if (arrProductDetails[0] == selectedproduct && arrProductDetails[4] == selectedproductpriceband){
			if (document.getElementById("flttot").value == "0") {

			}
			else
			{
			var oldprice = arrProductDetails[2]
			var newprice = arrProducts[i].replace('|'+oldprice+'|','|'+document.getElementById("flttot").value+'|');
			
			/* append details about the number of adults etc */
			
 			var select1 = document.getElementById('adults');
 			var adultselecttotal = select1.item(select1.selectedIndex).value 
 			var select2 = document.getElementById('children');
 			var childselecttotal = select2.item(select2.selectedIndex).value 
 			var select3 = document.getElementById('infants');
 			var infantselecttotal = select3.item(select3.selectedIndex).value 
	
			newprice = newprice + adultselecttotal + "|" + childselecttotal + "|" + infantselecttotal  + "|"

			var test = basketids.replace(arrProducts[i],newprice);

			eraseCookie('minibasket');
 			createCookie('basketIds',test,7);
			}
		}
	   i++;
	}
}


function soamendcookie () {

	var basketids = readCookie('basketIds');
	var selectedproductpriceband = document.getElementById('adultpriceband').value;
	var selectedproduct = document.getElementById('productid').value;

	// so i need to split the existing cookie and find the value that relates to the current product.

	var arrProducts = basketids.split(",");
	// now search for product that match
	var i=0;
	while (i < arrProducts.length) {

		var arrProductDetails = arrProducts[i].split("|");

		if (arrProductDetails[0] == selectedproduct && arrProductDetails[4] == selectedproductpriceband){
			if (document.getElementById("flttot").value == "0") {

			}
			else
			{
			var oldprice = arrProductDetails[2]
			var newprice = arrProducts[i].replace('|'+oldprice+'|','|'+document.getElementById("flttot").value+'|');
			
			/* append details about the number of adults etc */
			
 			var select1 = document.getElementById('adults');
 			var adultselecttotal = select1.item(select1.selectedIndex).value 
 			//var select2 = document.getElementById('children');
 			var childselecttotal = 0;
 			//var select3 = document.getElementById('infants');
 			var infantselecttotal = 0;
	
			newprice = newprice + adultselecttotal + "|" + childselecttotal + "|" + infantselecttotal  + "|"

			var test = basketids.replace(arrProducts[i],newprice);

			eraseCookie('minibasket');
 			createCookie('basketIds',test,7);
			}
		}
	   i++;
	}
}
function amendcookie_escourted_tour () {

	var basketids = readCookie('basketIds');
	var arrProducts = basketids.split(",");
	var selectedproduct = document.getElementById('productid').value;

	var i=0;
	while (i < arrProducts.length) {
		var arrProductDetails = arrProducts[i].split("|");
		
		if (arrProductDetails[0] == selectedproduct){
			if (document.getElementById("etourtot").value == "0") {

			}
			else
			{
			var oldprice = arrProductDetails[2]
			var newprice = arrProducts[i].replace('|'+oldprice+'|','|'+document.getElementById("etourtot").value+'|');
		
			var select1 = document.getElementById('singles');
			var singlesselecttotal = select1.item(select1.selectedIndex).value 
			var select2 = document.getElementById('doubles');
			var doublesselecttotal = select2.item(select2.selectedIndex).value 
			var select3 = document.getElementById('triples');
			var triplesselecttotal = select3.item(select3.selectedIndex).value 
 
			newprice = newprice + singlesselecttotal + "|" + doublesselecttotal + "|" + triplesselecttotal  + "|";

			var test = basketids.replace(arrProducts[i],newprice);			

			eraseCookie('minibasket');
 			createCookie('basketIds',test,7);
			}
		}
	i++;
	}
}


function cancelitem (productid, numeral) {
    
    var currentBasketInfo = readCookie("basketIds");

       if(currentBasketInfo.indexOf(productid) > -1) {

       var arrProducts = currentBasketInfo.split(",");
        var newCookieValue = currentBasketInfo
        for (i=0;i < arrProducts.length;i++){

            if (arrProducts[i].indexOf(productid)> -1 && i == (numeral -1)) {
                //The product may be the begining of the string or in the middle or the last product and not have a comma 
                newCookieValue = newCookieValue.replace(arrProducts[i]+',', '');
                newCookieValue = newCookieValue.replace(","+arrProducts[i], '');
                newCookieValue = newCookieValue.replace(arrProducts[i], '');
            }
            else {
                //Do nothing, for the item with the product id that is in the basket
             }
        }
        //test to see if you have removed the last product
        //if so, delete the basket cookie
        if (newCookieValue.length==0){
        	eraseCookie("basketIds");
        }
        //otherwise recreate the cookie with the product removed fromt he list
        else{
        createCookie("basketIds",newCookieValue);
        }
       //reload the window to show change in basket
	eraseCookie('minibasket');       
	window.location.reload(true);
      }

}

function cancelitempre (productid) {
    
    var currentBasketInfo = readCookie("basketIds");

       if(currentBasketInfo.indexOf(productid) > -1) {

       var arrProducts = currentBasketInfo.split(",");
        var newCookieValue = currentBasketInfo
        for (i=0;i < arrProducts.length;i++){

            if (arrProducts[i].indexOf(productid)> -1 ) {
                //The product may be the begining of the string or in the middle or the last product and not have a comma 
                newCookieValue = newCookieValue.replace(arrProducts[i]+',', '');
                newCookieValue = newCookieValue.replace(","+arrProducts[i], '');
                newCookieValue = newCookieValue.replace(arrProducts[i], '');
            }
            else {
                //Do nothing, for the item with the product id that is in the basket
             }
        }
        //test to see if you have removed the last product
        //if so, delete the basket cookie
        if (newCookieValue.length==0){
        	eraseCookie("basketIds");
        }
        //otherwise recreate the cookie with the product removed fromt he list
        else{
        createCookie("basketIds",newCookieValue);
        }
       //reload the window to show change in basket
	eraseCookie('minibasket');       
	window.location.reload(true);
      }

}

function createCookie(name,value,days)
{
	var date = new Date();
	var expires = "";
	
	if (days)
	{	
		date.setTime(date.getTime()+(days*24*60*60*1000));	
		expires = "; expires="+date.toGMTString();
	}
		
	document.cookie = name +"="+ value + expires +"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	if (readCookie(name) != null)
	{
		createCookie(name,"",-1);
	}
	
}