///////////////////////////////////////////////////////////////
/* this is to check if the country selected is an EU country */
// will return true/  false 								///
///////////////////////////////////////////////////////////////
function chk_eu_country(obj) {
	var eucountries = new Array(
		'AUSTRIA',
		'BELGIUM',
		'BULGARIA',
		'CYPRUS',
		'CZECH REPUBLIC',
		'DENMARK',
		'ESTONIA',
		'FINLAND',
		'FRANCE',
		'GERMANY',
		'GREECE',
		'HUNGARY',
		'IRELAND',
		'ITALY',
		'LATVIA',
		'LITHUANIA',
		'LUXEMBOURG',
		'MALTA',
		'NETHERLANDS',
		'POLAND',
		'PORTUGAL',
		'ROMANIA',
		'SLOVAKIA',
		'SLOVENIA',
		'SPAIN',
		'SWEDEN',
		'UNITED KINGDOM'
								
	);
	
	country = obj.value;
	eucountry = false;	
	for(i in eucountries){
		if(country == eucountries[i]){
			eucountry = true;
			break;
		} 
	}

	return eucountry;
} //end function

//////////////////////////////////
/* show or hide the vat field *///
//////////////////////////////////
function toggleVat(is_eu) {
	if (is_eu) {
		toggleLayer('vatno', 'show');
		toggleLayer('vatnodesc', 'show');		
	}
	else {
		toggleLayer('vatno', 'hide');
		toggleLayer('vatnodesc', 'hide');		
	}
} // end function

///////////////////////////////////////////////////////////////////////////////////////
// this is to update the fee value when country changes, used in the enrolment forms //
// 
///////////////////////////////////////////////////////////////////////////////////////
function change_country(obj,coursefee) {

	is_eu = chk_eu_country(obj);
	//alert(coursefee);

	formObj = document.theform;
	//coursefee = formObj.course_fee.value; //use back the original fee
	numofseats = get_num_seats();
	fee = coursefee * numofseats;
	newfee = fee;
	if (is_eu) { //is euro country
		if (formObj.course_id.value != '') {
			newfee = fee * vatrate; //to append 17.5% VAT charges
		}
		country = formObj.country.value;
		if (country.toUpperCase() == 'UNITED KINGDOM') {
			newfee = fee * vatrate; //to append 17.5% VAT charges
		}
		
	} //end is eu
	else 
	{
		formObj.vatnumber.value=""; //need to reset the vat number field if not EU country
	}
	
	//set the value to fields
	fee = parseFloat(newfee).toFixed(2).toString();
	formObj.totalfee.value = fee;
	formObj.total_payment.value = fee;	
	//alert(fee);
	
	toggleVat(is_eu);
	//alert(fee);
} //end function


function get_num_seats() {
	formObj = document.theform;	
	num_of_seats = 1;
	if (formObj.num_of_seats) {
		num_of_seats = Number(formObj.num_of_seats.value);
	} 
	else {
		num_of_seats = 1;
	} //end if
			
	if (num_of_seats <= 0 ) {
		num_of_seats = 1;
	}
	
	return num_of_seats;
}

/////////////////////////////////////////////////////////////////////////////////
/* this is to update the fees when course changes, used in the enrolment forms */
/////////////////////////////////////////////////////////////////////////////////
function change_total_payment()
{
	//alert ("in total payment");
	//document.theform.submit();
	formObj = document.theform;
	is_eu = chk_eu_country(formObj.country);	
	coursefee = formObj.course_fee.value;
	
	num_of_seats = get_num_seats();
	if (is_eu) {	
		totalfee = coursefee * num_of_seats * vatrate;
		
	}
	else {
		totalfee = coursefee * num_of_seats ;
	}
	
	fee = parseFloat(totalfee).toFixed(2).toString();
	formObj.totalfee.value = fee;
	formObj.total_payment.value = fee;	

} //end function 



/*
function write_course_id()
{
	interm = formObj.course_id.value;
	formObj.course_id_retained.value = interm;
}
*/


//////////////////////////////////////////////
/* this is to hide/unhide for the vat field */
//////////////////////////////////////////////
function check_country() {
	formObj = document.theform;
	is_eu = chk_eu_country(formObj.country);
	if (!is_eu) {
		if (formObj.vatnumber) {
			formObj.vatnumber.value="";
		}
	}
	toggleVat(is_eu);
}

/////////////////////////////////////////////////////////////////////////////////
// this is to check VAT numbers			
// - called onChange on the vatnumber field
/////////////////////////////////////////////////////////////////////////////////
function check_vat(obj,fee) {
	formObj = document.theform;
	numofseats = get_num_seats();
	if(fee==null)fee =formObj.course_fee.value;
	coursefee = fee * numofseats;
	if (obj.value!='')  {
		if (country.toUpperCase() == 'UNITED KINGDOM') {
			coursefee = coursefee * vatrate; //to append 17.5% VAT charges
		} 
	}
	else
	{
		coursefee = coursefee* vatrate
	}
	coursefee = parseFloat(coursefee).toFixed(2).toString();
	formObj.totalfee.value = coursefee;
	formObj.total_payment.value = coursefee;
	country = formObj.country.value;
}

function check_email(emailAddr)
{
	//emailAddr =
	i = emailAddr.indexOf("@");
	if (i == -1) {
		return false;
	}
	var username = emailAddr.substring(0, i);
	var domain = emailAddr.substring(i + 1, emailAddr.length);
	// look for spaces at the beginning of the username
	i = 0;
	while ((username.substring(i, i + 1) == " ") && 
	(i < username.length)) {
		i++;
	}
	// remove any found
	if (i > 0) {
		username = username.substring(i, username.length);
	}
	
	// look for spaces at the end of the domain
	i = domain.length - 1;
	while ((domain.substring(i, i + 1) == " ") && (i >= 0)) {
		i--;
	}
	// remove any found
	if (i < (domain.length - 1)) {
		domain = domain.substring(0, i + 1);
	}
	if(domain.indexOf(".")==-1) return false;
	// check for bad characters in the username
	var ch;
	for (i = 0; i < username.length; i++) {
		ch = (username.substring(i, i + 1)).toLowerCase();
		if (!(((ch >= "a") && (ch <= "z")) || 
			((ch >= "0") && (ch <= "9")) ||
			(ch == "_") || (ch == "-") || (ch == "."))) {
				return false;
		}
	}
	
	// check for bad characters in the domain
	for (i = 0; i < domain.length; i++) {
		ch = (domain.substring(i, i + 1)).toLowerCase();
		if (!(((ch >= "a") && (ch <= "z")) || 
			((ch >= "0") && (ch <= "9")) ||
			(ch == "_") || (ch == "-") || (ch == "."))) {
				return false;
		}
	}
		return true;
}

function change_course() {
	formObj = document.theform;
	formObj.country.value = formObj.country.value;
	check_country();
	formObj.submit();
}


function resubmit()
{
	document.forms[0].submit();
}

function isDate(dateStr,alert_true) 
{
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) 
	{
		if(alert_true)alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
		return false;
	}
	day = matchArray[1];
	month = matchArray[3]; // p@rse date into variables
	year = matchArray[5];
	if (month < 1 || month > 12) 
	{ // check month range
		if(alert_true)alert("Month must be between 1 and 12.");
		return false;
	}
	if (day < 1 || day > 31) 
	{
		if(alert_true)alert("Day must be between 1 and 31.");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) 
	{
		//alert("Month "+month+" doesn`t have 31 days!")
		return false;
	}
	if (month == 2) 
	{ // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) 
		{
		if(alert_true)alert("February " + year + " doesn`t have " + day + " days!");
		return false;
		}
	}
	return true; // date is valid
}
function convertDate(date,format,outputformat)
{
		//Accepts only 'dd-mm-yyyy' as input
	if(format==undefined) format='-';
	if(outputformat==undefined) outputformat='-';
	date_array = date.split(format);
	newDate =date_array[1]+outputformat+date_array[0]+outputformat+date_array[2];
	//alert(newDate);
	return newDate;
}
function check_number(x)
{
	x = x.replace('.','');
	x =x.replace(' ','');
	alert(x);
	if(!isNaN(x)){ alert("true");}
	return false;
}
