var e;

function checkEmail(email)

{
	return email.match(new RegExp("^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$")) !=null;
}

function DaysInMonth(year, month)
{
 var days = [31,28,31,30,31,30,31,31,30,31,30,31];
 if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) days[1]=29;
 return days[month];
}

function parseDate(text)
{
	var iaDate = new Array(3);
	var year, month, day;
	if (text=="mm/dd/yyyy" ||text=='') return null;
	var SplitChar ="/";
	iaDate = text.split(SplitChar);
	if (iaDate.length != 3) 
	{
		SplitChar ="-";
		iaDate =text.split(SplitChar);
	}
	if(iaDate.length !=3) 
	{
		SplitChar ="/";
		len = text.length;
		if(len==1 ||len==2 || len==4 || len==6 || len==8)
		{
			if(len==1 ||len ==2)
			{
		  		var today = new Date();
		  		iaDate[0] =today.getFullYear();
		  		iaDate[1] = today.getMonth()+1;	
		  		iaDate[2] = text.substring(0,len);
			}
			else if(len==4)
			{
				var today = new Date();
				iaDate[0] =today.getFullYear().toString();
				iaDate[1] = text.substring(0,2);
				iaDate[2] = text.substring(2,4);
			}
			else if(len==6 || len==8)
			{
				pos = len==6 ? 2 : 4;
				iaDate[0] = text.substring(0,pos);
				iaDate[1] = text.substring(pos,pos+2);
				pos+=2;
				iaDate[2] = text.substring(pos,pos+2);
			}
			text =iaDate[0]+SplitChar +iaDate[1] +SplitChar+iaDate[2];
		}
		else 
			return null;
	}
	if ((iaDate[0].length > 4 || iaDate[0].length ==0) || 
		(iaDate[1].length > 2 || iaDate[1].length ==0) || 
		(iaDate[2].length > 2 || iaDate[2].length ==0 )) return null;
	var m = text.match(new RegExp("^\\s*((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})\\s*$"));
	if(m == null ) 
		return null;
	year = parseFloat(iaDate[0]);
	if(year<100 && year>0) year +=2000;
	month = parseFloat(iaDate[1]);
	day=parseFloat(iaDate[2]);	
	if (year < 1900 || year > 2900) return null;
	if (month < 1 || month > 12) return null;
	if (day < 1 || day > DaysInMonth(year, month-1)) 
	return null;
	return year.toString()+ 
	SplitChar+ (month <10 ? "0"+month.toString() :month.toString()) + 
	SplitChar + (day <10 ? "0"+day.toString() :day.toString());
}

function checkNotNull(IdList, IdListMessage)
{
	var Len = IdList.length;
	for(var i=0;i<Len;i++)
	{
		e = document.getElementById(IdList[i]);
		if(!e || e.value.length==0)
		{
			alert("please input "+IdListMessage[i]+".");
			e.focus();
			return false;
		}
	}
	return true;
}

function checkConditionNull(condition, IdList, IdListMessage)
{
	if(condition)
	{
		return checkNotNull(IdList, IdListMessage);
	}
	return true;
}

function checkDateFormat(dateList)
{
	var Len = dateList.length;
	for(var i=0;i<Len;i++)
	{
		e = getObject(dateList[i]);
		var date = parseDate(e.value);
		if(date == null)
		{
 			alert("please input correct date format for "+dateList[i]+".");
			e.focus();
			return false;
		}
		e.value = date.toString();
	}
	return true;
}

function getObject(Id)
{
	return document.getElementById(Id);
}

function checkInput() 
{ 
	var t_str ="";
	var flag = false;
	e = getObject("g_t1");
	if(e&&e.checked)
	{
		flag = true;
		t_str += e.value;
	}
	e = getObject("g_t2");
	if(e&&e.checked) 
	{
		if(flag)
			t_str +=",";
		t_str += e.value;
	}
	if(t_str=="")
		t_str ="null";
		getObject("Title").value = t_str;
var IdList = ["FamilyName", "GivenName","Company"];
var IdListMessage = ["family name", "given Name","company or Institution"];

if(!checkNotNull(IdList, IdListMessage)) return false;
 e = getObject("EMail");	
 if(!checkEmail(e.value))
 {
 	alert("please check your email format!");
	e.focus();
	return false;
 }
IdList = ["PaperTitle", "FirstAuthor"];
IdListMessage = ["paper title for your delivered paper", "first author of your delivered paper"];
if(!checkConditionNull(getObject("deliverpaper1").checked, IdList, IdListMessage)) return false;
IdList = ["HotelName", "HotelPrice", "Arrivaldate", "Departuredate"];
IdListMessage = ["hotel name", "hotel approximate price", "arrival date", "departure date"];
var hotelBook =getObject("nextPageChk").checked && getObject("hotelChk").checked;
if(!checkConditionNull(hotelBook, IdList, IdListMessage)) return false;
return true; 
}

function switchEnabled(flag)
{
	document.getElementById("PaperTitle").disabled = !flag;
	document.getElementById("FirstAuthor").disabled = !flag;
	document.getElementById("SecondAuthor").disabled = !flag;
	document.getElementById("ss1").disabled = !flag;
	document.getElementById("ss2").disabled = !flag;
	document.getElementById("ss3").disabled = !flag;
}

function getParent(e, tag)
{
	while(e!=null && e.tagName!=tag) e = e.parentNode;
	return e;
}

function getText(e)
{
	return e.innerText ? e.innerText :e.textContent;
}

function setText(e,value)
{
	if(e.innerText)
		e.innerText = value;
	else
		e.textContent = value;
}

function __calcGroupPay(obj)
{
	var PayTable = document.getElementById("PayTable");
	var this_v = parseFloat(getText(getParent(obj, "TR").cells[1]));
	return parseFloat(obj.options[obj.selectedIndex].value) * this_v;
}

function calcPay(obj)
{
	var PayTable = document.getElementById("PayTable");
	var this_v = parseFloat(getText(getParent(obj, "TR").cells[1]));
	var this_sum = parseFloat(obj.options[obj.selectedIndex].value) * this_v;
	setText(getParent(obj, "TR").cells[3], this_sum);
	var this_sum = __calcGroupPay(obj);
	setText(getParent(obj, "TR").cells[3], this_sum);
	//set total
	var sum =0;
	for(var i=1;i <= 6;i++)
	{
		sum += __calcGroupPay(document.getElementById("Pay"+i+"Num"));
	}
	document.getElementById("PayTotal").value = sum;
}

function button_selected_is(button_name)
  {
    document.getElementById("button_selected").value=button_name;
  }