/*****************************************************************
Created :  6/5/2007
Author : Mr. Khwanchai Kaewyos (LookHin)
E-mail : webmaster@lookhin.com
Website : www.LookHin.com
Copyright (C) 2005-2007, www.LookHin.com all rights reserved.
*****************************************************************/


/*
Create by LookHin

Demo :
setFormValue('company','company_name');
*/

function setFormValue(objName,strValue){
	var arrObj = document.getElementsByName(objName);
	for(i=0;i<arrObj.length;i++){
		arrObjCheck = arrObj[i];
		if(arrObjCheck.type == "text" || arrObjCheck.type == "textarea" || arrObjCheck.type == "hidden"){ arrObjCheck.value = strValue;}
		if(arrObjCheck.type == "select-one") for(j=0;j<arrObjCheck.length;j++){ if(arrObjCheck.options[j].value == strValue){ arrObjCheck.options[j].selected = true;}else if(arrObjCheck.options[j].text == strValue){arrObjCheck.options[j].selected = true;}}
		if(arrObjCheck.type == "select-multiple") for(j=0;j<arrObjCheck.length;j++){ if(strValue.indexOf("|"+arrObjCheck.options[j].value+"|")  > -1){ arrObjCheck.options[j].selected = true;}else if(strValue.indexOf("|"+arrObjCheck.options[j].text+"|")  > -1){arrObjCheck.options[j].selected = true;}}
		if(arrObjCheck.type == "radio") if(arrObjCheck.value == strValue){ arrObjCheck.checked = true;}
		if(arrObjCheck.type == "checkbox") if(arrObjCheck.value == strValue){ arrObjCheck.checked = true;}
	}
}


// Menu
function toggleMenu(intMenuId){
	objDivMenu = document.getElementById("menu_"+intMenuId);
	
	var fullHeight = objDivMenu.offsetHeight;
	var links = objDivMenu.getElementsByTagName("a");
	for (var i = 0; i < links.length; i++) fullHeight += links[i].offsetHeight;
	var moveBy = Math.round(3 * links.length);
	
	if(objDivMenu.style.display == "none"){

		objDivMenu.style.display = "";
		var fullHeight = objDivMenu.offsetHeight;
		var links = objDivMenu.getElementsByTagName("a");
		for (var i = 0; i < links.length; i++) fullHeight += links[i].offsetHeight;
		var moveBy = Math.round(3 * links.length);
		
		fullHeight = fullHeight - 26*links.length;

		objDivMenu.style.height = "0px";
		
		var intId = setInterval(function() {
			var curHeight = objDivMenu.offsetHeight;
			var newHeight = curHeight + moveBy;

			if (newHeight < fullHeight){
				objDivMenu.style.height = newHeight + "px";
			}else {
				clearInterval(intId);

				objDivMenu.style.height = fullHeight + "px";
			}

		}, 30);

	}else{
			var intId = setInterval(function() {
			var curHeight = objDivMenu.style.height;
			var newHeight = curHeight - moveBy;
			
			if (newHeight > 0){
				objDivMenu.style.height = newHeight + "px";
			}else {
				clearInterval(intId);
				objDivMenu.style.display = "none";
				objDivMenu.style.height = fullHeight + "px";
			}
			
		}, 30);
	}
}



/*
Cookie Function
*/

function createCookie(name,value,days){
	if (days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}else{
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name){
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++){
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name){
	createCookie(name,"",-1);
}


/*
Add Event
*/
function addEvent(obj, evType, fn){ 
	 if(obj.addEventListener){ 
	   obj.addEventListener(evType, fn, false); 
	   return true; 
	 }else if(obj.attachEvent){ 
	   var r = obj.attachEvent("on"+evType, fn); 
	   return r; 
	 }else{ 
	   return false; 
	 } 
}



/*
AJAX
*/
function newXmlHttp(){
	var xmlhttp = false;
	
	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(e){
			xmlhttp = false;
		}
	}

	if(!xmlhttp && document.createElement){
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

/*
getContentLoad(strDivName,strUrl)
*/
function getContentLoad(strDivName,strUrl){
	var xmlhttp = Array();
	xmlhttp[strDivName] = newXmlHttp();
	xmlhttp[strDivName].onreadystatechange=function(){
		if(xmlhttp[strDivName].readyState == 4 && xmlhttp[strDivName].status==200){
			document.getElementById(strDivName).innerHTML = xmlhttp[strDivName].responseText;
		}else{
			document.getElementById(strDivName).innerHTML = "<img src='../images/loading.gif'>";
		}
	}
	xmlhttp[strDivName].open("GET", strUrl, true);
	xmlhttp[strDivName].send(null); 
}


/*
getContentNone(strDivName,strUrl)
*/
function getContentNone(strDivName,strUrl){
	var xmlhttp = Array();
	xmlhttp[strDivName] = newXmlHttp();
	xmlhttp[strDivName].open("GET", strUrl, false);
	xmlhttp[strDivName].send(null);
	
	document.getElementById(strDivName).innerHTML = xmlhttp[strDivName].responseText;

	$('div.divTitleCal a').fancyZoom();
}


function getContentNone1(strDivName,strUrl){
	var xmlhttp = Array();
	xmlhttp[strDivName] = newXmlHttp();
	xmlhttp[strDivName].open("GET", strUrl, false);
	xmlhttp[strDivName].send(null);
	
	document.getElementById(strDivName).innerHTML = xmlhttp[strDivName].responseText;
	
}

