
with(document)
{
	write('<script type="text/javascript" src="js/lib/jQuery/jquery-1.4.2.min.js"></script>');
	write('<script type="text/javascript" src="js/quickmenu.js"></script>');
}


function addLoadEvent(func) {
	var oldonload = window.onload;
	if(typeof window.onload != 'function'){
		window.onload = func;
	}else{
		window.onload = function() {
			if (oldonload) {
			oldonload();
			}
			func();
		}
	}
}

function displayHtml(sourceHtml, targetId){
	//var source = document.getElementById(sourceId);
	var sourceHtml = sourceHtml || '';
	var target = document.getElementById(targetId);
	if(target == null){
		return false;
	}
	
	target.innerHTML = sourceHtml;
}

var AboutNetwork = {
	displayContactInfo : function(sourceId){ 
		var source = document.getElementById(sourceId);
		if(source == null){
			return false;
		}
		
		html = '<fieldset><legend>Contact Informations</legend><div class="container">' + source.innerHTML + '</div></fieldset>';
		displayHtml(html, 'contactInfoContainer');
	}
}

function changeMovingPlan(){
	window.open("PlannerCalendar/plannerFrmMoving.php","frmMoving","width=500,height=550,resizable=no,location=no,status=no,scrollbars=yes");
}


var uc = new Object();
function underConstruction(){
	addLoadEvent(function(){
		var divUc = document.createElement('div');
		divUc.id = 'underConstruction';
		divUc.style.position = 'absolute';
		divUc.style.zIndex = '1000';
		divUc.style.left = '0';
		divUc.style.top = '215px';
		if( b.match(/ie/) ){
			divUc.style.filter = 'alpha(opacity=85)';
		}else{
			divUc.style.opacity = '0.85';
		}
		divUc.style.textAlign = 'center';
		divUc.style.width = '100%';
		var textHTML; 
		textHTML = '<div style="width:450px;margin:auto;border:2px outset #BF0000;background-color:#fff;text-align:right;">';
		textHTML += '<div><a href="javascript:ucClose(\''+ divUc.id +'\');" title="Close"><img src="images/btnClose.gif" alt="Close" /></a></div>';
		textHTML += '<div><img src="images/under-construction.jpg" title="UnderConstruction" alt="UnderConstruction" /></div>';
		textHTML += '</div>';
		divUc.innerHTML = textHTML;

		document.body.appendChild(divUc);
	});
}

function ucClose(elId){
	var el = document.getElementById(elId);
	document.body.removeChild(el);
}

var b = getBrowser();
function getBrowser(){
	var b = navigator.appVersion;
	var txt;
	if( b.match(/MSIE 6/i) ){
		txt = 'ie6';
	}else if( b.match(/MSIE 7/i) ){
		txt = 'ie7';
	}else{
		txt = 'other';
	}

	return txt;
}

function loadVideo(containerId, srcPath, langVersion){
	var srcPath = srcPath || '';
	var langVersion = langVersion || '';
	
	var videoName;
	
	switch(langVersion)
	{
		case 'th' :
			videoName = 'JVK_Revised.flv';
		break;
			
		case 'en' :
			videoName = 'JVK_Eng.flv';
		break;
		
		case 'jp' :
			videoName = 'JVK_JP_Revised2.flv';
		break;
	}

	function load(){
		FlashClass.flash(containerId, srcPath+'mediaplayer/mediaplayer.swf?file=../vdo/' + videoName + '&image=' + srcPath + 'vdo/pic-sample.jpg&shuffle=false&fullscreen=true',367,264,8,'opaque');
	}
	if( !window.XMLHttpRequest ){
		addLoadEvent(load);
	}else{
		load();
	}
}


function disabledFormItem(bool,frmId,itemsName){
	if( !(itemsName instanceof Array) ){
		itemsName = new Array(itemsName);
	}

	var frmEl = document.getElementById(frmId);
	for(var i=0; i<itemsName.length; i++){
		var items = document.getElementsByName(itemsName[i]);
		items[0].disabled = bool;
	}
	
}

FormValidator = function(frmEl,options){
	var frmEl = frmEl;
	var options = {
		warningLanguage : (options.warningLanguage) ? options.warningLanguage: 'th',
		warningColor : (options.warningColor) ? options.warningColor: '#FFDFE0',
		warningChoice : (options.warningChoice) ? options.warningChoice: null 
		// 'Tel/Fax' OR Array('Tel/Fax','Email/Mobile/Address') ##Comment: must have id Tel or Fax, must have Email or Mobile
	}

	//--------- Global Variable -----------//
	var input_bgcolor = options.warningColor;
	if(options.warningLanguage == "en"){ var langindex = 0; }else{ var langindex = 1; }
	var err_specify_msg = new Array("Please specify","โปรดระบุ");
	var err_email_msg = new Array("E-mail Address incorrect.","รูปแบบ E-mail Address ไม่ถูกต้อง");
	var err_number_msg = new Array("Please number only","ระบุตัวเลขเท่านั้น");
	var err=0;
	var errmsg="";
	var errmsg_last="";
	var radioNames = new Array();
	//--------- End Global Variable -----------//

	return ChkForm(); // Start Validate

	function ChkForm(){
		var tag_list=new Array('input','textarea','select');
		var obj=new Array();
		for(i=0;i<=tag_list.length-1;i++){
			obj[i] = frmEl.getElementsByTagName(tag_list[i]);
			
			for(var i1=0; i1<obj[i].length; i1++){
				var currentEl = obj[i][i1];
				Verify(currentEl);
			}
			
		}
		
		if(err == 1){
			alert(errmsg+errmsg_last);
			return false;
		}else{
			return true;
		}
	}

	function Verify(el){
		var currentEl = el;
		var currentTagName = currentEl.tagName.toLowerCase();

		if(currentEl.title && currentEl.title.length>0){
			var elType = currentEl.getAttribute("type");

			if(elType == 'radio' && radioNames[currentEl.name] == true){
				return true;
			}

			if(currentEl.value.match(/^\s*$/) || (elType == 'radio' && currentEl.checked == false) ){
				radioNames[currentEl.name] = true;

				if(
					(ChkChoice(currentEl) == true && elType != 'radio') ||
					(ChkRadio(currentEl) == true && elType == 'radio')
				){
					if(elType != 'radio'){
						currentEl.style.backgroundColor='';
					}
				}else{
					if(elType != "radio"){
						currentEl.style.backgroundColor=input_bgcolor;
						currentEl.value='';
					}
					errmsg+=err_specify_msg[langindex]+" "+currentEl.getAttribute("title")+" \n";
					err=1;
				}
			}else if(elType != "radio"){
				currentEl.style.backgroundColor='';
			}
			
			if(currentEl.getAttribute("alt")=="email" && elType != "radio"){
				if(!(isEmail(currentEl.value)) && !(currentEl.value.match(/^\s*$/))){
					errmsg_last+=currentEl.getAttribute("title")+": "+err_email_msg[langindex]+" \n";
					currentEl.style.backgroundColor=input_bgcolor;
					err=1;
				}else{
					if(!(currentEl.value.match(/^\s*$/))){ currentEl.style.backgroundColor=''; }
				}
			}
			if(currentEl.getAttribute("alt")=="number" && elType != "radio"){
				if(!(isNumeric(currentEl.value))){
					errmsg_last+=currentEl.getAttribute("title")+": "+err_number_msg[langindex]+" \n";
					currentEl.style.backgroundColor=input_bgcolor;
					err=1;
				}else{
					if(!(currentEl.value.match(/^\s*$/))){ currentEl.style.backgroundColor=''; }
				}
			}
		}

		if(err==1){
			return false;
		}else{
			return true;
		}
	}

	function ChkRadio(el){
		var items = frmEl.getElementsByTagName('input');
		var elName = el.name;
		var txt = '';
		for(var i=0; i<items.length; i++){
			var currentItem = items[i];
			if(currentItem.type){
				var itemType = currentItem.type;
			}else{
				var itemType = currentItem.getAttribute('type');
			}

			if(itemType == 'radio' && currentItem.name == elName && currentItem.checked == true){
				return true;
			}
		}

		return false;
	}

	function ChkChoice(el){
		if(options.warningChoice == null){
			return false;
		}

		if(!(options.warningChoice instanceof Array)){
			var choice = Array(options.warningChoice);
		}else{
			var choice = options.warningChoice;
		}

		var elType = el.getAttribute("type");

		var idExist = false;
		for(var i=0; i<choice.length; i++){
			var choiceSplit = choice[i].split('/');
			for(var i1=0; i1<choiceSplit.length; i1++){ // check exist el.id
				if(el.id == choiceSplit[i1]){ 
					idExist = true;
					var currentEl = choice[i];
					choiceSplit = null;
					break;
				}
			}
		}

		if(idExist == false){ return false; }
		
		var choiceSplit = currentEl.split('/');
		for(var i=0; i<choiceSplit.length; i++){ // check choice not white space
			if(el.id == choiceSplit[i]){ continue; }

			var currentEl = document.getElementById(choiceSplit[i]);
			if(currentEl.type){
				var currentElType = currentEl.type;
			}else{
				var currentElType = currentEl.getAttribute('type');
			}
			//alert(currentEl.id);
			if( !(currentEl.value.match(/^\s*$/)) && currentElType != 'radio' ){ // Not white space
				return true;
			}
		}

		return false;
	}


	function isEmail(str){
		var re = /^\w+[\w\._-]+@\w{2,}\.(([a-z]{2,4})|([a-z]{2}\.[a-z]{2,3}))$/i;
		return str.match(re);
	}

	function isNumeric(str){
		var re = /[\D]/g;
		return str.match(re);
	}
}



addItemRows = function(tbId,numRows,indexes){
	var indexes = indexes;
	var el = document.getElementById(tbId);
	if(
		numRows != null && numRows != 'undefined' &&
		el != null && tbId != null
	){
		for(var i=0; i<numRows; i++){
			add();
			indexes++;
		}

		return indexes;
	}
	function add(){
		if(el.getElementsByTagName('tbody')){
			var tBody = el.getElementsByTagName('tbody')[0];
		}
		var mainFieldName = 'objInfo';
		var subFieldNames = new Array('room','item','width','long','height','qty','comment');

		var trEl = document.createElement('tr');
		
		for(var i=0; i<subFieldNames.length; i++){
			var tdEl = document.createElement('td');

			var textBox = document.createElement('input');

			if(textBox.type){
				textBox.type = 'text';
			}else{
				textBox.setAttribute('type','text');
			}

			textBox.name = mainFieldName+'['+indexes+']'+'['+subFieldNames[i]+']';

			tdEl.appendChild(textBox);
			trEl.appendChild(tdEl);
		}
		
		if(tBody){
			tBody.appendChild(trEl);
		}else{
			el.appendChild(trEl);
		}
	}
}


function setCookie(name, value, expires, path, domain, secure) {
	document.cookie= name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}
