// JavaScript Document for Search Box 'Search Golf Course' and 'Book Tee Time'
function jsSearchMethod_OnChange(searchMethod, objForm){
	//clearForms();
	//document.getElementById('hdnGolfCourseOID').value='';
    document.getElementById('hdnFromSearchGolf').value='';
	switch(searchMethod){
		case 'GC'://Search Golf Course

					document.getElementById('hddSearchMethod').value = 'GC';
					document.getElementById('tbDateTimeGolfer').style.display = 'none';
					document.getElementById('tbHeaderSelect').style.display = 'none';
					jsSwitchClass_OnOff('left');
					jsSearchType_OnChange('Q');
					jsQuickBy_Onchanged('GC');
					document.getElementById('trTextDetail').style.display = '';
					
					fn_showAllGolfCourse();//<-- 9 Mar 10 เพิ่มให้ Load drpGolfCourse ใหม่ทุกครั้งที่เปลี่ยน Search Method สำหรับ List ที่ต่างกันของ 2 Search
					break;

		case 'TT'://Book Tee Time
					document.getElementById('hddSearchMethod').value = 'TT';
					document.getElementById('tbDateTimeGolfer').style.display = '';
					document.getElementById('tbHeaderSelect').style.display = '';
					jsSwitchClass_OnOff('right');
					jsSearchType_OnChange('Q');
					jsQuickBy_Onchanged('GC');
					document.getElementById('trTextDetail').style.display = 'none';

					fn_showAllGolfCourse();//<-- 9 Mar 10 เพิ่มให้ Load drpGolfCourse ใหม่ทุกครั้งที่เปลี่ยน Search Method สำหรับ List ที่ต่างกันของ 2 Search
					break;
	}//switch
}//jsSearchMethod_OnChange


function jsSearchType_OnChange(searchType){
    document.getElementById('hdnFromSearchGolf').value='';
	switch(searchType){
		case 'A'://Advance
					document.getElementById('hddSearchType').value = 'A';
					jsSwitchDisplay_OnOff('tbAdvanceSearch', 'tbQuickSearch');
					jsSwitchImage_SRC('imgArrowAdvance', 'images/Searchbox/arrow_down.gif');
					jsSwitchImage_SRC('imgArrowQuick', 'images/Searchbox/arrow_up.gif');

					document.getElementById("tdAdvanceSearch").className = 'txtBlackItalic_on';
					document.getElementById("tdQuickSearch").className = 'txtBlackItalic';
					

					//document.getElementById("IsAdvSearch").value="True";
					break;
		case 'Q'://Quick
					document.getElementById('hddSearchType').value = 'Q';
					jsSwitchDisplay_OnOff('tbQuickSearch', 'tbAdvanceSearch');
					jsSwitchImage_SRC('imgArrowAdvance', 'images/Searchbox/arrow_up.gif');
					jsSwitchImage_SRC('imgArrowQuick', 'images/Searchbox/arrow_down.gif');

					document.getElementById("tdAdvanceSearch").className = 'txtBlackItalic';
					document.getElementById("tdQuickSearch").className = 'txtBlackItalic_on';

					
					break;
	}//switch
}//jsSearchMethod_OnChange

function jsQuickBy_Onchanged(paramSelectedValue){
	document.getElementById('hddQuickSearchBy').value = paramSelectedValue;
	CurrentSearchMethod = document.getElementById('hddSearchMethod').value;
	var objSearch=document.getElementById('drpQuickby');
	for(var i=0;i<objSearch.options.length;i++)
	{
	    if(objSearch.options[i].value==paramSelectedValue)
	    {
	        objSearch.options[i].selected=true;
	        break;
	    }
	}
	switch(paramSelectedValue){
	      case 'GC':
		  				document.getElementById('trGC').style.display = '';
		  				document.getElementById('trPC').style.display = 'none';
		  				document.getElementById('trDT').style.display = 'none';
		  				document.getElementById('trAR').style.display = 'none';
		  				document.getElementById('trSC').style.display = 'none';
						break;
          case 'PC':
		  				document.getElementById('trGC').style.display = 'none';
		  				document.getElementById('trPC').style.display = '';
		  				document.getElementById('trDT').style.display = 'none';
						if(CurrentSearchMethod == 'TT'){
		  					document.getElementById('trAR').style.display = '';
		  					document.getElementById('divRadioPrice').style.display = 'none';
						}else{				
		  					document.getElementById('trAR').style.display = 'none';
		  					document.getElementById('divRadioPrice').style.display = '';
						}//if
		  				document.getElementById('trSC').style.display = 'none';
						break;
          case 'DT':
		  				document.getElementById('trGC').style.display = 'none';
		  				document.getElementById('trPC').style.display = 'none';
		  				document.getElementById('trDT').style.display = '';
		  				document.getElementById('trAR').style.display = 'none';
		  				document.getElementById('trSC').style.display = 'none';
						break;
          case 'AR':
		  				document.getElementById('trGC').style.display = 'none';
		  				document.getElementById('trPC').style.display = 'none';
		  				document.getElementById('trDT').style.display = 'none';
		  				document.getElementById('trAR').style.display = '';
		  				document.getElementById('trSC').style.display = 'none';
						break;
          case 'SC':
		  				document.getElementById('trGC').style.display = 'none';
		  				document.getElementById('trPC').style.display = 'none';
						if(CurrentSearchMethod == 'TT'){
		  					document.getElementById('trAR').style.display = '';
						}else{				
		  					document.getElementById('trAR').style.display = 'none';
						}//if
		  				document.getElementById('trDT').style.display = 'none';
		  				document.getElementById('trSC').style.display = '';
						break;
	}//switch
}//jsQuickBy_Onchanged

function jsCheckValidValue(objForm,hour){

	CurrentSearchType = document.getElementById('hddSearchType').value;//A=Advance, Q=Quick Search
	CurrentSearchMethod = document.getElementById('hddSearchMethod').value;//GC=Search Golf Course, TT=Book Tee Time
	
	CurrentQuickSearch = document.getElementById('hddQuickSearchBy').value;//GC=Golf Course, PC=Pricing, DT=Distance, AR=Area, SC=Scenery
	
	var fail=false;
	var errorMessage="";
	if(CurrentSearchMethod == 'TT'){//Book Tee Time
		if(CurrentSearchType == 'A'){//Advance Search
		    var GCOID=document.getElementById('drpGolfCourse').value;
            if(GCOID=='ALL')
            {
                fail=true;
                errorMessage+='Please select Golf course.\n';
            }
		 //if(!jsFn_ChkField(objForm.drpGolfCourse,"select","Golf course."))	return false;
			//validate field "Golf Course"
		}//if
			
		//alert("--->"+objForm.drpArea.options[objForm.drpArea.selectedIndex].value);

		if(CurrentSearchType == 'Q'){//Quick Search
			//alert(CurrentQuickSearch);
			switch(CurrentQuickSearch){
				case 'GC':   
				            var name=document.getElementById('txtGolfCourseName').value;
                            if(name=='')
                            {
                                fail=true;
                                errorMessage+='Please input Golf course.\n';
                            }
					        //if(!jsFn_ChkField(objForm.txtGolfCourseName,"text","Golf course."))	return false;//validate field "Golf Course"
							break;
				case 'PC':   
				            var area=document.getElementById('drpArea').value;
                            var price=document.getElementById('drpPrice').value;
                            if(area=='ALL')
                            {
                                fail=true;
                                errorMessage+='Please select Area.\n';
                            }
                            if(price=='ALL')
                            {
                                fail=true;
                                errorMessage+='Please select price.\n';
                            }
                            
				           /*if(objForm.drpArea.options[objForm.drpArea.selectedIndex].value == 'ALL')
				           {
				             alert('Please select Area');
				           }//if
				          
				           
				            if(objForm.SearchPrice.options[objForm.SearchPrice.selectedIndex].value == 'ALL')
				           {
				               alert('Please select Price');
				           } //if  //validate field "Area", "Price"
				          */
				           
					         //if(!jsFn_ChkField(objForm.drpArea,"select","Area.","All"))	return false;
				            //if(!jsFn_ChkField(objForm.SearchPrice,"select","Price.",''))	return false;//validate field "Area", "Price"
							break;
				case 'DT': 
				
				            var from=document.getElementById('drpDistanceFrom').value;
                            var rad=document.getElementById('txtRadius').value; 
                            
                            if(from=='ALL')
                            {
                                fail=true;
                                errorMessage+='Please select From.\n';
                            }
                            if(rad=='')
                            {
                                fail=true;
                                errorMessage+='Please input radius.\n';
                            }
                            /*
				           if(objForm.SearchDistanceFrom.options[objForm.SearchDistanceFrom.selectedIndex].value == 'ALL')
				           {
				               alert('Please select From');
				           }//if
				            if(!jsFn_ChkField(objForm.txtRadius,"text","Radius."));*/
				           //validate field "Form", "Radius"
							break;
				case 'AR':  
				            var area=document.getElementById('drpArea').value;
                            if(area=='ALL')
                            {
                                fail=true;
                                errorMessage+='Please select Area.\n';
                            }
				            /*
				            if(objForm.drpArea.options[objForm.drpArea.selectedIndex].value == 'ALL')
				           {
				             alert('Please select Area');
				           }//if*/
				           //validate field "Area"
							break;
				case 'SC':  
				            var area=document.getElementById('drpArea').value;
                            var scenary=document.getElementById('drpScenary').value;
                            if(area=='ALL')
                            {
                                fail=true;
                                errorMessage+='Please select Area.\n';
                            }
                            if(scenary=='ALL')
                            {
                                fail=true;
                                errorMessage+='Please select scenery.\n';
                            }
                            /*
				           if(objForm.drpArea.options[objForm.drpArea.selectedIndex].value == 'ALL')
				           {
				             alert('Please select Area');
				           }//if
				            if(objForm.SearchScenary.options[objForm.SearchScenary.selectedIndex].value == 'ALL')
				           {
				             alert('Please select Scenery');
				           }//if*/
				           //validate field "Area", "Scenery"
							break;
			}//switch
			
			
		}//if
		//Fix to validate field "Date", "Time","Golfer"
		    var date=document.getElementById('txtDate').value;
            if(date=='')
            {
                fail=true;
                errorMessage+='Please input Date.\n';
            } 
            var time=document.getElementById('txtTime').value;
            if(time=='')
            {
                fail=true;
                errorMessage+='Please input Time.\n';
            }
            else
            {
                if(!fn_checkRegular(time))
                {
                    fail=true;
                    errorMessage+='Incorrect time format.\n';
                }
            }
            
            var allowDateTime=new Date();
            var hr=allowDateTime.getHours();
            var sumHour=parseInt(hr,10)+parseInt(hour,10);
            allowDateTime.setHours(sumHour);
            
            var arrDate=date.split('/');
            var arrTime=time.split('.');
            
            var searchDateTime=new Date(parseInt(arrDate[2],10), parseInt(arrDate[1],10)-1, parseInt(arrDate[0],10), parseInt(arrTime[0],10), parseInt(arrTime[1],10), 0, 0);
            
            if(allowDateTime>=searchDateTime)
            {
                fail=true;
                errorMessage+='Sorry, you must reserve at lease '+hour+' hour before tee off.\nPlease specifiy the date/time again.\n';
            }
			/*if(!jsFn_ChkField(objForm.txtDate,"text","Date."))	return false;
			if(!jsFn_ChkField(objForm.txtTime,"text","Time."))	return false;
			if(objForm.drpGolfer.options[objForm.drpGolfer.selectedIndex].value == 'ALL')
		     {
			     alert('Please select Golfer');
			  }//if*/
	}//if

	if (fail) {
	    alert(errorMessage);
	    return false;
	}
	else {
	return true;
	}
	
	alert(document.getElementById('submitted').value);
}//jsCheckValidValue

function jsSwitchClass_OnOff(tabActived) {
	

	switch (tabActived) {
	    case "left":
          
	        document.getElementById("tdTabSearchBox").style.background = "url(images/Searchbox/L_Actived.gif)";
            document.getElementById("tdLTab").className = 'LTab_on';
	        document.getElementById("tdRTab").className = 'RTab';
	        break;
	    case "right":

	        document.getElementById("tdTabSearchBox").style.background = "url(images/Searchbox/R_Actived.gif)";
	        document.getElementById("tdLTab").className = 'LTab';
	        document.getElementById("tdRTab").className = 'RTab_on';


	        break;
	} //switch

	
}//jsSwitchClass_OnOff


function jsSwitchDisplay_OnOff(eleIDOn, eleIDOff){

	document.getElementById(eleIDOn).style.display = '';
	document.getElementById(eleIDOff).style.display = 'none';
						
}//jsSwitchDisplay_OnOff


function jsSwitchImage_SRC(imgID, newIMG){

	document.getElementById(imgID).src = newIMG;		
	
}//jsSwitchImage_SRC

//Check Re-Submit

submitted = false;
function goSubmit(){
	if(submitted==false){
		submitted = true;
		document.getElementById('submitted').value = 'true';
		return true;
	}else{
		alert("Your request is processing (Please Wait).\nPlease Click Submit Once Only.\nClicking the Submit button multiple times will result in multiple charges on your credit card.");
		return false;
	}//if
}//jsfn
//-->

//check only time format
function isTimeKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    //alert(charCode);
    if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode!=46)
    {
        return false;
    }
    return true;
}

//check regular format time
function fn_checkRegular(myvalue)
{
    var res=false;
    var re = /(00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23)[.](0|1|2|3|4|5)\d{1}/;
    if (!re.test(myvalue))
    {
        //alert('false');
        res=false;
    }
    else
    {
        //alert('true');
        res=true;
    }
    return res;
}
