// JavaScript Document

function winPop(URL,name, w, h) {
  //set default to arg
  URL  = typeof(URL)  != 'undefined' ? URL  : "empty";
  name = typeof(name) != 'undefined' ? name : "WinPoP";
  w	   = typeof(w)    != 'undefined' ? w    : "350";
  h    = typeof(h)    != 'undefined' ? h    : "200";
  x	   = 0;
  y    = 0;
  if(screen){
 	y  = (screen.availHeight - h)/2;
	x  = (screen.availWidth - w)/2;
  }
  
  if(URL!="empty"){
    window.open(URL, name,"resizable=yes,scrollbars=yes,toolbar=no,status=no,height="+h+",width="+w+",top="+y+",left="+x);			
  }else{ alert("Error:: winPop() : Undefined URL ");}
}

function pop_check_member_id(mem,URL){
  if(isUsername(mem)){
	  URL+="?mem_id="+mem.value;
	winPop(URL,'chkmem',220,130);  
  }else{
	alert("User name must be characters, numbers, _, - , lenght 5-20");
	mem.focus();
  }
}

function pop_reset_member_pw(mem){
  if(mem!='undefined'){
	  var URL ="win_reset_member_pw.php";
	  	  URL+="?mem_id="+mem;
	winPop(URL,'chgpw',290,210);  
  }
}

/* 
 submit the accecpt button from Member ID Popup check
 then disabled member id function
*/
function pop_accept_member_id(){
	window.opener.document.getElementById('mid').readOnly=true;
	window.opener.document.getElementById('avi_id').value=1;
	
	window.opener.document.getElementById('chk_id').disabled=true;
	window.opener.document.getElementById('chk_id').style.display = 'none';
	
	window.opener.document.getElementById('chg').disabled=false;
	window.opener.document.getElementById('chg').style.display = '';

	window.opener.document.getElementById('name').focus();
	window.self.close();
}
/*
 set writable for text box and show check button
*/
function change_member_id(){
	document.getElementById('avi_id').value=0;
	
	document.getElementById('chk_id').disabled=false;
	document.getElementById('chk_id').style.display = '';
	
	document.getElementById('chg').disabled=true;
	document.getElementById('chg').style.display = 'none';
	
	document.getElementById('mid').readOnly=false;
	document.getElementById('mid').focus();
}

/* 
  show example color code for add new tee module
*/
function show_color_code(code,obj_result_id){
	document.getElementById(obj_result_id).style.backgroundColor='#'+code;
}

/*  calculate total par for add tee  */
function course_tee_total_form(me,tee_id){
  if(!isEmpty(me) && !isNumeric(me)){
		alert("The value must be number only");	
		me.value=0;
		selectAll(me);
  }else{
		var f=me.form;
   
   //course rating elements
	var oc=f.elements['oc['+tee_id+']'];
	var fc=f.elements['fc['+tee_id+']'];
	var bc=f.elements['bc['+tee_id+']'];
   //slope rating elements
	var os=f.elements['os['+tee_id+']'];
	var fs=f.elements['fs['+tee_id+']'];
	var bs=f.elements['bs['+tee_id+']'];
   //par elements						 // set default
	var op=f.elements['op['+tee_id+']']; // var op_val = op.value!='' ? op.value:0;
	var fp=f.elements['fp['+tee_id+']']; // var fp_val = fp.value!='' ? fp.value:0;
	var bp=f.elements['bp['+tee_id+']']; // var bp_val = bp.value!='' ? bp.value:0;
	
     //calculate course rating of overall hole from front hole and back hole info	
	   if(!isEmpty(fc) && !isEmpty(bc) && isFloat(fc) && isFloat(bc)){
		oc.value=parseFloat(fc.value)+parseFloat(bc.value);//float(1 decimal),46.0-90.0
		oc.value=my_round(parseFloat(oc.value),1);//round and return 1 decimal
		check_course_value(oc,'all');
	   }
	  //calculate slope rating of overall hole from front hole and back hole info	
	   if(!isEmpty(fs) && !isEmpty(bs) && isInt(fs) && isInt(bs)){
		os.value =Math.round((parseInt(fs.value)+parseInt(bs.value))/2);//int,55-155
		check_slope_value(os);
	   }
	   //calculate totoal par of overall hole from front hole and back hole info	
	   if(!isEmpty(fp) && !isEmpty(bp) && isInt(fp) && isInt(bp)){
		op.value =(parseInt(fp.value)+parseInt(bp.value));//int,54-80
		check_par_value(op,'all');
	   }
  }
}
//course rating , float 1 decimal
var oc_max=90.0; 	var oc_min=46.0; 	//overall
var fc_max=47.0; 	var fc_min=23.0; 	//front and back
//slope rating , integer
var os_max=155;  	var os_min=55; 		//overall
//total par , integer
var op_max=80;   	var op_min=54; 		//overall
var fp_max=45;   	var fp_min=27; 		//front and back
//hole par
var hp_max=6;		var hp_min=3;		//par of every hole
/*admin save course form (a_new_course.php)*/
function check_save_course_frm(f,clu_hole){
  var msg=""; ai=new Array();
  var clu_hole = clu_hole!='undefined'?clu_hole:18;
  
  for(i=0; i<f.elements.length; i++){ // loop each from elements
	var elem =f.elements[i];
	var index=elem.name.substring(3,(parseInt(elem.name.length) - 1));
	var last_cha=elem.name.substr(parseInt(elem.name.length)-1);

	if(ai.indexOf(index)!=-1)//not found in array
		continue;
	if(last_cha==']'){
		//overall
		var oc = f.elements['oc['+index+']']; var oc_val = oc.value!=''?oc.value:0;
		var os = f.elements['os['+index+']']; var os_val = os.value!=''?os.value:0;
		var op = f.elements['op['+index+']']; var op_val = op.value!=''?op.value:0;
		var sum_all =parseInt(oc_val)+parseInt(os_val)+parseInt(op_val);
		
		if(clu_hole<18){//9 hole
			//change max and min value for 9 hole 
			oc_max=fc_max; oc_min=fc_min; op_max=fp_max; op_min=fp_min;
		}else{//18 hole
		//front
		var fc = f.elements['fc['+index+']']; var fc_val = fc.value!=''?fc.value:0;
		var fs = f.elements['fs['+index+']']; var fs_val = fs.value!=''?fs.value:0;
		var fp = f.elements['fp['+index+']']; var fp_val = fp.value!=''?fp.value:0;
			sum_all+=parseInt(fc_val)+parseInt(fs_val)+parseInt(fp_val);
		//back
		var bc = f.elements['bc['+index+']']; var bc_val = bc.value!=''?bc.value:0;
		var bs = f.elements['bs['+index+']']; var bs_val = bs.value!=''?bs.value:0;
		var bp = f.elements['bp['+index+']']; var bp_val = bp.value!=''?bp.value:0;
			sum_all+=parseInt(bc_val)+parseInt(bs_val)+parseInt(bp_val);	
		}//end clu_hole == 18
		
		if(sum_all<=0){
			ai.push(index);
		}else{

			if(!isEmpty(oc) && !isFloat(oc)){
				msg="Overall Course rating must be number only.";
				(tar = oc); break;
			}else{
			  if(!isBetween(parseFloat(oc_val),oc_min,oc_max)){
				msg="Overall Course rating must be between "+oc_min+" and "+oc_max+".";	
				(tar = oc); break;
			  }
			}
			
			if(!isEmpty(os) &&  !isInt(os)){
				msg="Overall Slope rating must be number only.";
				(tar = os); break;
			}else{
			  if(!isBetween(parseInt(os_val),os_min,os_max)){
				msg="Overall Slope rating must be between "+os_min+" and "+os_max+".";	
				(tar = os); break;
			  }
			}
			
			if(!isEmpty(op) &&  !isInt(op)){
				msg="Overall Par must be number only.";
				(tar = op); break;
			}else{
			  if(!isBetween(parseInt(op_val),op_min,op_max)){
				msg="Overall Par must be between "+op_min+" and "+op_max+".";	
				(tar = op); break;
			  }
			}
				
		}//end if sum_all
	}//end if elements not undefined
  }//end for elements.lenght

  if(msg!=""){alert(msg); selectAll(tar); return false;}
  else{ return true;}
}//end function

/*  calculate total par for add tee  */
function hole_total_form(){
   //par elements   
	var top=document.getElementById('tot_o_par');
	var f=top.form;
	var tfp=f.tot_f_par;
	var tbp=f.tot_b_par;
   //total of hole of this course
	var tho=f.hono;
   
   //define element for count
	var tfp_val=0;var tbp_val=0; 
   for(i=1; i<=tho.value; i++){
	var par=document.getElementById('p['+i+']');
	var par_val  = par.value  != '' ? par.value  : 0; //set default
	
	if(i<10)
	 tfp_val+=parseInt(par_val); //total front par
	else
	 tbp_val+=parseInt(par_val); //total back par

   }//end for
   //display value into text field
    top.value=parseInt(tfp_val)+parseInt(tbp_val);//int, 54-80
	tfp.value=tfp_val;
	tbp.value=tbp_val;
}

/* check slope value (55 - 155) integer*/
function check_slope_value(me){
  if(!isEmpty(me)){
	var msg="";
	var min_val = os_min;
	var max_val = os_max;
	var val=parseInt(me.value);
	 if(!isInt(me)){
		msg+="Slope rating must be number only.";
		me.value="";
	 }//end if not numeric value
	 else if(!isBetween(val,min_val,max_val)){ //if value between max and min then return true
		msg+="Slope rating must be between "+min_val+" and "+max_val+".";	
		me.value=min_val;
	 }//end if isBetween()
	 
	 if(msg!=""){//have error
	 	alert(msg);
		selectAll(me);
		return false;
	 }
  }//end if not empty
	 
}

/* check course value (46.0 - 90.0) float 1 decimal*/
function check_course_value(me,type){
  if(!isEmpty(me)){
	var msg="";
	if(type=='all'){//for overall hole
		var min_val = oc_min;
		var max_val = oc_max;
	}else{ //for front and back hole
		var min_val = fc_min;
		var max_val = fc_max;
	}
	var val=parseFloat(me.value);
	 if(!isFloat(me)){
		msg+="Course rating must be number only.";
		me.value="";
	 }//end if not numeric value
	 else if(!isBetween(val,min_val,max_val)){ //if value between max and min then return true
		msg+="Course rating must be between "+min_val+" and "+max_val+".";	
		me.value=min_val;
	 }//end if isBetween()
	 
	 if(msg!=""){//have error
	 	alert(msg);
		selectAll(me);
		return false;
	 }
  }//end if not empty
	 
}
/* check par value (54 - 80) integer*/
function check_par_value(me,type){
  if(!isEmpty(me)){
	var msg="";
	if(type=='all'){//for overall hole
		var min_val = op_min;
		var max_val = op_max;
	}else{//for front and back hole
		var min_val = fp_min;
		var max_val = fp_max;
	}
	var val=parseFloat(me.value);
	 if(!isInt(me)){
		msg+="Par entered must be number only.";
		me.value="";
	 }//end if not numeric value
	 else if(!isBetween(val,min_val,max_val)){ //if value between max and min then return true
		msg+="Par entered must be between "+min_val+" and "+max_val+".";	
		me.value=min_val;
	 }//end if isBetween()
	 
	 if(msg!=""){//have error
	 	alert(msg);
		selectAll(me);
		return false;
	 }
  }//end if not empty
	 
}
/*  calculate total par for add tee  */
function rating_total_form(me){
  if(!isEmpty(me) && !isNumeric(me)){
		alert("The value must be number only");	
		me.value=0;
		selectAll(me);
  }else{
		var f=me.form;
	   //par elements   	//course rating elements  //slope rating elements
		var top=f.tot_o_par;  var toc=f.tot_o_course;  var tos=f.tot_o_slope;
		var tfp=f.tot_f_par;  var tfc=f.tot_f_course;  var tfs=f.tot_f_slope;
		var tbp=f.tot_b_par;  var tbc=f.tot_b_course;  var tbs=f.tot_b_slope;
		
	
	   //calculate course rating of overall hole from front hole and back hole info	
	   if(!isEmpty(tfc) && !isEmpty(tbc) && isFloat(tfc) && isFloat(tbc)){
		toc.value=parseFloat(tfc.value)+parseFloat(tbc.value);//float(1 decimal),46.0-90.0
		toc.value=my_round(parseFloat(toc.value),1);
		check_course_value(toc,'all');
	   }
	  //calculate slope rating of overall hole from front hole and back hole info	
	   if(!isEmpty(tfs) && !isEmpty(tbs) && isInt(tfs) && isInt(tbs)){
		tos.value =Math.round((parseInt(tfs.value)+parseInt(tbs.value))/2);//int,55-155
		check_slope_value(tos);
	   }
	   //calculate totoal par of overall hole from front hole and back hole info	
	   if(!isEmpty(tfp) && !isEmpty(tbp) && isInt(tfp) && isInt(tbp)){
		top.value =(parseInt(tfp.value)+parseInt(tbp.value));//int,54-80
		check_par_value(top,'all');
	   }
  }//end else not numeric
 
}

/*  calculate total SCORE for add score  */
function total_score(me,course,slope){
  if(!isEmpty(me) && !isInt(me,true)){
		alert("Score must be number only");	
		me.value=0;
		selectAll(me);
  }else{
	
   //define total element
    var f= me.form;
	var hole_no=document.getElementById('hono');
	var elem_gross	  =f.gross;
	var elem_adj	  =f.adj_gross;
	var elem_diff	  =f.diff;
	var elem_hole_in1 =f.score_hole;
	var elem_par	  =f.score_par;
	var elem_eagle	  =f.score_eagle;
	var elem_dbogie	  =f.score_dbogie;
	var elem_birdie	  =f.score_birdie;
	var elem_bogie	  =f.score_bogie;
	
   //define element for count
	var gross_val=0;   var adj_val=0; 
	var st_hole_in1=0; var st_eagle=0;   var st_birdie=0;
	var st_par=0;      var st_dbogie=0;  var st_bogie=0;
   for(i=1; i<=hole_no.value; i++){
	var elem_score=document.getElementById('s['+i+']');
	var score_val  = elem_score.value  != '' ? elem_score.value  : 0; //set default

	var tmp_score=parseInt(score_val) - parseInt(s_par[i]);
	if(score_val>0){
		if(parseInt(s_par[i])==3 && tmp_score==-2){// hole in one (possible only par 3)
		  st_hole_in1++;
		}else if(tmp_score>2){ //more than double bogie adjust to bogie
		  tmp_score=2;
		}else if(tmp_score<-2){ //better than eagle 
		  st_eagle++;
		}
		switch(tmp_score){
			case -2: st_eagle++;   break;
			case -1: st_birdie++;  break;
			case 0 : st_par++;     break;
			case 1 : st_bogie++;   break;
			case 2 : st_dbogie++;  break;
		}//end switch
	}//end if socre_val >0
	  gross_val+=parseInt(score_val); //total score
	  adj_val+=(parseInt(s_par[i]) + parseInt(tmp_score)); //maximum is +2 (double bogie)

   }//end for
   //display value into text field
    elem_gross.value 	  = gross_val;
	elem_adj.value   	  = adj_val;
	elem_hole_in1.value   = st_hole_in1;
	elem_par.value   	  = st_par;
	elem_eagle.value   	  = st_eagle;
	elem_dbogie.value     = st_dbogie;
	elem_birdie.value     = st_birdie;
	elem_bogie.value      = st_bogie;
	cal_differential(elem_diff,course,slope,adj_val)
  }//end else if check numberic
}

function chg_method(f,req){
	f.method=req;
	f.submit();
}


function cal_differential(elem_result,course,slope,ajs_gross){
	var diff_val=((parseInt(ajs_gross)-parseFloat(course))*113)/parseInt(slope);
	elem_result.value = my_round(diff_val,1);
}


//rounds the input number to the desired precision
//and returns the rounded number
function my_round(inputNum, desiredPrecision){
 var precisionGuide = Math.pow(10, desiredPrecision);
 var rs_round= (Math.round(inputNum * precisionGuide) / precisionGuide );
 var result= addZeroesToPrecision(rs_round, desiredPrecision);
 return result;
}

//converts the input number into a string and adds zeroes
//until the desired precision is reached and then
//returns the new string
function addZeroesToPrecision(inputNum, desiredPrecision){
 var numString = inputNum + "";
 var afterDecimalString = "";
 var startPoint=numString.search(/\./);
 if(startPoint!=-1) // A float number
	 afterDecimalString = numString.substring(startPoint + 1);
 else // Not a float 
 	 numString+='.';

 while (afterDecimalString.length < desiredPrecision) {
   afterDecimalString += "0";
   numString += "0";
 }
 return(numString);
}

/*check input value is between the lenght or not
  return TRUE if value between in the lenght
  return FALSE if not in lenght
*/
function isBetween(num,min_val,max_val){
	if (min_val > num || max_val < num) {
		return false;  // not between
	}else return true; // between value
}

/*===================================================
     RESERVATION FUNCTION 
  ===================================================*/
function reserv_select_date(date_value,label_text){
	document.getElementById("reserv_date").value=date_value;
	label_text = "Selected date : <b> " + label_text +" </b>";
	document.getElementById("reserv_date_label").innerHTML=label_text;
	document.getElementById("reserv_date").form.submit();
}

  
