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) { 

	//alert("heloo");
			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() {
		
		requestInfo('showTable.php?mode=list','memberinfo','');
}
	
	function save_data() {
		
		    var newid=document.getElementById("newid").value;
			
			var MobileNo=document.getElementById("MobileNo").value;
			var Email=document.getElementById("Email").value;
			var Website=document.getElementById("Website").value;
			var URN=document.getElementById("URN").value;
			var CustomerId=document.getElementById("CustomerId").value;
			
			var Title=document.getElementById("Title").value;
			var FirstName=document.getElementById("FirstName").value;
			var SurName=document.getElementById("SurName").value;
			var Co=document.getElementById("Co").value;
			var Co1=document.getElementById("Co1").value;
			
			var Address1=document.getElementById("Address1").value;
			var Address2=document.getElementById("Address2").value;
			var Address3=document.getElementById("Address3").value;
			var City=document.getElementById("City").value;
			var Pin=document.getElementById("Pin").value;
			
			var State=document.getElementById("State").value;
			var Tel=document.getElementById("Tel").value;
			var Fax=document.getElementById("Fax").value;
			var SrNo=document.getElementById("SrNo").value;
			
			var Field1=document.getElementById("Field1").value;
			var Field2=document.getElementById("Field2").value;
			var Field3=document.getElementById("Field3").value;
			
			var checkValidation=emptyValidation('MobileNo');
			//var checkValidation=emptyValidation('id~MobileNo~Title~FirstName~SurName~CustomerId~Address~EmailId~Phone1~Field1~Field2~Field3');
		if(checkValidation==true) {
			requestInfo('showTable.php?mode=save_new&newid='+newid+'&MobileNo='+MobileNo+'&Email='+Email+'&Website='+Website+'&URN='+URN+'&CustomerId='+CustomerId+'&Title='+Title+'&FirstName='+FirstName+'&SurName='+SurName+'&Co='+Co+'&Co1='+Co1+'&Address1='+Address1+'&Address2='+Address2+'&Address3='+Address3+'&City='+City+'&Pin='+Pin+'&State='+State+'&Tel='+Tel+'&Fax='+Fax+'&SrNo='+SrNo+'&Field1='+Field1+'&Field2='+Field2+'&Field3='+Field3,'memberinfo','');
		} 
	}
	
	function update_data() {
		
		
			var prev_id=document.getElementById("prev_id").value;
			var prev_GroupId=document.getElementById("prev_GroupId").value;
			
			var MobileNo=document.getElementById("MobileNo").value;
			var Email=document.getElementById("Email").value;
			var Website=document.getElementById("Website").value;
			var URN=document.getElementById("URN").value;
			var CustomerId=document.getElementById("CustomerId").value;
			
			var Title=document.getElementById("Title").value;
			var FirstName=document.getElementById("FirstName").value;
			var SurName=document.getElementById("SurName").value;
			var Co=document.getElementById("Co").value;
			var Co1=document.getElementById("Co1").value;
			
			var Address1=document.getElementById("Address1").value;
			var Address2=document.getElementById("Address2").value;
			var Address3=document.getElementById("Address3").value;
			var City=document.getElementById("City").value;
			var Pin=document.getElementById("Pin").value;
			
			var State=document.getElementById("State").value;
			var Tel=document.getElementById("Tel").value;
			var Fax=document.getElementById("Fax").value;
			var SrNo=document.getElementById("SrNo").value;
			
			var Field1=document.getElementById("Field1").value;
			var Field2=document.getElementById("Field2").value;
			var Field3=document.getElementById("Field3").value;
			
			var checkValidation=emptyValidation('MobileNo');
		if(checkValidation==true) {
		requestInfo('showTable.php?mode=update_data&MobileNo='+MobileNo+'&Email='+Email+'&Website='+Website+'&URN='+URN+'&CustomerId='+CustomerId+'&Title='+Title+'&FirstName='+FirstName+'&SurName='+SurName+'&Co='+Co+'&Co1='+Co1+'&Address1='+Address1+'&Address2='+Address2+'&Address3='+Address3+'&City='+City+'&Pin='+Pin+'&State='+State+'&Tel='+Tel+'&Fax='+Fax+'&SrNo='+SrNo+'&Field1='+Field1+'&Field2='+Field2+'&Field3='+Field3+'&prev_id='+prev_id+'&prev_GroupId='+prev_GroupId,'memberinfo','');
		
		}
		//if(checkValidation==true) {
		//	requestInfo('showTable.php?mode=update_data&MobileNo='+MobileNo+'&Title='+Title+'&FirstName='+FirstName+'&SurName='+SurName+'&CustomerId='+CustomerId+'&Address='+Address+'&EmailId='+EmailId+'&Phone1='+Phone1+'&Field1='+Field1+'&Field2='+Field2+'&Field3='+Field3+'&prev_id='+prev_id,'showTable','');
		//} 
	}

function update_file() {
		
		
			var groupId=document.getElementById("groupId").value;
			var attachlist=document.getElementById("attachlist").value;
			
			var checkValidation=emptyValidation('attachlist');
		if(checkValidation==true) {
		requestInfo('showTable.php?mode=uploadFile&groupId='+groupId+'&attachlist='+attachlist,'memberinfo','');
		}
	}
	
	
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;
}

/* file upload with ajax */
function startUpload(){
      document.getElementById('f1_upload_process').style.visibility = 'visible';
      document.getElementById('f1_upload_form').style.visibility = 'hidden';
      return true;
}

function stopUpload(success){
      var result = '';
      if (success == 1){
         result = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
      }
      else {
         result = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
      }
      document.getElementById('f1_upload_process').style.visibility = 'hidden';
      document.getElementById('f1_upload_form').innerHTML = result + '<label>File:<input name="attachlist" type="file" size="30" /><\/label><label><input type="submit" name="list" class="sbtn" value="Upload" /><\/label>';
      document.getElementById('f1_upload_form').style.visibility = 'visible';      
      return true;   
}
