function showStars(num) {
	imgurl = "http://video.clipmass.com/demo/images"
	for(i=1;i<=5;i++){
		if(i<=num){
			document.getElementById('star'+i).style.background='url('+imgurl+'/star.gif)';
			document.getElementById('star'+i).style.cursor="hand";
		}else{
			document.getElementById('star'+i).style.background='url('+imgurl+'/blank_star.gif)';
			document.getElementById('star'+i).style.cursor="hand";
		}
	}
}

function clearStars(){
	for(i=1;i<=5;i++){
		document.getElementById('star'+i).style.background='url('+imgurl+'/blank_star.gif)';
	}
}

function hideMe(myId) {
	document.getElementById(myId).style.display = "none";
	
}

function showMe(myId) {
	document.getElementById(myId).style.display = "block";
	
}

function toggleVisibility(targetId) {
	if (document.getElementById) {
		target = document.getElementById(targetId);
		if (target.style.display == "none"){
			target.style.display = "block";
		} else {
			target.style.display = "none";
		}
	}
}

function check_form(frm) {
	var allow_extensions = /(avi|wmv|flv|mov|3gp|mp4|mpeg|mpg|divx|dat)$/i;

	if (frm.field_video_option.value == 1) {
		if (frm.upload.value != "") {
			if(!frm.upload.value.match(allow_extensions)) {
				var string = frm.upload.value;
				var num_of_last_slash = string.lastIndexOf("\\");

				if(num_of_last_slash < 1) { num_of_last_slash = string.lastIndexOf("/"); }

				var file_name = string.slice(num_of_last_slash + 1, string.length);
				var file_extension = file_name.slice(file_name.indexOf(".")).toLowerCase();
				
				alert('Sorry, uploading a file with the extension "' + file_extension + '" is not allowed.');
				return false;
			}
		} else {
			alert("กรุณาใส่ ไฟล์วีดีโอด้วยค่ะ");
			frm.upload.focus();
			return false;
		}
	} else {
		if (frm.field_embed_code.value == "") {
			alert("กรุณาใส่ Embed code ด้วยค่ะ");
			frm.field_embed_code.focus();
			return false;
		}
	}

	/*
	if (frm.upload.value == "") {
		alert("กรุณาใส่ ไฟล์วีดีโอด้วยค่ะ");
		frm.upload.focus();
		return false;
	}
	*/

	if (frm.field_myvideo_title.value == "") {
		alert("กรุณาใส่ ไตเติ้ลวีดีโอด้วยค่ะ");
		frm.field_myvideo_title.focus();
		return false;
	}

	if (frm.field_myvideo_descr.value == "") {
		alert("กรุณาใส่ รายละเอียดวีดีโอด้วยค่ะ");
		frm.field_myvideo_descr.focus();
		return false;
	}

	if (frm.field_myvideo_keywords.value == "") {
		alert("กรุณาใส่ คำค้นหาวีดีโอด้วยค่ะ");
		frm.field_myvideo_keywords.focus();
		return false;
	}

	total = 0;
	//selectedChannels = "";
	for(var i=0; i < frm.chlist.length; i++){
		if(frm.chlist[i].checked) {
			total++;
			/*
			if (selectedChannels == "") {
				selectedChannels = frm.chlist[i].value;
			} else {
				selectedChannels += ", " + frm.chlist[i].value;
			}
			*/
		}
	}
	if (total < 1 || total > 3) {
		alert("กรุณาเลือกช่องวีดีโอด้วยค่ะ");
		return false;
	}

	//frm.upload.disabled = true;
	
	frm.upload_btn.disabled = true;

	//frm.chlist_selected.value = selectedChannels;

	return true;
}

function check_upload(frm) {
	var allow_extensions = /(avi|wmv|flv|mov|3gp|mp4|mpeg|mpg|divx|dat)$/i;

	if (frm.field_video_option.value == 1) {
		if (frm.upload.value != "") {
			if(!frm.upload.value.match(allow_extensions)) {
				var string = frm.upload.value;
				var num_of_last_slash = string.lastIndexOf("\\");

				if(num_of_last_slash < 1) { num_of_last_slash = string.lastIndexOf("/"); }

				var file_name = string.slice(num_of_last_slash + 1, string.length);
				var file_extension = file_name.slice(file_name.indexOf(".")).toLowerCase();
				
				alert('Sorry, uploading a file with the extension "' + file_extension + '" is not allowed.');
				return false;
			}
		} else {
			alert("กรุณาใส่ ไฟล์วีดีโอด้วยค่ะ");
			frm.upload.focus();
			return false;
		}
	} else {
		if (frm.field_embed_code.value == "") {
			alert("กรุณาใส่ Embed code ด้วยค่ะ");
			frm.field_embed_code.focus();
			return false;
		}
	}

	if (frm.field_myvideo_title.value == "") {
		alert("กรุณาใส่ ไตเติ้ลวีดีโอด้วยค่ะ");
		frm.field_myvideo_title.focus();
		return false;
	}

	if (frm.field_myvideo_descr.value == "") {
		alert("กรุณาใส่ รายละเอียดวีดีโอด้วยค่ะ");
		frm.field_myvideo_descr.focus();
		return false;
	}

	if (frm.field_myvideo_keywords.value == "") {
		alert("กรุณาใส่ คำค้นหาวีดีโอด้วยค่ะ");
		frm.field_myvideo_keywords.focus();
		return false;
	}

	if (frm.chlist.value == "") {
		alert("กรุณาเลือกช่องวีดีโอด้วยค่ะ");
		return false;
	}

	frm.upload_btn.disabled = true;

	return true;
}

function valid(frm) {
	if (frm.first_name.value == "") {
		alert("กรุณาใส่ ชื่อ ด้วยค่ะ");
		frm.first_name.focus();
		return false;
	}

	if (frm.last_name.value == "") {
		alert("กรุณาใส่ นามสกุล ด้วยค่ะ");
		frm.last_name.focus();
		return false;
	}

	if (frm.address.value == "") {
		alert("กรุณาใส่ ที่อยู่่ ด้วยค่ะ");
		frm.address.focus();
		return false;
	}

	if (frm.postcode.value == "") {
		alert("กรุณาใส่ รหัสไปรษณีย์ ด้วยค่ะ");
		frm.postcode.focus();
		return false;
	}

	if (frm.phone.value == "") {
		alert("กรุณาใส่ เบอร์โทรศัพท์ ด้วยค่ะ");
		frm.phone.focus();
		return false;
	}

	if (frm.email.value == "") {
		alert("กรุณาใส่ อีเมล์ ด้วยค่ะ");
		frm.email.focus();
		return false;
	}

	max = frm.reference.length;
	ref_selected = "";
	for (i = 0; i < max; i++) {
		if (frm.reference[i].checked) {
			if (ref_selected != "") {
				ref_selected += ", "+ frm.reference[i].value;
			} else {
				ref_selected = frm.reference[i].value;
			}
		}
	}

	frm.reference_selected.value = ref_selected;

	return true;
}

function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("รูปแบบ email ไม่ถูกต้อง");
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("รูปแบบ email ไม่ถูกต้อง");
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert("รูปแบบ email ไม่ถูกต้อง");
		return false;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		alert("รูปแบบ email ไม่ถูกต้อง");
		return false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert("รูปแบบ email ไม่ถูกต้อง");
		return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		alert("รูปแบบ email ไม่ถูกต้อง");
		return false;
	 }
	
	 if (str.indexOf(" ")!=-1){
		alert("รูปแบบ email ไม่ถูกต้อง");
		return false;
	 }

	 return true;
}

function trim(_data) {
  return _data.replace(/^\s+|\s+$/g, '');
}

function forumPostValid(frm) {
	err = 0;
	if (frm.name.value == "") {
		alert("กรุณาใส่ ชื่อ ด้วยค่ะ");
		frm.name.focus();
		err++;
		return false;
	}

	if (frm.email.value != "") {
		if (echeck(frm.email.value) == false) {
			err++;
			return false;
		}
	} else {
		alert("กรุณาใส่ email ของคุณด้วยค่ะ");
		frm.email.focus();
		err++;
		return false;
	}

	if (frm.forum_title.value == "") {
		alert("กรุณาใส่ หัวข้อ ด้วยค่ะ");
		frm.forum_title.focus();
		err++;
		return false;
	}

	if (frm.forum_detail.value == "") {
		alert("กรุณาใส่ รายละเอียด ด้วยค่ะ");
		frm.forum_detail.focus();
		err++;
		return false;
	}

	if (frm.captcha.value == "") {
		alert("กรุณาใส่ รหัสรูปภาพ ด้วยค่ะ");
		frm.captcha.focus();
		err++;
		return false;
	}

	return true;
}

function forumReplyValid(frm) {
	err = 0;
	if (frm.name.value == "") {
		alert("กรุณาใส่ ชื่อ ด้วยค่ะ");
		frm.name.focus();
		err++;
		return false;
	}

	if (frm.email.value != "") {
		if (echeck(frm.email.value) == false) {
			err++;
			return false;
		}
	} else {
		alert("กรุณาใส่ email ของคุณด้วยค่ะ");
		frm.email.focus();
		err++;
		return false;
	}

	if (frm.detail.value == "") {
		alert("กรุณาใส่ รายละเอียด ด้วยค่ะ");
		frm.detail.focus();
		err++;
		return false;
	}

	if (frm.captcha.value == "") {
		alert("กรุณาใส่ รหัสรูปภาพ ด้วยค่ะ");
		frm.captcha.focus();
		err++;
		return false;
	}

	return true;
}