function externaliseLinks() { 
	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"; 
		}
	} 
} 

function PostBack(e, objField, objButton) {
	if (e.which || e.keyCode) {
		if ((e.which == 13) || (e.keyCode == 13)) {
			objField.blur();
			document.getElementById(objButton).click();
			return false;
		}
	} 
	else {
		return true;
	}
}

function IsNumericKey(e) {
  if( !e ) {
    if( window.event ) {
      e = window.event;
    } else {
      return;
    }
  }
  
  var num = 0;
  
  if( typeof( e.keyCode ) == 'number') {
    //DOM
    num = e.keyCode;
  } else if( typeof( e.which ) == 'number') {
    //NS 4 compatible
    num = e.which;
  } else if( typeof( e.charCode ) == 'number') {
    //also NS 6+, Mozilla 0.9+
    num = e.charCode;
  }
  
  return ((num>=48&&num<=57) || (num>=96&&num<=105));
}

function IsAlpha(e) {
  if( !e ) {
    if( window.event ) {
      e = window.event;
    } else {
      return;
    }
  }
  
  var num = 0;
  
  if( typeof( e.keyCode ) == 'number') {
    //DOM
    num = e.keyCode;
  } else if( typeof( e.which ) == 'number') {
    //NS 4 compatible
    num = e.which;
  } else if( typeof( e.charCode ) == 'number') {
    //also NS 6+, Mozilla 0.9+
    num = e.charCode;
  }
  
  return (num>=58&&num<=95);
}

function setListeners() {
	inputList = document.getElementsByTagName("INPUT");
	for(i=0;i<inputList.length;i++){
		inputList[i].onpropertychange = restoreStyles;
		inputList[i].style.backgroundColor = "";
	}
	
	selectList = document.getElementsByTagName("SELECT");
	for(i=0;i<selectList.length;i++){
		selectList[i].onpropertychange = restoreStyles;
		selectList[i].style.backgroundColor = "";
	}
}

function restoreStyles(){
	if(event.srcElement.style.backgroundColor != "") {
		event.srcElement.style.backgroundColor = "";
	}
}

function initFileUpload(input) {
	var objFileUpload = $(input);
	var x = document.getElementsByTagName('input');
	
	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'file') continue;
		if (x[i].parentNode.className != 'fileinputs') continue;

		var clone = objFileUpload.cloneNode(true);
		//x[i].parentNode.appendChild(clone);
		x[i].relatedElement = x[i].next().getElementsByTagName('input')[0];
		x[i].onchange = x[i].onmouseout = x[i].onkeyup = function () {
			var sLastSlash = this.value.lastIndexOf("\\");
			if (sLastSlash != -1) {
				this.relatedElement.value = this.value.substring(sLastSlash+1, this.value.length);
			}
			else {
				this.relatedElement.value = this.value;
			}
		}
	}
}

function load() {
	dropOtherValidator();
	externaliseLinks();
	setListeners();
}

var otherHidden;
var otherShow;

function dropOtherValidator() {
	if (typeof($('contact')) != "undefined") {
		otherHidden = $('contact').getElementsByClassName('hidden');


		var pageValidatorsNew = [];
		if ((typeof(Page_Validators) != "undefined") && (Page_Validators != null)) {
			for (var b = 0; b < Page_Validators.length; b++) {
				if (Page_Validators[b].id != '_ctl2_rfvOther') {
					pageValidatorsNew.push(Page_Validators[b]);
				}
			}
			Page_Validators = pageValidatorsNew;
		}
	}
}


function showOtherField(dropdown) {
	var selectedValue = dropdown.options[dropdown.selectedIndex].value;
	var otherShow = $('contact').getElementsByClassName('show');
	var pageValidatorsNew = [];
	var otherExists = false;
	
	if ((selectedValue == 'Other search engine') || (selectedValue == 'Google') || (selectedValue == 'Yahoo') || (selectedValue == 'MSN')) {
		$(otherHidden[1]).removeClassName('hidden');
		$(otherHidden[1]).addClassName('show');
	}
	else if (selectedValue == 'Another Website') {
		$(otherHidden[0]).removeClassName('hidden');
		$(otherHidden[0]).addClassName('show');
	}
	else {
		for (var a = 0; a < otherShow.length; a++) {
			$(otherShow[a]).removeClassName('show');
			$(otherShow[a]).addClassName('hidden');
		}
	}
}

window.onload = load;

