
/*
 * enforce number text input
 * use: onKeyPress="return numbersonly(event, false)" for integer
 *      onKeyPress="return numbersonly(event, true)" for decimal number
 */
function numbersonly(e, decimal) {
var key;
var keychar;

if (window.event) {
   key = window.event.keyCode;
}
else if (e) {
   key = e.which;
}
else {
   return true;
}
keychar = String.fromCharCode(key);

if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) ) {
   return true;
}
else if ((("0123456789").indexOf(keychar) > -1)) {
   return true;
}
else if (decimal && (keychar == ".")) { 
  return true;
}
else
   return false;
}

/**
 *  for text trim off space
 */
String.prototype.trim = function() { 
	return this.replace(/^\s+|\s+$/g,""); 
}

/**
 * this check email function is same as one page checkout's
 */
function checkEmail(emailStr) {
   if (emailStr.length == 0) {
       return true;
   }
   var emailPat=/^(.+)@(.+)$/;
   var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
   var validChars="\[^\\s" + specialChars + "\]";
   var quotedUser="(\"[^\"]*\")";
   var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})^[+]$/;
   var atom=validChars + '+';
   var word="(" + atom + "|" + quotedUser + ")";
   var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
   var domainPat=new RegExp("^([a-zA-Z\d\-][^+]+)(\.[a-zA-Z\d\-]+)[^+]+$");
   var matchArray=emailStr.match(emailPat);
   if (matchArray == null) {
       return false;
   }
   var user=matchArray[1];
   var domain=matchArray[2];
   if (user.match(userPat) == null) {
       return false;
   }  
   var IPArray = domain.match(ipDomainPat);
   if (IPArray != null) {
       for (var i = 1; i <= 4; i++) {
          if (IPArray[i] > 255) {
             return false;
          }
       }
       return true;
   }
   var domainArray=domain.match(domainPat);
   if (domainArray == null) {
       return false;
   }
   var atomPat=new RegExp(atom,"g");  
   var domArr=domain.match(atomPat);   
   var len=domArr.length;
   if ((domArr[domArr.length-1].length < 2) ||
       (domArr[domArr.length-1].length > 4)) {
       return false;
   }
   if (len < 2) {
       return false;
   }
   return true;
}

function updateVatSectionVisibility(el, infoEl, countryCodes) {
	selectedCountry = jQuery(el).val();

	if (!countryCodes) {
		countryCodes = [];
	}
	if (-1 < jQuery.inArray(selectedCountry, countryCodes)) {
		infoEl.show();
		if (jQuery.browser.msie) {
			infoEl.find('span').css('display', 'block');
			infoEl.find('input').css('display', 'block');
		}
	} else {
		infoEl.hide();
		infoEl.find('input').val("");
	}
}

/** This method will attach an onchange handler to the countrySelectId which
 * will check the selected value and determine whether or not it should display
 * another element on the page, specified by vatSectionId.
 * @param countrySelectId
 * @param vatSectionId
 * @return
 */
function setupCountryCombo(countrySelectId, vatSectionId, options) {
	jQuery('#' + vatSectionId).hide();

	var optionsHtml = '';
	if (options.defaultText) {
		optionsHtml += '<option value="">' + options.defaultText + '</option>';
	}

	if ('uk' == options.region) {
		optionsHtml += '<option value="GB">Great Britain (UK)</option>';
		optionsHtml += '<optgroup label="----------"></optgroup>';
	} else if ('us' == options.region) {
		optionsHtml += '<option value="US">United States</option>';
		optionsHtml += '<optgroup label="----------"></optgroup>';
	}
	jQuery('#' + countrySelectId).prepend(optionsHtml);

	jQuery('#' + countrySelectId).bind("change", function(e) {
		updateVatSectionVisibility(jQuery(this), jQuery('#' + vatSectionId), options.vatCountryCodes)
	});
}