var conference_cost = 175;
var saturday_only_cost = 100;
var saturday_break_fast_cost = 25;
var saturday_lunch_cost = 30;
var saturday_dinner_cost = 50;
var annual_membership_cost = 30;

function $()
{
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++)
	{
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

function getHTTPReq(url,destinationid)
{
	url += '?sid='+Math.random();
	var xmlhttp = false;
	if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
	else if (window.ActiveXObject) { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
	if (xmlhttp)
	{
		xmlhttp.open("GET", url, true);
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState==4)
			{
				if (xmlhttp.status==200) { $(destinationid).innerHTML = xmlhttp.responseText; }
				else { $(destinationid).innerHTML = 'Fail'; }
			}
		}
		if (window.XMLHttpRequest) { xmlhttp.send(null); }
		else if (window.ActiveXObject) { xmlhttp.send(); }
	}
}

function getInsideWindowWidth()
{
	var intW = 0;
	if(self.innerWidth)
	{
		intW = window.innerWidth;
	} 
	else
	{
		if(document.documentElement && document.documentElement.clientWidth)
		{
			intW = document.documentElement.clientWidth;
		}
		else
		{
			if(document.body && document.body.clientWidth)
			{
				intW = document.body.clientWidth;
			}
		}
	}
	var adj = (navigator.userAgent.indexOf('MSIE')!=-1) ? 24 : 0;
	return intW+adj;
}

function shiftTo(num)
{
	var i;
	for(i=1; i<6; i++)
	{
		$('box'+i).style.display='none';
	}
	$('box'+num).style.display='inline';
}

var total = 0;
var fErrors = new Array ()
fErrors[0] = 'Name is required';
fErrors[1] = 'Address is required';
fErrors[2] = 'Town/city is required';
fErrors[3] = 'State is required';
fErrors[4] = 'Zip code is required';
fErrors[5] = 'Wrong zip code';
fErrors[6] = 'Email is required';
fErrors[7] = 'There is a type error on your email address';
fErrors[8] = 'Please type cardholder\'s name';
fErrors[10] = 'No valid date provided';
fErrors[11] = 'Credit card is expired';
fErrors[12] = 'No card number provided';
fErrors[13] = 'Credit card number is in invalid format';
fErrors[14] = 'Credit card number is invalid';
fErrors[15] = 'Please select your credit card';
fErrors[16] = 'Please select number of attendee';
fErrors[17] = 'Occupation is required';

function validateRegister()
{
	calculatePayment();
	if(total == 0 || ($('number_of_registrants').value=='0' && $('saturday_only_conference_registration').value=='0' && $('saturday_breakfast').value=='0' && $('saturday_lunch').value=='0' && $('saturday_dinner_and_auction').value=='0')) { alert('Please select at least 1 event'); shiftTo(1); $('number_of_registrants').focus(); return false; }
	
	var name = $('name').value;
	if (name == '') { alert(fErrors[0]); shiftTo(5); $('name').focus(); return false; }
	var address = $('address').value;
	if (address == '') { alert(fErrors[1]) ; shiftTo(5); $('address').focus(); return false; }
	var city = $('city').value;
	if (city == '') { alert(fErrors[2]); shiftTo(5); $('city').focus(); return false; }
	var state = $('state').value;
	if (state == '') { alert(fErrors[3]); shiftTo(5); $('state').focus(); return false; }
	var zip_code = $('zip_code').value;
	if (zip_code == '')
	{
		alert(fErrors[4]); shiftTo(5); $('zip_code').focus(); return false;
	}
	else
	{
		if(zip_code.length != 5) { alert(fErrors[5]); shiftTo(5); $('zip_code').focus(); return false; }
		if(isNaN(zip_code)) { alert(fErrors[5]); shiftTo(5); $('zip_code').focus(); return false; }
	}
	var email = $('email_address').value;
	if (email == '') { alert(fErrors[6]); shiftTo(5); $('email_address').focus(); return false; }
	if (email.search(/@/) == -1) { alert(fErrors[7]); shiftTo(5); $('email_address').focus(); return false; }
	if (email.search(/\./) == -1) { alert(fErrors[7]); shiftTo(5); $('email_address').focus(); return false; }
	if (email!= '')
	{
		var emailArray = email.split('.');
		if (emailArray[1].length < 2) { alert(fErrors[7]); shiftTo(5); $('email_address').focus(); return false; }
		if (emailArray[2] != undefined) { if(emailArray[2].length < 2) { alert(fErrors[7]); shiftTo(5); $('email_address').focus(); return false; }}
	}
	var occupation = $('occupation').value;
	if (occupation == '') { alert(fErrors[17]); shiftTo(5); $('occupation').focus(); return false; }
	var type_of_card = $('type_of_card').value;
	var card_number = $('card_number').value;
	if (card_number == '') { alert(fErrors[12]); shiftTo(5); $('card_number').focus(); return false; }
	if(type_of_card == '')
	{
		alert(fErrors[15]); shiftTo(5); $('type_of_card').focus(); return false;
	}
	else
	{
		if(type_of_card == 'VISA')
		{
			if(card_number.length != 13 && card_number.length != 16)
			{
				alert(fErrors[13]); shiftTo(5); $('card_number').focus(); return false;
			}
			else
			{
				if(card_number.substring(0,1) != '4')
				{
					alert(fErrors[14]); shiftTo(5); $('card_number').focus(); return false;
				}
			}
		}
		else if (type_of_card == 'MASTERCARD')
		{
			if(card_number.length != 16)
			{
				alert(fErrors[13]); shiftTo(5); $('card_number').focus(); return false;
			}
			else
			{
				var prefix = card_number.substring(0,2);
				if(prefix != '51' && prefix != '52' && prefix != '53' && prefix != '54' && prefix != '55')
				{
					alert(fErrors[14]); shiftTo(5); $('card_number').focus(); return false;
				}
			}
		}
	}
	var expiration = $('expiration_year').value+$('expiration_month').value;
	if (expiration.length <= 4)
	{
		alert(fErrors[10]); shiftTo(5); $('expiration_year').focus(); return false;
	}
	else
	{
		var now = new Date();
		var thisyear = now.getYear();
		thisyear = (thisyear <= 1900) ? 1900+thisyear : thisyear;
		var thismonth = now.getMonth()+1;
		dt1 = Date.UTC($('expiration_year').value,$('expiration_month').value,1);
		dt2 = Date.UTC(thisyear,thismonth,1);
		if ($('expiration_month').value == '' || $('expiration_year').value == '')
		{
			alert(fErrors[10]); shiftTo(5); $('expiration_year').focus(); return false;
		}
		else
		{
			if(dt1<dt2)
			{
				alert(fErrors[11]); shiftTo(5); $('expiration_year').focus(); return false;
			}
		}
	}
	return true;
}

function cleanCard()
{
	var card_number = $('card_number').value;
	card_number = card_number.replace(/ /g, '');
	card_number = card_number.replace(/-/g, '');
	$('card_number').value = card_number;
}

function is_numeric(value)
{
	return value!='' && !isNaN(value);
}

function copyName()
{
	var name = $('name').value;
	$('cardholders_name').value=name;
}

function calculatePayment()
{
	total = 0;
	var number_of_registrants = $('number_of_registrants').value;
	var annual_memberships = $('annual_memberships').value;
	var contribution = $('contribution').value;
	var saturday_breakfast = $('saturday_breakfast').value;
	var saturday_lunch = $('saturday_lunch').value;
	var saturday_dinner_and_auction = $('saturday_dinner_and_auction').value;
	var saturday_only_conference_registration = $('saturday_only_conference_registration').value;
	if(is_numeric(number_of_registrants)) { total += number_of_registrants * conference_cost; } else { $('number_of_registrants').value=''; }
	if(is_numeric(annual_memberships)) { total += annual_memberships * annual_membership_cost; } else { $('annual_memberships').value=''; }
	if(is_numeric(contribution)) { total += parseFloat(contribution); } else { $('contribution').value=''; }
	if(is_numeric(saturday_breakfast)) { total += saturday_breakfast * saturday_break_fast_cost; } else { $('saturday_breakfast').value=''; }
	if(is_numeric(saturday_lunch)) { total += saturday_lunch * saturday_lunch_cost; } else { $('saturday_lunch').value=''; }
	if(is_numeric(saturday_dinner_and_auction)) { total += saturday_dinner_and_auction * saturday_dinner_cost; } else { $('saturday_dinner_and_auction').value=''; }
	if(is_numeric(saturday_only_conference_registration)) { total += saturday_only_conference_registration * saturday_only_cost; } else { $('saturday_only_conference_registration').value=''; }
	$('total').value = total;
	$('stotal').innerHTML = total;
}

var curnum = new Array();
function createAttendeeBox(label, fieldname, id, parent, num)
{
	var table = $(id);
	curnum[id] = (!curnum[id] || curnum[id] == '') ? 0 : parseInt(curnum[id]);
	if(num != '' && num > 0)
	{
		var i;
		var j;
		var r;
		var c;

		if(num < curnum[id])
		{
			for(i = curnum[id]; i > num ; i--)
			{
				table.deleteRow(i);
			}
		}
		else
		{
			for(i = curnum[id]+1; i <= num; i++)
			{
				r = table.insertRow(i);
				c = r.insertCell(0);
				c.setAttribute('width', '140');
				c.innerHTML = label+' '+i+' Name';
				c = r.insertCell(1);
				s = '<input type="text" size="30" name="'+fieldname+'[]" id="'+fieldname+i+'" value="" \/>';
				c.innerHTML = s;
			}
		}
		curnum[id] = num;
	}
	else
	{
		$(parent).value='0';
		if(curnum[id] > 0)
		{
			for(i = curnum[id]; i > 0; i--)
			{
				table.deleteRow(i);
			}
			curnum[id] = 0;
		}
	}
}

function setAttendee1()
{
	$('attendee1').value=$('name').value;
}

function setLeft()
{
	var lf = ((getInsideWindowWidth() - 728)/2 - 378);
	$('dwouter').style.left=lf+'px';
}

function drawForm()
{
	var loading = document.createElement('div');
	loading.style.position='absolute';
	loading.style.left='-99999px';
	loading.style.top='-99999px';
	document.body.appendChild(loading);
	loading.innerHTML = '<img src="inc/loading.gif" alt="Loading ..." \/>';
	if(!$('dwouter'))
	{
		var dwouter = document.createElement("div");
		dwouter.setAttribute('id', 'dwouter');
		document.body.appendChild(dwouter);
	}
	var lf = ((getInsideWindowWidth() - 728)/2 - 378);
	$('dwouter').style.zIndex='100';
	$('dwouter').style.position='absolute';
	$('dwouter').style.top='360px';
	$('dwouter').style.left=lf+'px';
	$('dwouter').innerHTML = '<img src="inc/loading.gif" alt="Loading ..." \/>';
	
	getHTTPReq('inc/form.html', 'dwouter');
}

function addComponents()
{
	var csscomponent = 'regcss';
	var objHead = document.getElementsByTagName("head");
	if (objHead[0])
	{
		try {
			if (!$(csscomponent))
			{
				var objCSS = document.createElement("link");
				objHead[0].appendChild(objCSS);
				objCSS.setAttribute("id", csscomponent);
				objCSS.setAttribute("rel", "stylesheet");
				objCSS.setAttribute("href", "inc/registration.css");
				objCSS.setAttribute("type", "text/css");
			}
		}
		catch(e)
		{
			setTimeout("addComponents()", 10);
		}
	}
}

addComponents();
window.onload = drawForm;
window.onresize = setLeft;
