var searchArray = function(arr, key) {
	var isFound = false;
	var i;

	for (i = 0; i < arr.length; i++) {
	  if (arr[i] == key) {
	    isFound = true;
	  }
	}

	return isFound;
};

var nsDomains = ['www.netsuite.com.au','www.netsuite.com.sg','www.netsuite.com.hk','www.netsuite.co.jp','www.netsuite.com'
				,'www.netsuite.co.uk','www.netsuite.eu'];
var domain = document.domain;
if(searchArray(nsDomains,domain)){
	function getParameterByName(name) {
		name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
		var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'),
			results = regex.exec(location.search);
		return results == null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
	} 
	//console.log('au');
	window.onload = function(e){
		var a = document.getElementsByTagName('a');
		var i, href;
		var source = getParameterByName('source');

		for(i=0;i<a.length; i++){
			href = a[i].href;

			if((href.indexOf('go-netsuite-com') > -1) || (href.indexOf('go.netsuite.com') > -1)){
				//check href for 'leadsource' 
				//console.log(href);
				if(href.indexOf('leadsource') > -1){
					//console.log('leadsource found');
					var temp = '';
					var lsParam = href.split('&');
					var j;
					//console.log("lsParm:" + lsParam);
					for(j=0;j<lsParam.length;j++){
						if(lsParam[j].indexOf('?') > -1){ //subsrting includes domain
							temp = lsParam[j];
						}else{
							if(lsParam[j].indexOf('leadsource') == -1){ //temp already has atleast 1 param
								//console.log('temp: '+temp);
								temp = temp + '&' + lsParam[j];
							}
						}
					}

					a[i].href = temp;
				}

				if(source != ''){
					//append source
					//console.log('append source');
					href = a[i].href + '&source=' + source;

					a[i].href = href;
				}
			}
		}
	};
}