// JavaScript Document
<!--
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->


function checkUsernameForm(ch){
	var len, digit;
	if(ch == " "){ 
		len=0;
	}else{
		len = ch.length;
	}
	if (len == 0)
		return false;
		
	for(var i=0 ; i<len ; i++)
	{
		digit = ch.charAt(i)
		//if( (digit >= "A" && digit <= "Z") || (digit >= "a" && digit <= "z") || (digit >="0" && digit <="9") || (digit == "_") || (digit == "-") ){
		if( (digit >= "A" && digit <= "Z") || (digit >= "a" && digit <= "z") || (digit >="0" && digit <="9") || (digit == "_") || (digit == "-") ){
		; 
		}else{
			return false; 
		} 
	}  // end for
	
	return true;
}// end function

function checkNumber(ch){
	var len, digit;
	if(ch == " "){ 
		len=0;
	}else{
		len = ch.length;
	}
	if (len == 0)
		return true;
		
	for(var i=0 ; i<len ; i++)
	{
		digit = ch.charAt(i)
		if( (digit >= "0" && digit <= "9") || (digit >="๑" && digit <="๙") ){
			return true;
		}else{
			; 
		} 
	}  // end for
	
	return false;
}// end function

function checkNumber2(ch){
	var len, digit;
	if(ch == " "){ 
		len=0;
	}else{
		len = ch.length;
	}
	if (len == 0)
		return true;
		
	for(var i=0 ; i<len ; i++)
	{
		digit = ch.charAt(i)
		if( (digit >= "0") && (digit <= "9") ){
			return true;
		}else{
			; 
		} 
	}  // end for
	
	return false;
}// end function

function checkMemberLogin(){
	if ( ($('loginUsername').value.length == '') || ($('loginUsername').value.toLowerCase() == 'username') ) {
		alert('กรุณากรอก Username');
		$('loginUsername').focus();
		return false;
	} // end if
	
	if ( ($('loginPassword').value.length == '') || ($('loginPassword').value.toLowerCase() == 'password') ) {
		alert('กรุณากรอก Password');
		if ($('loginPassword').style.display == 'none')
			$('xxx').focus();
		else
			$('loginPassword').focus();
		return false;
	} // end if
	
}

function checkSubmit30Team(){
	
	var checkRadio = false;
	for(var i=1; i<=36; i++) {
		if ($('brief_date_'+i)) {
				if ( ($('brief_date_'+i).checked) && !($('brief_date_'+i).disabled) ) {
					checkRadio = true;	
					break;
				}
		} // end if
	} // end for
	
	if (!checkRadio) {
		alert('กรุณาเลือกเวลาที่ต้องการ');
		return false;
	} // end if
	
	/*if ($('univercity_detail').value.length == 0) {
		alert('กรุณากรอก รายละเอียดกิจกรรมในมหาวิทยาลัย');
		$('univercity_detail').focus();
		return false;
	} // end if
	
	if ($('cinema_detail').value.length == 0) {
		alert('กรุณากรอก รายละเอียดกิจกรรมในโรงภาพยนตร์');
		$('cinema_detail').focus();
		return false;
	} // end if*/
	
	return true;
	
}

function checkSubmit30TeamTest(){
	
	var checkRadio = false;
	for(var i=1; i<=36; i++) {
		if ($('brief_date_'+i)) {
				if ( ($('brief_date_'+i).checked) && !($('brief_date_'+i).disabled) ) {
					checkRadio = true;	
					break;
				}
		} // end if
	} // end for
	
	if (!checkRadio) {
		alert('กรุณาเลือกเวลาที่ต้องการ');
		return false;
	} // end if
	
	/*if ($('univercity_detail').value.length == 0) {
		alert('กรุณากรอก รายละเอียดกิจกรรมในมหาวิทยาลัย');
		$('univercity_detail').focus();
		return false;
	} // end if
	
	if ($('cinema_detail').value.length == 0) {
		alert('กรุณากรอก รายละเอียดกิจกรรมในโรงภาพยนตร์');
		$('cinema_detail').focus();
		return false;
	} // end if*/
	
}

function checkMemberRegister()
{ 
var format_mail=/^([a-zA-Z0-9\_\-\.]{3,})+@([a-zA-Z0-9\-]{3,})+.+([a-zA-Z]{2,}|.+([a-zA-Z]{2,}))$/;
var memberError = 0;
var limitChoice = 10;
var callAjax = 1;
var data = 'z=z';
var username = document.getElementById('username');
var password = document.getElementById('password');
var project_name = document.getElementById('project_name');
var univercity = document.getElementById('univercity');
var teacher = document.getElementById('teacher');
var phone1 = document.getElementById('phone1');
var project_email = document.getElementById('project_email');
var mode = document.getElementById('mode');
	
	if ( !checkUsernameForm(username.value) || (username.value.length < 6) || (username.value.length > 15) ) {
		alert("กรุณากรอก Username ให้ถูกต้อง");
		username.focus();
		callAjax = 0;
		return false;
	} else {
		data += "&username=" + username.value;
	}
	
	if ( !checkUsernameForm(password.value) || (password.value.length < 6) || (password.value.length > 15) ) {
		alert("กรุณากรอก Password ให้ถูกต้อง");
		password.focus();
		callAjax = 0;
		return false;
	} else {
		data += "&password=" + password.value;
	}
	
	if (project_name.value.length == 0) {
		alert("กรุณากรอกชื่อทีม");
		project_name.focus();
		callAjax = 0;
		return false;
	} else {
		data += "&project_name=" + project_name.value;
	}
	
	if (univercity.value.length == 0) {
		alert("กรุณากรอกมหาวิทยาลัย");
		univercity.focus();
		callAjax = 0;
		return false;
	} else {
		data += '&univercity=' + univercity.value;
	}
		
	
	for (var i=1; i<=limitChoice; i++) {
		var nameID = document.getElementById('name_'+i);
		var surnameID = document.getElementById('surname_'+i);
		var ageID = document.getElementById('age_'+i);
		var levelID = document.getElementById('level_'+i);
		var majorID = document.getElementById('major_'+i);
		var facID = document.getElementById('fac_'+i);
		var emailID = document.getElementById('email_'+i);
		var mobileID = document.getElementById('mobile_'+i);
		
		if ( (nameID.value.length == 0) || (surnameID.value.length == 0) || (ageID.value.length == 0) || (levelID.value.length == 0) || (majorID.value.length == 0) || (facID.value.length == 0) || (emailID.value.length == 0) || (mobileID.value.length == 0) ) {
			memberError++;
		} else if (!(format_mail.test(emailID.value))){
			alert("E-mail ไม่ถูกต้อง");
			emailID.focus();
			callAjax = 0;
			return false;
		} else {
			data += '&aName_' + i + '=' + nameID.value;
			data += '&aSurname_' + i + '=' + surnameID.value;
			data += '&aAge_' + i + '=' + ageID.value;
			data += '&aLevel_' + i + '=' + levelID.value;
			data += '&aMajor_' + i + '=' + majorID.value;
			data += '&aFac_' + i + '=' + facID.value;
			data += '&aEmail_' + i + '=' + emailID.value;
			data += '&aMobile_' + i + '=' + mobileID.value;
		} 
	} // end for
	
	if (memberError == limitChoice) {
		alert("กรุณาระบุข้อมูลสมาชิกในกลุ่มให้ครบ");
		callAjax = 0;
		return false;
	}
	
	if (teacher.value.length == 0) {
		alert("กรุณากรอกชื่ออาจารย์ที่ปรึกษา");
		teacher.focus();
		callAjax = 0;
		return false;
	} else {
		data += '&teacher=' + teacher.value;
	}
	
	if (phone1.value.length == 0) {
		alert("กรุณากรอกหมายเลขโทรศัพท์");
		phone1.focus();
		callAjax = 0;
		return false;
	} else {
		data += '&phone1=' + phone1.value;
	}
	
	if (!(format_mail.test(project_email.value))) {
		alert("กรุณากรอกอีเมลล์");
		project_email.focus();
		callAjax = 0;
		return false;
	} else {
		data += "&project_email=" + project_email.value;
	}
	
	if (callAjax == 1) {
		var url = '';
		data += '&mode=' + mode.value;
		if (mode.value == 'insert') {
			url = 'registerProjectAdd.php';
		} else if (mode.value == 'update') {
			url = 'registerProjectEdit.php';
		}
		//postDataReturnText(url, data, callbackCheckMemberRegister);
		saveMemberRegister(url, data);
	}

} // end function

function saveMemberRegister(url, data){
		new Ajax.Request(url,
			{
				method: "POST",
				parameters: data,
				onComplete: callbackCheckMemberRegister
			}
		);
} // end function

function callbackCheckMemberRegister(data) {
	var aData = data.responseText.split('===');
	if (aData[0] == 'No') {
		alert(aData[1]);
		return false;
	} else {
		alert('บันทึกข้อมูลเสร็จเรียบร้อยแล้ว');
		window.location = 'http://www.disneyfilmfan.com/sendMailBuzzoncampus/sendemail.php'+aData[1];
	}
} // end function

function onloadRegisterProject(){
	document.getElementById('bSubmit').disabled = true;
	document.getElementById('disableCheck').checked = false;
} // end function

function checkUploadFIle(){
	var file1 = document.getElementById('file1');
	if (file1.value == '') {
		alert('กรุณาเลือกไฟล์');
		file1.focus();
		return false;
	}
	
	if ( (file1.value.indexOf('.gif') < 0) && (file1.value.indexOf('.png') < 0) && (file1.value.indexOf('.jpg') < 0) ) {
		alert('ไฟล์ที่อัพโหลดต้องเป็นรูปภาพ (.gif, .jpg, .png) เท่านั้น');
		file1.focus();
		return false;
	}
	
	return true;	
} // end function

function checkUploadFIle2_add(){
	var file1 = document.getElementById('file1');
	var image_title = document.getElementById('image_title');
	if (file1.value == '') {
		alert('กรุณาเลือกไฟล์');
		file1.focus();
		return false;
	}
	if ( (file1.value.toLowerCase().indexOf('.png') < 0) && (file1.value.toLowerCase().indexOf('.gif') < 0) && (file1.value.toLowerCase().indexOf('.jpg') < 0) ) {
		alert('ไฟล์ที่อัพโหลดต้องเป็นรูปภาพ (.gif, .jpg, .png) เท่านั้น');
		file1.focus();
		return false;
	}
	
	if (image_title.value == '') {
		alert('กรุณากรอกรายละเอียด');
		image_title.focus();
		return false;
	}
	
	return true;	
} // end function

function checkUploadFIle2_update(){
	var image_title = document.getElementById('image_title');
	if (image_title.value == '') {
		alert('กรุณากรอกรายละเอียด');
		image_title.focus();
		return false;
	}
	
	return true;	
} // end function

function checkUploadFIle30Team(){
	var file1 = document.getElementById('file1');
	if (file1.value == '') {
		alert('กรุณาเลือกไฟล์');
		file1.focus();
		return false;
	}
	
	if ( (file1.value.toLowerCase().indexOf('.xls') < 0) && (file1.value.toLowerCase().indexOf('.xlsx') < 0) && (file1.value.toLowerCase().indexOf('.pdf') < 0) ) {
		alert('ไฟล์ที่อัพโหลดต้องเป็น (.xls, .xlsx, .pdf) เท่านั้น');
		file1.focus();
		return false;
	}
	
	return true;	
} // end function

function checkUploadFilePDF(){
	var file1 = document.getElementById('file1');
	if (file1.value == '') {
		alert('กรุณาเลือกไฟล์');
		file1.focus();
		return false;
	}
	
	if ( (file1.value.indexOf('.pdf') < 0) ) {
		alert('ไฟล์ที่อัพโหลดต้องเป็นไฟล์ PDF (.pdf) เท่านั้น');
		file1.focus();
		return false;
	}
	
	return true;	
} // end function

function voteProject(project_id, point){
	var url = 'voteProject.php';
	var data = 'project_team_id='+project_id+'&point='+point;
		new Ajax.Request(url,
			{
				method: "POST",
				parameters: data,
				onComplete: callbackVoteProject
			}
		);
} // end function

function callbackVoteProject(data){
	alert(data.responseText);
} // end function

function showFlashDetail(flashName){
	var url = 'getFlashDetail.php';
	var data = 'flashName='+flashName;
		new Ajax.Request(url,
			{
				method: "GET",
				parameters: data,
				onComplete: callbackShowFlashDetail
			}
		);
} // end function

function waitingShowFlashDetail(){
	$('showFlashDetail').innerHTML = '<img src="images/loading.gif" border="0">';
}

function callbackShowFlashDetail(data){
	$('showFlashDetail').innerHTML = data.responseText;
} // end function

function checkUploadFileLogin() {
	if (!checkUsernameForm($('loginUsername').value)) {
		alert('กรุณากรอก Username อีกครั้ง');	
		$('loginUsername').focus();
		return false;
	}
	if (!checkUsernameForm($('loginPassword').value)) {
		alert('กรุณากรอก Password อีกครั้ง');	
		$('loginPassword').focus();
		return false;
	}
} // end function

function showVdoLink(flashName){
	var url = 'getVdoLink.php';
	var data = 'flashName='+flashName;
		new Ajax.Request(url,
			{
				method: "GET",
				parameters: data,
				onComplete: callbackShowVdoLink
			}
		);
} // end function

function callbackShowVdoLink(data){
	$('showVdoLink').innerHTML = data.responseText;
} // end function

function loadingShowVdoLink(){
	$('showVdoLink').innerHTML = '<div aligh="center"><img src="images/loading.gif" border="0"></div>';
}

function check30Team(team_id){
	var url = '30teamCheck.php';
	var data = 'team_id='+team_id;
		new Ajax.Request(url,
			{
				method: "POST",
				parameters: data,
				onComplete: callbackCheck30Team,
				onInteractive: waitCheck30Team
			}
		);
} // end function

function callbackCheck30Team(data){
	var aData = new Array();
	aData = data.responseText.split('***');
	$('showVideo').innerHTML = aData[0];
	$('showText').innerHTML = aData[1];
	
	setInterval("showTextDetail()", 45*1000);
} // end function

function waitCheck30Team(){
	$('showVideo').innerHTML = '<div aligh="center"><img src="images/loading.gif" border="0"></div>';
}

function showTextDetail(){
	$('imageLoding').style.display = 'none';
	$('textDetail').style.display = '';
} // end function

function checkUploadVdo30Team(){
	if ($('file2').value.length == 0) {
		alert('กรุณาเลือก Image');
		$('file2').focus();
		return false;
	} // end if
	
	if ($('file1').value.length == 0) {
		alert('กรุณาเลือกไฟล์');
		$('file1').focus();
		return false;
	} // end if
	
	if ($('title').value.length == 0) {
		alert('กรุณากรอก Title');
		$('title').focus();
		return false;
	} // end if
} // end function


function checkCertiFormDigit(ch){
	var len, digit;
	if(ch == " "){ 
		len=0;
	}else{
		len = ch.length;
	}
	if (len == 0)
		return false;
		
	for(var i=0 ; i<len ; i++)
	{
		digit = ch.charAt(i);
		ascii = ch.charCodeAt(i);
		//if( (digit >= "A" && digit <= "Z") || (digit >= "a" && digit <= "z") || (digit >="0" && digit <="9") || (digit == "_") || (digit == "-") ){
		if( (digit >= "A" && digit <= "Z") || (digit >= "a" && digit <= "z") || (digit >="0" && digit <="9") || (digit == "_") || (digit == "-") || (ascii == 10) || (ascii == 13) || (ascii == 32) || (digit == ".") ){
		; 
		}else{
			return false; 
		} 
	}  // end for
	
	return true;
}// end function

function checkCertiForm(){
	
	var memberError = 0;
	var limitChoice = 10;	
		
		
	if ( ($('team').value.length == 0) || (!checkCertiFormDigit($('team').value)) ) {
		alert('กรุณากรอกชื่อทีมเป็นภาษาอังกฤษ');
		$('team').focus();
		return false;
	} // end if
	
	if ( ($('university').value.length == 0) || (!checkCertiFormDigit($('university').value))) {
		alert('กรุณากรอกชื่อมหาวิทยาลัยเป็นภาษาอังกฤษ');
		$('university').focus();
		return false;
	} // end if
	
	
	for (var i=1; i<=limitChoice; i++) {
		var titleID = document.getElementById('title_'+i);
		var nameID = document.getElementById('name_'+i);
		var surnameID = document.getElementById('surname_'+i);
		
		if ( (nameID.value.length > 0) && !checkCertiFormDigit(nameID.value) ) {
			alert('กรุณากรอกชื่อเป็นภาษาอังกฤษ');	
			nameID.focus();
			return false
		} // end if
		
		if ( (surnameID.value.length > 0) && !checkCertiFormDigit(surnameID.value) ) {
			alert('กรุณากรอกนามสกุลเป็นภาษาอังกฤษ');	
			surnameID.focus();
			return false
		} // end if
		
		if ( (nameID.value.length == 0) || (surnameID.value.length == 0) ) {
			memberError++;
		}
	} // end for
	
	if (memberError == limitChoice) {
		alert("กรุณาระบุข้อมูลสมาชิกในกลุ่มให้ครบ");
		callAjax = 0;
		return false;
	}
	
} // end function


function checkCashAward(){		
		
	if ( $('team_name').value.length == 0 ) {
		alert('กรุณากรอกชื่อทีมเป็นภาษาอังกฤษ');
		$('team_name').focus();
		return false;
	} // end if
	
	if ( $('agent_name').value.length == 0 ) {
		alert('กรุณากรอกชื่อ');
		$('agent_name').focus();
		return false;
	} // end if
	
	if ( $('address_no').value.length == 0 ) {
		alert('กรุณากรอกที่อยู่');
		$('address_no').focus();
		return false;
	} // end if
	
	if ( ($('id_card').value.length != 13) || (!checkNumber2($('id_card').value)) ) {
		alert('กรุณากรอกหมายเลขบัตรประชาชน 13 หลัก');
		$('id_card').focus();
		return false;
	} // end if
	
	if ( $('phone').value.length == 0 ) {
		alert('กรุณากรอกเบอร์โทรศัพท์');
		$('phone').focus();
		return false;
	} // end if
	
} // end function

