// JavaScript Document

function toggleTab(Item){

	tabContentArray= new Array('tab1Content','tab2Content','tab3Content');
	tabArray= new Array('tab1','tab2','tab3');
	for(aT=0;aT<tabArray.length;aT++){
			currentItemT=document.getElementById(tabArray[aT]);
			currentItemC=document.getElementById(tabContentArray[aT]);
			if(Item.id==tabArray[aT])
					currentItemT.className='tabActive';
			else
					currentItemT.className='tabNonActive';

			if(tabContentArray[aT]==Item.id+"Content")
					currentItemC.style.display="block";
			else
					currentItemC.style.display="none";			
	}
}


function switchMask(Item){
	currentItem=document.getElementById(Item);
	if(currentItem.className=='mask'){
			currentItem.className='maskOver';
			return;
	}
	if(currentItem.className=='maskOver'){
			currentItem.className='mask';
			return;
	}
	
	if(currentItem.className=='steps'){
			currentItem.className='stepsOver';
			return;
	}
	if(currentItem.className=='stepsOver'){
			currentItem.className='steps';
			return;
	}
}

function gotolocation(loc){
		location=loc;
}

var checkedArray=new Array();

function bodyOnLoad(){
		
		var tabArray= new Array('tab1','tab2','tab3');
		for(aT=0;aT<tabArray.length;aT++){
				//alert(tabArray.length);
				currentItem=document.getElementById(tabArray[aT]);
				if(currentItem){
						currentItem.onmouseover=function(){;
									if(this.className=='tabNonActive'){
											this.className='tabActive';
									}
							}
							
							currentItem.onmouseout=function(){
									if(this.className=='tabActive'){
											currentItemContent=document.getElementById(this.id+"Content");
											if(currentItemContent.style.display=="none")
													this.className='tabNonActive';
											
									}
							}
							
							currentItem.onclick=function(){
										toggleTab(this);							
							}					
				}
	
		}
		
		function getTimeSerial(){
				mydate= new Date();
				timeSerial=(mydate.getHours()*1000*60*60)+(mydate.getMinutes()*1000*60)+(mydate.getSeconds()*1000)+mydate.getMilliseconds();
				return timeSerial;
		}
		var divArray= new Array('company1','company2','company3','company17','company5','company6','company7','company8','company9','company10','company11','company12','company13','company14','company15','company16');
		var descArray= new Array("บริษัท วิริยะประกันภัย จำกัด","บริษัท อาคเนย์ประกันภัย (2000)จำกัด","บริษัท เทเวศประกันภัย จำกัด","บริษัท ไทยพาณิชย์สามัคคีประกันภัย จำกัด (มหาชน)","บริษัท เอเชียประกันภัย1950 จำกัด","บริษัท เจ้าพระยาประกันภัย จำกัด (มหาชน)","เอ็ม เอส ไอ จี ประกันภัย (ประเทศไทย) จำกัด","บริษัท ประกันภัยศรีเมือง จำกัด","บริษัท อลิอันซ์ ซี. พี. ประกันภัย จำกัด","บริษัท ประกันคุ้มภัย จำกัด ","บริษัท มิตรแท้ประกันภัย จำกัด","บริษัท แอลเอ็มจี ประกันภัย จำกัด","บริษัท คูเนีย ประกันภัย (ประเทศไทย) จำกัด","บริษัท กมลประกันภัย จำกัด (มหาชน)","บริษัท ไทยศรีประกันภัย จำกัด","บริษัท กรุงเทพประกันภัย จำกัด (มหาชน)");
		var selectorArray= new Array(
														   'insureType1','insureType1_p','insureType1_t',
														   'insureType2','insureType2_p','insureType2_t','insureType2_v',
														   'insureType3','insureType3_p','insureType3_t','insureType3_v',
														   'insureType4','insureType4_p','insureType4_t','insureType4_v','insureType4_tx','insureType4_m',
														   'insureType5','insureType5_p','insureType5_t','insureType5_v','insureType5_tx','insureType5_m'
									   );
		var selectorClickArray= new Array(
														   'index.php?ui=sales&cat=1','index.php?ui=sales&subcat=26','index.php?ui=sales&subcat=27',
														   'index.php?ui=sales&cat=2','index.php?ui=sales&subcat=5','index.php?ui=sales&subcat=6','index.php?ui=sales&subcat=8',
														   'index.php?ui=sales&cat=4','index.php?ui=sales&subcat=13','index.php?ui=sales&subcat=14','index.php?ui=sales&subcat=16',
														   'index.php?ui=sales&cat=3','index.php?ui=sales&subcat=9','index.php?ui=sales&subcat=10','index.php?ui=sales&subcat=12','index.php?ui=sales&subcat=19','index.php?ui=sales&subcat=23',
														   'index.php?ui=sales&cat=18','index.php?ui=sales&subcat=29','index.php?ui=sales&subcat=30','index.php?ui=sales&subcat=32','index.php?ui=sales&subcat=34','index.php?ui=sales&subcat=37'
									   );
		
		for(aP=0;aP<divArray.length;aP++){
				currentItem=document.getElementById(divArray[aP]);
				if(currentItem){
						currentItem.onmouseover=function(){
								if(this.className!='company_over'){
										this.className='company_over';
										showDesc(this.id);
								}
						}
						
						currentItem.onmouseout=function(){
								if(this.className=='company_over')
										if(isCompanyChecked(this.id))
												this.className='company_selected';
										else
												this.className='company_nonSelected';
										hideDesc();
						}
						
						currentItem.onclick=function(){
								var newArray= new Array();
								var isFound=false;
								for(ii=0;ii<checkedArray.length;ii++)
									if(this.id.substr(7,this.id.length-7)!=checkedArray[ii])
											newArray.push(checkedArray[ii]);
									else
											isFound=true;
											
									if(isFound!=true)
											newArray.push(this.id.substr(7,this.id.length-7));									
									checkedArray=newArray;
		
								ajaxGetData('content/modules/ajax/quickfilter.php?arg='+checkedArray.toString()+'&timeserial='+getTimeSerial(),removeWaitStatusnWrite);	
								resultBoxOver=document.getElementById('resultBoxOverlay');				
								resultBoxOver.className='resultBox_over'; 
								resultBoxz=document.getElementById('resultBox');
								resultBoxz.className='resultBox_hide';
						}		
				}

		}
		
		for(aP=0;aP<selectorArray.length;aP++){
				currentItem=document.getElementById(selectorArray[aP]);
				if(currentItem){
						currentItem.onmouseover=function(){;
								if(this.className=='selector_nonSelected'){
										this.className='selector_selected';
										showDesc(this.id);
								}
						}
						
						currentItem.onmouseout=function(){
								if(this.className=='selector_selected'){
										this.className='selector_nonSelected';
										hideDesc();
								}
						}
						
						currentItem.onclick=function(){
								var isFound=false;
								for(ii=0;ii<selectorArray.length;ii++)
									if(this.id==selectorArray[ii]){
											location=selectorClickArray[ii];
											return true;
									}
						}					
				}

		}

}

var IE = document.all?true:false
if (!IE) document.captureEvents(Event.MOUSEMOVE)
	document.onmousemove = getMouseXY;

var tempX = 0;
var tempY = 0;

function getMouseXYx() {
	  if (IE) { 
			tempX = window.event.clientX + document.body.scrollLeft;
			tempY = window.event.clientY + document.body.scrollTop;
	  } else {  
			tempX = pageX;
			tempY = pageY;
	  }  
	  if (tempX < 0){tempX = 0}
		  if (tempY < 0){tempY = 0}  
		  return true;
	}

function getMouseXY(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	tempX = posx;
	tempY = posy;
	return true;
	// posx and posy contain the mouse position relative to the document
	// Do something with this information
}


function showDesc(arg) {
		var divArray= new Array('company1','company2','company3','company17','company5','company6','company7','company8','company9','company10','company11','company12','company13','company14','company15','company16');
		var descArray= new Array("บริษัท วิริยะประกันภัย จำกัด","บริษัท อาคเนย์ประกันภัย (2000)จำกัด","บริษัท เทเวศประกันภัย จำกัด","บริษัท บริษัท ไทยพาณิชย์สามัคคีประกันภัย จำกัด (มหาชน)","บริษัท เอเชียประกันภัย1950 จำกัด","บริษัท เจ้าพระยาประกันภัย จำกัด (มหาชน)","เอ็ม เอส ไอ จี ประกันภัย (ประเทศไทย) จำกัด","บริษัท ประกันภัยศรีเมือง จำกัด","บริษัท อลิอันซ์ ซี. พี. ประกันภัย จำกัด","บริษัท ประกันคุ้มภัย จำกัด ","บริษัท มิตรแท้ประกันภัย จำกัด","บริษัท แอลเอ็มจี ประกันภัย จำกัด","บริษัท คูเนีย ประกันภัย (ประเทศไทย) จำกัด","บริษัท กมลประกันภัย จำกัด (มหาชน)","บริษัท ไทยศรีประกันภัย จำกัด","บริษัท กรุงเทพประกันภัย จำกัด (มหาชน)");

		var selectorIDArray= new Array(
														   'insureType1','insureType1_p','insureType1_t',
														   'insureType2','insureType2_p','insureType2_t','insureType2_v',
														   'insureType3','insureType3_p','insureType3_t','insureType3_v',
														   'insureType4','insureType4_p','insureType4_t','insureType4_v','insureType4_tx','insureType4_m',
														   'insureType5','insureType5_p','insureType5_t','insureType5_v','insureType5_tx','insureType5_m'
									   );
		var selectorDescArray= new Array(
										 					'ประกันชั้น 1','ประกันชั้น 1 รถเก๋ง','ประกันชั้น 1 รถกระบะ',
															'ประกันชั้น 2+','ประกันชั้น 2+ รถเก๋ง','ประกันชั้น 2+ รถกระบะ','ประกันชั้น 2+ รถตู้',
															'ประกันชั้น 3+','ประกันชั้น 3+ รถเก๋ง','ประกันชั้น 3+ รถกระบะ','ประกันชั้น 3+ รถตู้',
															'ประกันชั้น 3','ประกันชั้น 3 รถเก๋ง','ประกันชั้น 3 รถกระบะ','ประกันชั้น 3 รถตู้','ประกันชั้น 3 รถแท็กซี่','ประกันชั้น 3 จักรยานยนต์',
															'พรบ.','พรบ. รถเก๋ง','พรบ. รถกระบะ','พรบ. รถตู้','พรบ. รถแท็กซี่','พรบ. จักรยานยนต์'
										 );

		for(aP=0;aP<divArray.length;aP++){
				if(divArray[aP]==arg)
						arg=descArray[aP];
		}
		for(aP=0;aP<selectorIDArray.length;aP++){
				if(selectorIDArray[aP]==arg)
						arg=selectorDescArray[aP];
		}
		
		x=document.getElementById('infoPopup');
		x.style.display="block";
		x.style.top=tempY;
		x.style.left=tempX;		
		x.innerHTML = arg;
		

}

function hideDesc(){
	x=document.getElementById('infoPopup');
	x.style.display="none";
	
}

function removeWaitStatusnWrite(responseText){
		resultBoxz=document.getElementById('resultBox');
		resultBoxz.className='resultBox';
		resultBoxz.innerHTML=responseText;	
}

function isCompanyChecked(ItemName){
		
		if(checkedArray.length==0)
				return false;
		else
				for(ii=0;ii<checkedArray.length;ii++)
					if(ItemName.substr(7,ItemName.length-7)==checkedArray[ii])
							return true;
				return false;
}

function ajaxGetData(url, callback){
	var objRequest = false;
	
	if(window.XMLHttpRequest){
		objRequest = new XMLHttpRequest() ;
	}else if(window.ActiveXObject){
		objRequest = new ActiveXObject("Microsoft.XMLHTTP") ;
	}

	if(objRequest){
		objRequest.open("GET", url) ;
		objRequest.onreadystatechange = handleResponse ;
		objRequest.send(null) ;
		
	}
	
	function handleResponse(){
		if (objRequest.readyState == 4 && objRequest.status == 200){
			callback(objRequest.responseText) ;
			delete objRequest ;
			objRequest = null ;
		}
	}
	
}

function quickFilter(){
	var filterForm = document.forms["filterForm"];
	filterForm.action="index.php?ui=filter";
	filterForm.submit();			
}	
