function getHTTPObject() {
  var xmlhttp;
 
  if(window.XMLHttpRequest){
    xmlhttp = new XMLHttpRequest();
  }
  else if (window.ActiveXObject){
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    if (!xmlhttp){
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    
}
  return xmlhttp;

  
}
var http = getHTTPObject(); // We create the HTTP Object

/*
	Funtion Name=requestInfo 
	Param = url >> Url to call : id = Passing div id for multiple use ~ as a seprator for eg. div1~div2 :
	redirectPage >> if you like to redirect to other page once the event success then 
	the response text = 1 and the redirectPage not left empty
*/

    function requestInfo(url,id,redirectPage) {      
		var temp=new Array();
			http.open("GET", url, true);
			http.onreadystatechange = function() {
				if (http.readyState == 4) {
				  if(http.status==200) {
			  		var results=http.responseText;
					if(redirectPage=="" || results!="1") {
						
						var temp=id.split("~"); // To display on multiple div 
						//alert(temp.length);
						var r=results.split("~"); // To display multiple data into the div 
						//alert(temp.length);
						if(temp.length>1) {
							for(i=0;i<temp.length;i++) {	
								//alert(temp[i]);
								document.getElementById(temp[i]).innerHTML=r[i];
							}
						} else {
							document.getElementById(id).innerHTML = results;
						}	
					} else {
						//alert(results);
						window.location.href=redirectPage;			
					}
				  } 
  				}
			};
			http.send(null);
       }

/*
	Function Name= emptyValidation
	Desc = This function is used to validation for the empty field 
	Param fieldList = This arguments set as a string varialble. you just need to supply the textbox name
	if the textbox is multiple then supply with ~ separator for eg. username~password
*/
function emptyValidation(fieldList) {
		
		var field=new Array();
		field=fieldList.split("~");
		var counter=0;
		for(i=0;i<field.length;i++) {
			if(document.getElementById(field[i]).value=="") {
				document.getElementById(field[i]).style.backgroundColor="#FF0000";
				counter++;
			} else {
				document.getElementById(field[i]).style.backgroundColor="#FFFFFF";	
			}
		}
		if(counter>0) {
				alert("The Field mark as red could not left empty");
				return false;
				
		}  else {
			return true;
		}
		
}

function init_table() {
 var hcid=document.getElementById("hcid").value;

		requestInfo('showtb.php?mode=list','showTable','');
	}
	
	function save_data() {
                       var hcid=document.getElementById("hcid").value;
			var bed=document.getElementById("bed").value;
			var day1=document.getElementById("day1").value;
			var day2=document.getElementById("day2").value;
                        var day3=document.getElementById("day3").value;
                       var day4=document.getElementById("day4").value;
                       var day5=document.getElementById("day5").value;
                       var day6=document.getElementById("day6").value;
			var checkValidation=emptyValidation('bed~day1~day2~day3~day4~day5~day6');
	
		if(checkValidation==true) {
			requestInfo('showTable.php?mode=save_new&hc_id='+hcid+'&bed='+bed+'&day1='+day1+'&day2='+day2+'&day3='+day3+'&day4='+day4+'&day5='+day5+'&day6='+day6,'showTable','');
		} 

	}
	
	function update_data() {
			var prev_id=document.getElementById("prev_id").value;
                      var hcid=document.getElementById("hcid").value;
			var id=document.getElementById("id").value;
		        var day1=document.getElementById("day1").value;
                        var day2=document.getElementById("day2").value;
                      var day3=document.getElementById("day3").value;
                      var day4=document.getElementById("day4").value;
                      var day5=document.getElementById("day5").value; 
                     var day6=document.getElementById("day6").value; 
                  var checkValidation = emptyValidation('id~hcid~day1~day2~day3~day4~day5~day6');
	
if(checkValidation==true) {
requestInfo('showTable.php?mode=update_data&id='+id+'&hc_id='+hcid+'&day1='+day1+'&day2='+day2+'&day3='+day3+'&day4='+day4+'&day5='+day5+'&day6='+day6+'&prev_id='+prev_id,'showTable','');
		
 } 
	}
	
function confirmLink(theLink)
{
    var is_confirmed = confirm('Are you sure to delete this record?\n\nThis will permanently delete the Record!');
    if (is_confirmed) {
        theLink.href += '';
    }
    return is_confirmed;
}
