// UA (user agent) contains detailed browser info
var UA = navigator.userAgent.toLowerCase();

// get an element with the id
function getObj(elementID) {
  if (typeof elementID == "string")
    return document.getElementById(elementID);
  else
    return elementID;
}

// Set the text color of an object
function setColor(object, color) {
  getObj(object).style.color = color;
}

// Change the cursor.
// The second argument is optional.
function setCursor(cursortype, thisobj) {
  if (UA.indexOf("msie 5")>=0 && cursortype == 'pointer')
    cursortype = 'hand';
  if (thisobj==null)
    document.body.style.cursor = cursortype;
  else
    getObj(thisObj).style.cursor = cursortype;
}

function productLightup(elementID, position, color) {
  if(position == 'over') {
    getObj(elementID).parentNode.style.background = color;
    setCursor("pointer");
  } else {
    getObj(elementID).parentNode.style.background = color;
    setCursor("auto");
  }
}

// Set the background of an object
function setBackground(object, color) {
  getObj(object).style.background = color;
}

// Set the text color of an object
function setColor(object, color) {
  getObj(object).style.color = color;
}

function setBackgroundImage(object, url) {
	getObj(object).style.backgroundImage = "url('"+url+"')";
}

function underline(object, choice) {
    if(choice)
        getObj(object).style.textDecoration = "underline";
    else
        getObj(object).style.textDecoration = "none";
}

function changeBackground(element, position, imageUrl) {
    setBackgroundImage(element, imageUrl);
    if(position == 'over') {
        setCursor("pointer");
    } else {
        setCursor("auto");
    }
}

// Setting the visibility of an object
function setVisibility(objectID, vis) {
  var object = getObj(objectID);
  if(vis == true || vis=='visible' || vis=='y')
    object.style.visibility = "visible";
  else
    object.style.visibility = "hidden";
}

function showSubmenu(id) {
    getObj(id).style.borderCollapse = "collapse";
    setVisibility(id, true);
    setCursor("pointer");
}

function hideSubmenu(id) {
    setVisibility(id, false);
    setCursor("auto");
}

function goPage(location) {
window.location.href = location;
}

//There must be two decimals.
function checkValue(oldValue) {
	var newValue = new Array();
	newValue = (""+oldValue).split('.');
	if(!newValue[1])
		return oldValue+'.00';
	else if(newValue[1].length==1)
		return oldValue+'0';
	else
		return Math.round(oldValue*100)/100;
}

var goGreen = 'http://www.golfgear.nl/web/images/green.gif';
var goRed = 'http://www.golfgear.nl/web/images/red.gif';
var formErrors = new Array(false,false,false,false,false,false,false);
function checkInput(elementID, source) {
	var value = getObj(elementID).value;
	if(source=='numbers') {
		if(value != parseInt(value))
			getObj(elementID).value = value.substring(0,value.length-1);
	} else if(source=='name') {
		if(value.length>=2) {
			formErrors[0] = true;
			getObj('imgname').src = goGreen;
		} else {
			formErrors[0] = false;
			getObj('imgname').src = goRed;
		}
	} else if(source=='address') {
		if(value.length>=2) {
			formErrors[1] = true;
			getObj('imgaddress').src = goGreen;
		} else {
			formErrors[1] = false;
			getObj('imgaddress').src = goRed;
		}
	} else if(source=='postalcode') {
		if(value.match(/^(\d{4})$|^(\d{5})$|^(\d{4}[a-zA-Z]{2})$/)) { 
			formErrors[2] = true;
			if(formErrors[3])
				getObj('imgcode').src = goGreen;
		} else {
			formErrors[2] = false;
			getObj('imgcode').src = goRed;
		}
	} else if(source=='place') {
		if(value.length>=2) {
			formErrors[3] = true;
			if(formErrors[2])
				getObj('imgcode').src = goGreen;
		} else {
			formErrors[3] = false;
			getObj('imgcode').src = goRed;
		}
	} else if(source=='telephone') {
		if(value.match(/^(\d{2}-\d{8})$|^(\d{3}-\d{7})$|^(\d{4}-\d{6})$|^(\d{6,14})$/)) {
			formErrors[4] = true;
			getObj('imgtelephone').src = goGreen;
		} else {
			formErrors[4] = false;
			getObj('imgtelephone').src = goRed;
		}
	} else if(source=='email1') {
		if(value.match(/^([a-z0-9\.\-\_]+)*@([a-z0-9\.\-\_]+)\.([a-z0-9\.\-\_]{2,3})$/)) {
			formErrors[5] = true;
			getObj('imgemail1').src = goGreen;
		} else {
			formErrors[5] = false;
			getObj('imgemail1').src = goRed;
		}
	} else if(source=='email2') {
		if(value.match(/^([a-z0-9\.\-\_]+)*@([a-z0-9\.\-\_]+)\.([a-z0-9\.\-\_]{2,3})$/) && getObj('email1').value == value) {
			formErrors[6] = true;
			getObj('imgemail2').src = goGreen;
		} else {
			formErrors[6] = false;
			getObj('imgemail2').src = goRed;
		}
	}
}

function getErrors() {
	var errorStr = '';
	if(!formErrors[0])
		errorStr += "De naam is niet goed ingevuld.\n";
	if(!formErrors[1])
		errorStr += "Het adres is niet goed ingevuld.\n";
	if(!formErrors[2])
		errorStr += "De postcode is niet goed ingevuld.\n";
	if(!formErrors[3])
		errorStr += "De plaats is niet goed ingevuld.\n";
	if(!formErrors[4])
		errorStr += "Het telefoonnummer is niet goed ingevuld.\n";
	if(!formErrors[5] || !formErrors[6])
		errorStr += "Het e-mail adres is niet goed ingevuld.\n";
    if(getObj('conditions') &&  !getObj('conditions').checked)
        errorStr += "U dient akkoord te gaan met de algemene voorwaarden\n";
	if(!errorStr)
		return true;
	else
		alert(errorStr);
		return false;
}

function addProduct(tag_id, module, productName, extraspec)
{
  var option = '';
  if(extraspec) {
      index = getObj('extraspec').selectedIndex;
      if(index) {
          option = '/option/'+index;
      } else {
         option = '/option/0';
      }
  }
  new Ajax.Updater(tag_id, module+option+'.html', {asynchronous:true, evalScripts:false});
  alert(productName+' is toegevoegd aan de winkelwagen.');
  return false;
}

function changePrice(select)
{
    var newPrice = price+priceList[select.selected];
    newPrice = Math.round((price+priceList[select.selectedIndex])*100)/100;
    newPrice = checkValue(newPrice);
    newPrice = newPrice.toString().replace(".",",");
    getObj('price').innerHTML = newPrice;
}

function checkTimePopularity(productId)
{
    new Ajax.Updater(tag_id, module+option+'.html', {asynchronous:true, evalScripts:false});
    alert(productName+' is toegevoegd aan de winkelwagen.');
    return false;
}

var globalSendcosts;
var globalTotalCosts;
function changeCosts(selectObject, free)
{
  if ((getObj('paySelect') != selectObject) && (getObj('paySelect').value == 'cba'))
  {
    return;
  }
  
  var total = getObj('tdTotal').innerHTML;
  var sendCostsNl = 4.75;
  var sendCostsClassFree = "cartVerzendkosten";
  var sendCostsTextFree = "Verzendkosten (nl):";
  var sendCostsClass = "cartExtra";
  var sendCostsText = "Verzendkosten:";
  if (free == 'nl')
  {
    sendCostsNl = 0;
  }
  total = total.substring(2,total.length).replace(",",".");;
  total = parseFloat(total);

  oldSendCosts = getObj('tdSendCosts').innerHTML;
  oldSendCosts = oldSendCosts.substring(2,oldSendCosts.length).replace(",",".");
  oldSendCosts = parseFloat(oldSendCosts);
  sendCosts = oldSendCosts;
  
  total = total - oldSendCosts;
  
  if (selectObject.value == 'cba')
  {
    sendCosts = 0;
    if ((getObj('countrySelect') == null)
        && (globalSendcosts == undefined)
        && (globalTotalCosts== undefined))
    {
      globalSendcosts = getObj('tdSendCosts').innerHTML;
      globalTotalCosts = getObj('tdTotal').innerHTML;
    }
  }
  else if (getObj('countrySelect') != null)
  {
    sendCosts = 12.50;
    switch (getObj('countrySelect').value)
    {
      case 'NL':
        sendCosts = sendCostsNl;
        break;
      case 'BE':
      case 'DE':
      case 'LU':
        sendCosts = 9.95;
        break;
      case 'other':
      default:
        break;
    }
  }
  total = total + sendCosts;
  
  total = checkValue(Math.round(total*100)/100);
  
  if ((selectObject.value != 'cba') && (getObj('countrySelect') == null)
      && (globalSendcosts != undefined)
      && (globalTotalCosts!= undefined))
  {
    getObj('tdSendCosts').innerHTML = globalSendcosts;
    getObj('tdTotal').innerHTML = globalTotalCosts;
  }
  else if (sendCosts != undefined)
  {
    getObj('tdSendCosts').innerHTML = '&euro; '+checkValue(sendCosts).toString().replace(".",",");
    getObj('tdTotal').innerHTML = '&euro; '+total.toString().replace(".",",");
  }

  if (free == 'nl' && (getObj('countrySelect') == null || getObj('countrySelect').value == 'NL'))
  {
    getObj('tdSendCostsText').className = sendCostsClassFree;
    getObj('tdSendCostsText').innerHTML = sendCostsTextFree;
  }
  else
  {
    getObj('tdSendCostsText').className = sendCostsClass;
    getObj('tdSendCostsText').innerHTML = sendCostsText;
  }
}