﻿//
//  Date 28th April 2006
//
//  Copyright (c) 2006 Global Optimum Limited.
//
// Javascript functions

function callSearchPage(action, m)
{
        document.searchOptions.action = action;
        document.searchOptions.mode.value = m;
        document.searchOptions.submit();        
}

function goBack()
{
    history.go(-1);
}

function openWin(url, w, h, s)
{
    var winprop = "resizeable=no,width=" + w + ",height=" + h + ",scrollbars=" + s + ",menubar=0,titlebar=0,status=0";
    openwin = window.open(url,'',winprop);
}
	
function changeMode(newMode)
{
    document.searchOptions.mode.value = newMode;
    document.searchOptions.submit();
        
}

function updateqty()
{
    var numWines = document.searchOptions.qty.value;
    var bpw = document.searchOptions.qtyPerWine.value;
	if(bpw == 3)
	{
	    if(numWines == 2 || numWines == 1)
	    {
	        alert("There is a mismatch between your total bottles and bottles of each wine. Please try again.");
    	    document.getElementById('6total').selected = true;	        
    	    return false;
	    }
    }
	
	if(bpw == 2)
	{
	    if(numWines == 3 || numWines == 1)
	    {
	        alert("There is a mismatch between your total bottles and bottles of each wine. Please try again.");
    	    document.getElementById('6total').selected = true;	        
    	    return false;
	    }
    }
    
    return true;
}


function updatebpw()
{
    var numWines = document.searchOptions.qty.value;
    var bpw = document.searchOptions.qtyPerWine.value;
	if(numWines == 3)
	{
	    if(bpw == 2)
	    {
    	    document.getElementById('onebtl').selected = true;	        
	    }
    }
	if(numWines == 2)
	{
	    if(bpw == 3)
	    {
    	    document.getElementById('onebtl').selected = true;	        
	    }
    }
	if(numWines == 1)
	{
	    if(bpw == 3 || bpw == 2)
	    {
    	    document.getElementById('onebtl').selected = true;	        
	    }
    }
    
    return true;				
}


function submitSearch(call)
{
    document.searchOptions.mode.value = "search";
    document.searchOptions.call.value = 1 + Math.round(Math.random()*call);
    document.searchOptions.submit();
        
}

function submitNewBudgetDelta(delta)
{
    document.searchOptions.mode.value = "search";
    document.searchOptions.call.value = 0;
    document.searchOptions.budget.value = Number(document.searchOptions.budget.value) + delta;
    document.searchOptions.submit();      
}

function submitNewBudget(b)
{
    document.searchOptions.mode.value = "search";
    document.searchOptions.call.value = 0;
    document.searchOptions.budget.value = b;
    document.searchOptions.submit();      
}
 

// six and mix functions
function sixCritFound()
{
    document.searchOptions.crit.value = document.searchOptions.sixCrit.value;
    document.searchOptions.mode.value = "sixShowCrit";
    document.searchOptions.submit();

}

function mixCritFound()
{
    document.searchOptions.mixCrit.value = document.searchOptions.mixCritSelect.value;
    document.searchOptions.mode.value = "mixShowCrit";
    document.searchOptions.submit();

}

function sixCritValFound()
{
    document.searchOptions.critVal.value = document.searchOptions.sixCritVal.value;
    document.searchOptions.mode.value = "sixShowWines";
    document.searchOptions.submit();

}

function mixCritValFound()
{
    document.searchOptions.mixCritVal.value = document.searchOptions.mixCritValSelect.value;
    document.searchOptions.mode.value = "search";
    document.searchOptions.submit();

}


function sixWineFound()
{

    document.searchOptions.sixWine.value = document.searchOptions.sixWineSelect.value;
    document.searchOptions.mode.value = "mixSelectCrit";
    document.searchOptions.submit();

}

function sixWineFocus()
{

    document.searchOptions.sixWineSelect.value = 'Select Wine';
}



// advanced functions


function setCurGroup(gpNum)
{
    document.searchOptions.curGP.value = gpNum;
}

function progress(el, val, newMode)
{
    el.value = val;
    document.searchOptions.mode.value = newMode;
    document.searchOptions.submit();
}

function progress2(el1, val1, el2, val2, newMode)
{
    el1.value = val1;
    el2.value = val2;
    document.searchOptions.mode.value = newMode;
    document.searchOptions.submit();
}

function progress3(el1, val1, el2, val2, el3, val3, newMode)
{
    el1.value = val1;
    el2.value = val2;
    el3.value = val3;
    document.searchOptions.mode.value = newMode;
    document.searchOptions.submit();
}

function progress4(el1, val1, el2, val2, el3, val3, el4, val4, newMode)
{
    el1.value = val1;
    el2.value = val2;
    el3.value = val3;
    el4.value = val4;
    document.searchOptions.mode.value = newMode;
    document.searchOptions.submit();
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function getElementValue(formObj, type, sName) {
  var elems = formObj.getElementsByTagName(type);
  var c_elem = elems.length;
  var i = 0;
  for (i=0;i<c_elem;i++)
  {
    var elem = elems[i];
    if (elem.name == sName)
    {
        return elem.value;    
    }
  }    

  return "";
}

// other functions
function submitCMToCart()
{
    document.searchOptions.add.value = "1";
    document.searchOptions.business.value = "james@bigredwine.co.uk";
    document.searchOptions.method='post'; 
//    document.searchOptions.action='https://www.sandbox.paypal.com/cgi-bin/webscr'; 
    document.searchOptions.action='https://www.paypal.com/cgi-bin/webscr'; 
    document.searchOptions.submit();
}

function setStdOptionsForForm(add, bus)
{   
    add.value = "1";
    bus.value = "james@bigredwine.co.uk";
}

