function in_array(stringToSearch, arrayToSearch) {
	for (s = 0; s < arrayToSearch.length; s++) {
		thisEntry = arrayToSearch[s].toString();
		if (thisEntry == stringToSearch) {
			return true;
		}
	}
	return false;
}

function clearMe(thefield){
	if (thefield.defaultValue==thefield.value) {
		thefield.value = "";
	}
} 

function pop(ShowPage, intwidth, intheight) {

	var leftpos = (screen.width - intwidth) / 2;
	var toppos = (screen.height - intheight) / 2;
	var winprops = 'toolbar=0, scrollbars=1, location=0, statusbars=0, menubar=0, width=' + intwidth + ', height=' + intheight + ', left=' + leftpos + ', top=' + toppos + ', resizable=1';
    var popupwindow = window.open(ShowPage, 'poppage', winprops);

}

function toggleEnableField(myForm) {

	foo = myForm.send_invoice.checked;
	
	if (foo) {
		myForm.invoice_name.disabled=false;
		myForm.invoice_name.style.backgroundColor='#fff';
	} else {
		myForm.invoice_name.disabled=true;
		myForm.invoice_name.value='';
		myForm.invoice_name.style.backgroundColor='#eee';
	}
	
}

function duplicateAddresses(myForm) {

	myForm.billing_address1.value = myForm.postal_address1.value;
	myForm.billing_address2.value = myForm.postal_address2.value;
	myForm.billing_town.value = myForm.postal_town.value;
	myForm.billing_city.value = myForm.postal_city.value;
	myForm.billing_postcode.value = myForm.postal_postcode.value;
	myForm.billing_country.selectedIndex = myForm.postal_country.selectedIndex;


}

// toggle visibility

function toggle(targetID) {
	if (document.getElementById) {
		target = document.getElementById(targetID);
		if (target.style.display == "none") {
			target.style.display = "";
		} else {
			target.style.display = "none";
		}
	}
}

function toggleItem(targetID, target2ID) {
	if (document.getElementById) {
		target = document.getElementById(targetID);
		target2 = document.getElementById(target2ID);
		if (target.style.display == "none") {
			target.style.display = "";
			target2.style.backgroundImage = "url('../images/triangle-open.gif')";
		} else {
			target.style.display = "none";
			target2.style.backgroundImage = "url('../images/triangle-close.gif')";
		}
	}
}

function newImage(arg) {	
	if (document.images) {		
		rslt = new Image();		
		rslt.src = arg;		
		return rslt;	
	}
}

function changeImages() {	
	if (document.images && (preloadFlag == true)) {		
		for (var i=0; i<changeImages.arguments.length; i+=2) {			
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];		
		}	
	}
}

var preloadFlag = false;

function preloadImages() {	
	if (document.images) {		
		img_1 = newImage("../images/donate_button_on.gif");
		preloadFlag = true;
	}
}
	
function goJump(which_form) {
	self.location.href=which_form.jump.options[which_form.jump.selectedIndex].value;
}

function selectJump(which_form) {
	self.location.href=which_form.jump.options[which_form.jump.selectedIndex].value;
}

function validateNumber(field, msg, min, max) {
	if (!min) { min = 0 }
	if (!max) { max = 255 }
	if ( (parseInt(field.value) != field.value) || field.value.length < min || field.value.length > max) {
		alert(msg);
		field.focus();
		field.select();
		return false;
	}
	return true;
}

function validateString(field, msg, min, max) {
	if (!min) { min = 1 }
	if (!max) { max = 65535 }
	if (!field.value || field.value.length < min || field.value.max > max) {
		alert(msg);
		field.focus();
		field.select();
		return false;
	}
	return true;
}

function validateEmail(email, msg, optional) {
	if (!email.value && optional) {
		return true;
	}

	var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
	
	if (!re_mail.test(email.value)) {
		alert(msg);
		email.focus();
		email.select();
		return false;
	}
	return true;
}

function validateList(listname, msg) {

	var foo = listname.selectedIndex;

	if (foo == 0 || foo == -1) {
		alert(msg);
		return false;
	}
	
	return true;
	
}

function validateSelect(listID, msg) {
	if (document.getElementById(listID)) {
		var list = document.getElementById(listID);
		var listValue = list.value;
		if (listValue == 0 || listValue == '') {
			alert(msg);
			return false;
		}
	}
	return true;
}

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) {
	// cross-browser event handling for IE5+, NS6 and Mozilla 
	// By Scott Andrew 
	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 findTarget(e) {
	// cross-browser function to find the event target
	var target;
	if (window.event && window.event.srcElement) {
		// IE does it differently... stores the event in a window.event object
		target = window.event.srcElement;
	}
	if (e && e.target) {
		target = e.target;
	}
	return target;
}

/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
*/

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

/*
function attachCurrencyFunction() {
	if (document.getElementById('select_currency')) {
		var el = document.getElementById('select_currency');
		// attach onclick function
		addEvent(el, 'change', changeCurrency, false);
	}
}
*/

function changeCurrency() {
	if (document.getElementById('select_currency') && document.getElementById('input_ref')) {
		var el = document.getElementById('select_currency');
		var ref = document.getElementById('input_ref');
		
		var path = '/inc/set_currency.php?currency=' + el.value + '&ref=' + ref.value;
		
		
		var querystring = document.getElementById('input_querystring').value;
		if (querystring.length > 0) {
			path += '&querystring=' + querystring;
		}
		
		//alert(path);
		window.location.href= path;
	}
}

function createAjaxObject() {
	var ro;
	var browser = navigator.appName;
	if (browser == "Microsoft Internet Explorer") {
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		ro = new XMLHttpRequest();
	}
	return ro;
}

function checkDiscountCode() {

	// get the discount code value
	// get the course id
	// pass them to the PHP file
	// update discount_code_cell with class (code_success, or code fail)
	
	// get the discount code value
	var discount_code = document.getElementById('discount_code').value;
	
	if (discount_code.length == 0) {
		alert('Please enter the Special Rate Code.');
		document.getElementById('discount_code').focus();
		return false;
	}
	
	// get the course id
	var course_id = document.getElementById('course_id').value;
	// build the GET URL to the PHP file
	var url = '/earfound2/inc/check_discount_code.php?course=' + escape(course_id) + '&code=' + escape(discount_code);
	doAjaxRequest(url);
	
	function doAjaxRequest(url) {	
		// setup the ajax object, define the response handler and send the data
		http = createAjaxObject();
		http.open('GET', url);
		http.onreadystatechange = handleResponse;
		http.send(null);
	}
	
	function handleResponse() {
		if (http.readyState == 4) {
			var response = http.responseText;
			var cell = document.getElementById('discount_code_cell');
			if (response == '1') {
				// set class to cell
				cell.className = 'discount_code_success';
				alert('This is the correct Special Rate Code.');
			} else {
				cell.className = 'discount_code_fail';
				alert('Sorry, the Special Rate Code you have entered is incorrect.');
			}
		}
	}
}

/* addLoadEvent(attachCurrencyFunction); */