
function MM_validateForm() {
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain a valid e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain numbers only.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is a required field.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }

function validChars(evt, validChars, doUpperCase) {
  if (evt.target) {
	sourceElement = evt.target;
  }
  else {
	sourceElement =  event.srcElement;
  }

  elemValue = sourceElement.value;
  if (doUpperCase == 'true') {
	elemValue = elemValue.toUpperCase();
  }

  var newElemValue = "";
  for (vIdx=0; vIdx < elemValue.length; vIdx++) {
	if (validChars.indexOf(elemValue.substr(vIdx, 1)) != -1
		|| (doUpperCase == 'false' &&
			validChars.toLowerCase().indexOf(elemValue.substr(vIdx, 1)) != -1) ) {
	  newElemValue = newElemValue + elemValue.substr(vIdx, 1);
	}
  }
  sourceElement.value = newElemValue;
}

function isEmail(v)
{ 
	var re = /^[a-zA-Z._0-9-]+@[a-zA-Z0-9]{1}[a-zA-Z._0-9-]+\.[a-zA-Z]+[.]?[a-zA-Z]{0,3}$/; 
	return re.test(v); 
}

function showHideDelivery()
{
	var del = document.getElementById("fieldsetOtherAddress");
	
	if (document.getElementById("difDelivery").checked)
	{
		del.style.display = "block";
	}
	else
	{
		del.style.display = "none";
	}
}

function showHidePayment(idPaymentType)
{
	var credit = document.getElementById("creditCardFields");
	
	if (idPaymentType == 1)	{
		credit.style.display = "block";
	}
	else {
		credit.style.display = "none";
	}
	
}

function optionsDelivery(idCountry, from)
{
	var difDelivery = document.getElementById("difDelivery");
	
	if (from == 2 || (from == 1 && difDelivery.checked == false))
	{
		if (idCountry == 1)
		{
			document.getElementById("labelDeliveryTypeOutside").style.display = "none";
			document.getElementById("labelDeliveryTypeLocal").style.display = "block";
		}
		else
		{
			document.getElementById("labelDeliveryTypeLocal").style.display = "none";
			document.getElementById("labelDeliveryTypeOutside").style.display = "block";
		}
	}
}

function validateAllForm(idForm, fields)
{
	var frm = document.getElementById(idForm);
	
	var flds = fields.split(",");
	
	for (var i = 0; i < flds.length; i++)
	{
		var cmp = flds[i].split(".");
		var idField = cmp[0];
		var labelField = cmp[1];
		var option = cmp[2];
		
		if (document.getElementById(idField).value == "")
		{
			alert("Please, fill the " + labelField + " field");
			document.getElementById(idField).focus();
			return false;
		}
		
		if (option == "email")
		{
			if (!isEmail(document.getElementById(idField).value)) 
			{
				alert("Please, fill the " + labelField + " field correctly");
				document.getElementById(idField).focus();
				return false;
			}
		}
	}
	
	return true;
}

function validateFormSubscribe()
{
	if (!validateAllForm('formSubHome', 'firstName.First Name,lastName.Last Name,email.E-mail.email,captionCode.Enter Code'))
	{
		return false;
	}
	
	return true;
}

function redirect(url)
{
	window.location = url;	
}

function confirmAction(url, text)
{
	if (confirm(text))	
	{
		redirect(url);
	}	
}

function focusField(idField)
{
	document.getElementById(idField).focus();
}

function reloadPage(url)
{
	window.location = url;
}

function showHideField(idField, idCheckbox)
{
	if (document.getElementById(idCheckbox).checked == true)
		showField(idField);
	else
		hideField(idField);	
}

function showField(idField)
{
	document.getElementById(idField).style.display = "block";	
}

function hideField(idField)
{
	document.getElementById(idField).style.display = "none";	
}


