function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}

function popUp(url,w,h) {
   newWin = window.open(url,'_blank','width='+w+',height='+h+',menubar=no,scrollbars=yes,status=yes,resizable=yes');
   newWin.focus();
}

/* Start external links handler */
function externalLinks() {
   if (!document.getElementsByTagName) return;
   var anchors = document.getElementsByTagName("a");
   for (var i=0; i<anchors.length; i++) {
      var anchor = anchors[i];
      if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
         anchor.target = "_blank";
   }
}

addLoadEvent(externalLinks);
/* End external links handler */

function copyInvoiceData(isChecked) {
	if (isChecked) {
		document.getElementById('txtShipName').value = document.getElementById('txtName').value;
		document.getElementById('txtShipEmail').value = document.getElementById('txtEmail').value;
		document.getElementById('txtShipAddress').value = document.getElementById('txtAddress').value;
		document.getElementById('txtShipCity').value = document.getElementById('txtCity').value;
		document.getElementById('txtShipPostcode').value = document.getElementById('txtPostcode').value;
		document.getElementById('txtShipCountry').value = document.getElementById('txtCountry').value;
		document.getElementById('txtShipPhone').value = document.getElementById('txtPhone').value;
	}
}

function checkUncheckAll(objForm) {
	var blnChecked;
	for (i=0; i<objForm.elements.length; i++) {
		if (objForm.elements[i].type=='checkbox') {
			if (typeof(blnChecked)=='undefined') blnChecked = !objForm.elements[i].checked;
			objForm.elements[i].checked = blnChecked;
		}
	}
}


/* Start basket/favorites handler */

var xml_err_conn = 'Hi ha hagut un error amb la conexió. Si us plau, intenti-ho més tard.'
var xml_status_ok = 'OK';

//var basket_url = '_carrito_nook.xml';
var basket_caller = 'basketadd';
var basket_ok_class = 'basketadded';
//var basket_ok_text = 'Afegit a la cistella';
//var basket_ok_imgsrc = 'images/icon_basket_added.gif';

//var fav_url = '_favoritos_ok.xml';
var fav_caller = 'favadd';
var fav_ok_class = 'favadded';
//var fav_ok_text = 'Afegit als favorits';
var fav_ok_imgsrc = 'images/icon_fav_added.gif';

//var shop_url = '_shoppinglist_ok.xml';
var shop_caller = 'favadd';
var shop_ok_class = 'favadded';
//var shop_ok_text = 'Afegit a la meva llista';
var shop_ok_imgsrc = 'images/icon_fav_added.gif';

function addToBasket(id) {
	getAjax(id, basket_url, basket_caller, basket_ok_class, basket_ok_text, basket_ok_imgsrc);
}

function addToFav(id) {
	getAjax(id, fav_url, fav_caller, fav_ok_class, fav_ok_text, fav_ok_imgsrc);
}

function addToShoppingList(id) {
    getAjax(id, shop_url, shop_caller, shop_ok_class, shop_ok_text, shop_ok_imgsrc);
}

function getAjax(id, url, caller, ok_class, ok_text, ok_imgsrc) {
	$.ajax({
		url: url + '?ref=' + id,
		type: 'GET',
		dataType: 'xml',
		timeout: 1000,
		error: function(){
			alert(xml_err_conn);
		},
		success: function(xml){
		    $(xml).find('status').each(function(){
			 	var conn_status = $(this).text();

				if (conn_status == xml_status_ok) {
					var caller_elem = caller + '_' + id;
					var is_text = true;
					$('#' + caller_elem).find('img').each(function(){
						$(this).attr('src', ok_imgsrc).attr('alt', ok_text);
						is_text = false;
					});
					if (is_text) {
						$('#' + caller_elem).attr('class', ok_class).text(ok_text);
					}
				} else {
					$(xml).find('message').each(function(){
						var err_mesg = $(this).text();
						alert(err_mesg);
					});
			 	}
			});
		}
	});
}

/* End basket/favorites handler */



/* Start form handler */

function submitForm(strFormId, strCommand, strAction, blnCheckForm) {
	objForm = document.getElementById(strFormId);
	if ((!blnCheckForm) || objForm.onsubmit) {
		if ((typeof(strCommand) != 'undefined') && (strCommand != '')) {
			document.getElementById('command').value = strCommand;
		}
		if ((typeof(strAction) != 'undefined') && (strAction != '')) {
			objForm.action = strAction;
		}
		objForm.submit();
	}
}

function getLabel(objForm, fieldId) {
	var labelList = document.getElementById(objForm.id).getElementsByTagName('label');
	// loop through label array attempting to match each 'for' attribute to the id of the current element
	for(var lbl = 0; lbl < labelList.length; lbl++) {
		// Internet Explorer requires the htmlFor test
		if(labelList[lbl]['htmlFor'] && labelList[lbl]['htmlFor'] == fieldId) {
			label = labelList[lbl];
			// All other compliant browsers
		} else if(labelList[lbl].getAttribute('for') == fieldId) {
			label = labelList[lbl];
		}
	}
	return label;
}

function checkShipPayMethod(objForm) {
	var blnShipMeth = false;
	var blnPayMeth = false;
	if (objForm.shipmeth1.checked || objForm.shipmeth2.checked || (objForm.shipmeth3.checked && objForm.pickupShop.value != '')) {
		blnShipMeth = true;
		getLabel(objForm, 'shipmeth1').className = '';
		getLabel(objForm, 'shipmeth2').className = '';
		getLabel(objForm, 'shipmeth3').className = '';
		getLabel(objForm, 'pickupShop').className = '';
		objForm.pickupShop.className = '';
	} else {
		if (!(objForm.shipmeth1.checked || objForm.shipmeth2.checked || objForm.shipmeth3.checked)) {
			getLabel(objForm, 'shipmeth1').className = 'invalid';
			getLabel(objForm, 'shipmeth2').className = 'invalid';
			getLabel(objForm, 'shipmeth3').className = 'invalid';
			getLabel(objForm, 'pickupShop').className = '';
			objForm.pickupShop.className = '';
		} else {
			getLabel(objForm, 'shipmeth1').className = '';
			getLabel(objForm, 'shipmeth2').className = '';
			getLabel(objForm, 'shipmeth3').className = '';
			getLabel(objForm, 'pickupShop').className = 'invalid';
			objForm.pickupShop.className = 'invalid';
		}
	}
	for (i=1; i<=4; i++) {
		objRadio = document.getElementById('paymeth'+i);
		if (objRadio != null) {
			if (objRadio.checked) {
				blnPayMeth = true;
				for (j=1; j<=4; j++) {
					getLabel(objForm, 'paymeth'+j).className = '';
				}
			}
		}
	}
	if (!(blnPayMeth)) {
		for (i=1; i<=4; i++) {
			if (document.getElementById('paymeth'+i) != null)
				getLabel(objForm, 'paymeth'+i).className = 'invalid';
		}
	}
	var isValidForm = (blnShipMeth && blnPayMeth);
	if (!(isValidForm)) alert(validacio_camps_nok);
	return isValidForm;
}

function selRadio(fldGroup, numMethod, blnChecked) {
	document.getElementById(fldGroup + numMethod).checked = blnChecked;
}

/* End form handler */
