// Ajax Call functions

function GetXmlHttpObject(){ 
	var objXMLHttp=null;
	if (window.XMLHttpRequest)
	{
	objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
	objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}

function stateChanged(divID){ 
	if(xmlHttp.readyState==4){
    	if(xmlHttp.status==200){
			document.getElementById(divID).innerHTML=xmlHttp.responseText;
		}else{
			alert('error : ' + xmlHttp.responseText);
		}
	}
}

function toggleMenu(id){ // This function is used for main display of admin catalog homepage

	var allULs=document.getElementById("moduleContent").getElementsByTagName("ul");
	var openUL="ulcat"+id;

	for(i=0;i<allULs.length;i++){
		if(allULs[i].id==openUL){
			if(allULs[i].style.display=="block"){
				allULs[i].style.display="none";
			}else{
				allULs[i].style.display="block";
			}
		}
	}	
	currentPic=document.getElementById('i'+id).src;
	currentPic=currentPic.split("/")

	if(currentPic[4]=="btn_plus.gif") {
		document.getElementById('i'+id).src="/images/btn_minus.gif";
	} else {
		document.getElementById('i'+id).src="/images/btn_plus.gif";
	}
}

function newwin(url,w,h,s,left,top){
	if(left==undefined){
		left=(screen.width/2)-(w/2);
	}
	if(top==undefined){
		top=(screen.height/2)-(h/2);
	}
	win=window.open(url,"swin","left="+left+", top="+top+", toolbar=no, location=0, directories=no, status=no, menubar=no, resizable=no, copyhistory=no,scrollbars="+s+",resizable=0,width="+w+",height="+h)
	win.focus();
}


// Check all checkbox input on Wbs module > list
/*
function getValues(elm){	
	var  total=elm.length;
	for(var i=0;i<total;i++){
		if(elm[i].checked==true){
			alert(elm[i].value );
		}
	}	
}	*/

function checkAll(form){ 

   for (var i = 1; i < form.elements.length; i++){     
	if(form.elements[i].type=="checkbox"){
		if(form.elements[i].checked ==true) {
			form.elements[i].checked =false;
		} else {
			form.elements[i].checked =true;
		}
	}
  } 

} 
 
function batchDel(elm,action){	
	var  total=elm.length;
	if(total>0){
		for(var i=0;i<total;i++){
			if(elm[i].checked==true){
				//alert(elm[i].value);
				window.location.href='index.php?action='+action+'&id='+elm[i].value;
				alert("Deleted"+elm[i].value);
			}
		}	
	}else{
		if(elm.checked==true){
			window.location.href='index.php?action='+action+'&id='+elm.value;
		} 
	}
	
}	

function radioCheck( rad, value){
	var  loop = rad.length;
	 for(i=0 ;i<rad.length; i++)
	{
		if( rad[i].value == value)
		{
			rad[i].checked = true;
			break;
		}
		
	}
}

function selectCheck( chk, value){
	 for(i=0 ; i<chk.length; i++)
	{
		if( chk.options[i].value == value)		{
			chk.selectedIndex = i;
			break;
		}
	}
}

// Catalog & Order functions used
function isStartZero(el){
	if(!isNaN(el.value)){
		if ((el.value!="") &&(el.value.charAt(0)==0)) {
			el.value='';
		return true;
		}
	} else {
		el.value='';
	}
	return false;
}

function formatCurrency(num, decimal) {
		num = num.toString().replace(/\$|\,/g,'');
		if(isNaN(num))
		num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
		if(cents<10)
		cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
		if(decimal==true){
			return (((sign)?'':'-') + num + '.' + cents);
		} else {
			return (((sign)?'':'-') + num);
		}
	} 
	
function equalHeight(classToSet){
	for(var i=0;i<classToSet.length;i++){
		var moduleListItem =YAHOO.util.Dom.getElementsByClassName(classToSet[i]);
		var j=0;
		var itemHeight=0;
		var maxHeight = 0;
		for( j in moduleListItem){
			curHeight=moduleListItem[j].offsetHeight;
			if (curHeight >= maxHeight) {
				maxHeight =curHeight;
			}
		}
		maxHeight =maxHeight-10;
		var j=0;
		for( j in moduleListItem){
			moduleListItem[j].style.height=maxHeight+'px';
		}
	}
}

function  showHint(obj){
	collection=obj.parentNode.getElementsByTagName('span');
	if(collection[0].className=="hint hide") {
		collection[0].className="hint";
	}
}

function  hideHint(obj){
	collection=obj.parentNode.getElementsByTagName('span');
	if(collection[0].className=="hint") {
		collection[0].className="hint hide";
	}
}

function  toggleElm(elm, elmButton) {
	if ((document.getElementById(elm))&&(document.getElementById(elmButton))){
		if((document.getElementById(elm).style.display=='') || (document.getElementById(elm).style.display=='none')) {
			document.getElementById(elm).style.display='block';
			document.getElementById(elmButton).style.backgroundImage='url(/images/btn_arrow_up.gif)';
			YAHOO.util.Cookie.set("loginFormOn", "on", {path: "/"});
		} else {
			document.getElementById(elm).style.display='none';
			document.getElementById(elmButton).style.backgroundImage='url(/images/btn_arrow_down.gif)';
			YAHOO.util.Cookie.set("loginFormOn", "off", {path: "/"});
		}
	}
}

function  toggleLoginMsg() {
	if(document.getElementById('loginMsg')){
		if((document.getElementById('loginMsg').style.display=='') || (document.getElementById('loginMsg').style.display=='block')) {
			document.getElementById('loginMsg').style.display='none';
		} else {
			document.getElementById('loginMsg').style.display='block';
		}
	}	
}
 
function  toggleCommentBox(id) {
	if(document.getElementById(id)){
		if((document.getElementById(id).style.display=='') || (document.getElementById(id).style.display=='none')) {
			document.getElementById(id).style.display='block';
		} else {
			document.getElementById(id).style.display='none';
		}
	}
}
 
function showMyVideos(data, div) {
	var feed = data.feed;
	var entries = feed.entry || [];
	var html = ['<ul>'];
	for (var i = 0; i < entries.length; i++) {
	var entry = entries[i];
	var title = entry.title.$t;
	//var url = entry.media$group.media$content[0].url
	var url = entry.media$group.yt$videoid.$t
	var thumbnailUrl = entries[i].media$group.media$thumbnail[0].url;

	html.push('<li class="columnVideo"><img src="', 
		  thumbnailUrl, '"/><a  target="_blank" title="',title,'" rel="prettyPhoto" href="http://www.youtube.com/watch?v=',url,'">',
		  '<span class="titlec">', title, '...</span>', '</a></li>')
	}
	html.push('</ul>');
	document.getElementById('videos').innerHTML = html.join('');
}

function showHintUsername(str) {
	document.getElementById('txtHintWrapper').style.visibility='visible';
	if (str.length==0)  { 
		document.getElementById("txtHintWrapper").innerHTML="";
		return;
	}
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }else {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function() {
	if (xmlhttp.readyState==4 && xmlhttp.status==200){
		document.getElementById('txtHintWrapper').innerHTML=xmlhttp.responseText;

    }
	}
	xmlhttp.open("GET","/include/modules/ims/gethint.php?q="+str,true);
	xmlhttp.send();

}

function confirmAction(action, url){
   if(action=="Delete"){
   var answer = confirm("Are you sure to delete ?")
   }
   if(answer==true){
      window.location = url;
    }
 }
 
function initJsScript(){
	if(typeof window.getOnlineUsers == 'function') {
		getOnlineUsers();
	}
	if(typeof window.getNewMessages == 'function') {
		getNewMessages();
	}
	if(typeof window.initialize == 'function') {
		initialize();
	}
	if(typeof window.refreshChat == 'function') {
		refreshChat();
	}
}
 	
function makeRequest(formID, targetDiv, url){
	var div = document.getElementById(targetDiv);

	var handleSuccess = function(o){
		if(o.responseText !== undefined){
			div.innerHTML =  o.responseText;			
		}
	};

	var callback ={
	  success:handleSuccess
	};

	var formObject = document.getElementById(formID);
	YAHOO.util.Connect.setForm(formObject);
	var cObj = YAHOO.util.Connect.asyncRequest('POST', url, callback);
}

function uploadRequest(formID, targetDiv, url){
	var div = document.getElementById(targetDiv);
	var handleSuccess = function(o){
		if(o.responseText !== undefined){
			div.innerHTML =  o.responseText;			
		}
		document.getElementById('uploadfile').form.reset();
		
		$(document).ready(function(){
			$("a[rel^='prettyPhoto']").prettyPhoto({
				animationSpeed: 'normal', /* fast/slow/normal */
				padding: 40, /* padding for each side of the picture */
				opacity: 0.8, /* Value betwee 0 and 1 */
				showTitle: false, /* true/false */
				allowresize: true, /* true/false */
				counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
				theme: 'dark_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
				hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
				changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
				callback: function(){} /* Called when prettyPhoto is closed */
			});
			autoResizeTextarea(formID.slice(9));
		});
		
	};
	
	var callback ={
	  upload: handleSuccess,
	  success:handleSuccess
	};

	var formObject = document.getElementById(formID);
	YAHOO.util.Connect.setForm(formObject, true);
	var cObj = YAHOO.util.Connect.asyncRequest('POST', url, callback);
}

function autoResizeTextarea(id){
	$('textarea#detail-'+id).autoResize({
	onResize : function() {
	    $(this).css({opacity:0.8});
	 },
	animateCallback : function() {
	     $(this).css({opacity:1});
	 },
	 animateDuration : 300,
	 extraSpace : 40
	});
}
function genDropDownAjax(url, id){
	var div = document.getElementById(id);

	var handleSuccess = function(o){
		if(o.responseText !== undefined){
			div.innerHTML =  o.responseText;			
		}
	};

	var callback ={
	  success:handleSuccess
	};
 
	var cObj = YAHOO.util.Connect.asyncRequest('GET', url, callback);
}

function setPanelChat(panelname, paneltxt, talkname, requestor){
	if(!document.getElementById(panelname)){
		var panelHdName = document.createElement('span');
		var  profileLink = document.createElement('a');
		profileLink.target = "blank";
		profileLink.className="profileLink";
		
		if(talkname==""){
			profileLink.href = "/members/?action=view&name="+requestor;
 			var  profileLinkTxt = document.createTextNode(requestor);
		} else {
			profileLink.href = "/members/?action=view&name="+talkname;
 			var  profileLinkTxt = document.createTextNode(talkname);
		}
		
		profileLink.appendChild(profileLinkTxt);
		
		var panelHdNameTxt = document.createTextNode(paneltxt+' กับ ');	
		
		panelHdName.appendChild(panelHdNameTxt);
		panelHdName.appendChild(profileLink);
		
		var panelHdCls = document.createElement('a');
		panelHdCls.className = "close";
		
		var panelHdClsTxt = document.createTextNode('close[x]');
		panelHdCls.appendChild(panelHdClsTxt);
		panelHdCls.onclick=function(){
			document.body.removeChild(panel);
			// Find Number of currently opened panel and reposition each panel not to overlap
			var panels = YAHOO.util.Dom.getElementsByClassName('panel');
			numPanels = panels.length
			for(i=0;i<numPanels;i++){
				rightPosition = i*21.6;
				panels[i].style.right=rightPosition+"%";
			}

			// Ajax Touch Chat Request
			var handleSuccessChatRequest = function(o){
				if(o.responseText !== undefined){
				}
			}
			var callbackChatRequest ={
			  success:handleSuccessChatRequest
			}
			var sUrl = "/include/inc-requestchat.php?requestor="+requestor+"&action=cancel&id="+Math.random();
			YAHOO.util.Connect.asyncRequest('GET', sUrl, callbackChatRequest); 
		}
				
		var panelHd = document.createElement('div');
		panelHd.appendChild(panelHdName);
		panelHd.appendChild(panelHdCls);
		panelHd.className= "hd";
		
		var panelBody = document.createElement('div');
		panelBody.className = "body";
		
		var panelIframe = document.createElement('iframe');
		panelIframe.id="iframe-chat";
		panelIframe.width="100%";
		panelIframe.height="330";
		panelIframe.frameBorder=0;
		panelIframe.borderColor="#ffffff";
		panelIframe.src="/chat/?action=livechat&talkname="+talkname+"&requestor="+requestor+"&layout=swin";
		panelBody.appendChild(panelIframe);

		var panel = document.createElement('div');		
		panel.id=panelname;	
		
		// Find Number of currently opened panel and reposition each panel not to overlap
		var panels = YAHOO.util.Dom.getElementsByClassName('panel');
		numPanels = panels.length
		rightPosition = numPanels*21.6;
		
		panel.style.right=rightPosition+"%";
		panel.className="panel";
		panel.name=panelname;
		
		panel.onclick = function() {
			var panels = YAHOO.util.Dom.getElementsByClassName('panel');
			for(i=0;i<=numPanels;i++){
				if(panels[i]!=undefined) { 
					if (panel.id==panels[i].id){
						panels[i].style.zIndex=1000;
					}else  {
						panels[i].style.zIndex=0;
					}
				}
			}
		}
		
		panel.appendChild(panelHd);
		panel.appendChild(panelBody);
		document.body.appendChild(panel);
		// Ajax Touch Chat Request
		var handleSuccessChatRequest = function(o){
			if(o.responseText !== undefined){
			}
		}
	 
		var callbackChatRequest ={
		  success:handleSuccessChatRequest
		}
		
		var sUrl = "/include/inc-requestchat.php?talkname="+talkname+"&requestor="+requestor+"&id="+Math.random();
		YAHOO.util.Connect.asyncRequest('GET', sUrl, callbackChatRequest); 
	 }
}

function  floatPanel(talkname, requestor){
	var panels = parent.YAHOO.util.Dom.getElementsByClassName('panel');
	numPanels = panels.length
	if(talkname==""){
		panel=parent.document.getElementById('panel-chat-'+requestor);
	} else  {
		panel=parent.document.getElementById('panel-chat-'+talkname);
	}
	for(i=0;i<=numPanels;i++){
		if(panels[i]!=undefined) { 
			if (panel.id==panels[i].id){
				panels[i].style.zIndex=1000;
			}else  {
				panels[i].style.zIndex=0;
			}
		}
	}
}


