﻿//
//  Date 9th November 2007
//
//  Copyright (c) 2007 Global Optimum Limited.
//
//  Javascript functions 
//

function callSearchPage(action, m)
{
        document.searchOptions.action = action;
        document.searchOptions.mode.value = m;
        document.searchOptions.submit();        
}

function callATCPage(action, m, bl, qstype)
{
        document.searchOptions.action = action;
        document.searchOptions.mode.value = m;
        document.searchOptions.bl.value = bl;
        document.searchOptions.mode.qstype = 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 submitSearch(cid)
{
    document.searchOptions.cid.value = 1 + Math.round(Math.random()*cid);
    document.searchOptions.action = "casemixer.asp";
    document.searchOptions.submit();
        
}

function submitNewBudgetDelta(delta)
{
    document.searchOptions.cid.value = 0;
    document.searchOptions.budget.value = Number(document.searchOptions.budget.value) + delta;
    document.searchOptions.submit();      
}

function submitNewBudget(b)
{
    document.searchOptions.cid.value = 0;
    document.searchOptions.budget.value = b;
    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(buyList)
{
    document.searchOptions.mode.value = "atc";
    document.searchOptions.bl.value = buyList;
    document.searchOptions.submit();
}

function validatePrice()
{
   if (isNaN(document.searchOptions.minprice.value))
   {
        alert("You have entered an invalid Minimum Bottle price. Please try again.");
        return false;
   }
   if (isNaN(document.searchOptions.maxprice.value))
   {
        alert("You have entered an invalid Maximum Bottle price. Please try again.");
        return false;
   }
  
   if (document.searchOptions.totalBottles.value != document.searchOptions.qty.value)
   {
        alert("The total number of wines you have asked for does not match your case size. Please adjust the number of each type of wine you require, or adjust your case size.");
        return false;
   }
   
   return true;

}

