function initobject(){
 			var A;
 			var msxmlhttp = new Array(
				'Msxml2.XMLHTTP.5.0',
				'Msxml2.XMLHTTP.4.0',
				'Msxml2.XMLHTTP.3.0',
				'Msxml2.XMLHTTP',
				'Microsoft.XMLHTTP');
			for (var i = 0; i < msxmlhttp.length; i++) {
				try {
					A = new ActiveXObject(msxmlhttp[i]);
				} catch (e) {
					A = null;
				}
			}
 			if(!A && typeof XMLHttpRequest != "undefined")
				A = new XMLHttpRequest();
			if (!A)
				alert("Could not create connection object.");
			return A;
}
		
function scriptVote(param){
  var http = initobject();
  param += '&rand=' + new Date().getTime();
  http.open('GET', url_site + '/xml_result.php?' + param, true);         
  http.send(null);
  http.onreadystatechange=function(){
  readVote(http)
  }
}

function script(param,option){
  var http = initobject();
  param += '&rand=' + new Date().getTime();
  //alert(param);
  http.open('GET', url_site + '/xml_result.php?' + param, true);         
  http.send(null);
  http.onreadystatechange=function(){
  read(http,option)
  } 
}

function scriptComment(param,option){
  var http = initobject();
  param += '&rand=' + new Date().getTime();
  //alert(param);
  http.open('GET', url_site + '/xml_result.php?' + param, true);         
  http.send(null);
  http.onreadystatechange=function(){
  displayComment(http,option)
  } 
}
function scriptTag(param,option){
  var http = initobject();
  param += '&rand=' + new Date().getTime();
  //alert(param);
  http.open('GET', url_site + '/xml_result.php?' + param, true);         
  http.send(null);
  http.onreadystatechange=function(){
  displayTag(http,option)
  } 
}
function display_comment_image_page(imagId, page)
{
	var http = initobject();
	param = 'pimgid=' + imagId + '&page=' + page + '&rand=' + new Date().getTime();
	//alert(param);
	http.open('GET', url_site + '/xml_result.php?' + param, true);         
	http.send(null);
	http.onreadystatechange=function(){
	displayComment(http,imagId)
	}	
}
function displayTag(http,imgId){
        
      if (http.readyState == 4) {
        if ((http.status==200)||(window.location.href.indexOf("http")==-1)){
	        var response = http.responseText;
		    var list = document.getElementById("taglist" + imgId);
			response = (response).replace(/^\s*|\s*$/g,'');
		    //alert(response);
			if (response == 1) {
				list.innerHTML = '<div>error when update database</div>';
			} else {
				//alert(response);
				list.innerHTML = response;
			}
		} else {alert('Error: '+ http.status);}
      }
}   
function displayComment(http,imgId){
        
      if (http.readyState == 4) {
        if ((http.status==200)||(window.location.href.indexOf("http")==-1)){
	        var response = http.responseText;
		    var list = document.getElementById("listcomment" + imgId);
			var paging = document.getElementById("listcommentpaging" + imgId);
		    response = (response).replace(/^\s*|\s*$/g,'');
		    if (response == 1) {
				list.innerHTML = '<div>error when display comment</div>';
				paging.innerHTML = '<div>error when display comment</div>';
			} else {
				//alert(response);
				var temp = new Array();
				temp = response.split('|');
				list.innerHTML = temp[0];
				paging.innerHTML = temp[1];
			}
		} else {alert('Error: '+ http.status);}
      }
}   
function rating(val,imageId)
{
	//alert('You rated it ' + val + ' star(s)! ' + imageId);
	var d = "vote=" + val + "&imageid=" + imageId;
	scriptVote(d);
	return false;
}
function readVote(http,input){
        
      if (http.readyState == 4) {
        if ((http.status==200)||(window.location.href.indexOf("http")==-1)){
	        var response = http.responseText;
		    response = (response).replace(/^\s*|\s*$/g,'');
		    if (response == 1) {
				alert('Error when update vote.Please try again!');
			}
			if (response == 2) {
				alert('Your action can not complete.Please login to vote! Thanks you!');
			}
		} else {alert('Error: '+ http.status);}
      }
}
function readComment(http,input){
        
      if (http.readyState == 4) {
        if ((http.status==200)||(window.location.href.indexOf("http")==-1)){
	        var response = http.responseText;
		    var b = document.getElementById("listcomment" + input);
		    response = (response).replace(/^\s*|\s*$/g,'');
		    if (response == 1) {
				b.innerHTML = '<div>error when add comment</div>';
			} else {
				//alert(response);
				b.innerHTML = response;
			}
		} else {alert('Error: '+ http.status);}
      }
}   
function read(http,input){
        
      if (http.readyState == 4) {
        if ((http.status==200)||(window.location.href.indexOf("http")==-1)){
          var response = http.responseText;
		  var a = document.getElementById("inputaddtitle" + input);
		  var b = document.getElementById("addtitlehere" + input);
		  response = (response).replace(/^\s*|\s*$/g,'');
		  a.style.display = 'none';
		  b.style.display = 'block';
			
		  	if (response == 1) {
			b.innerHTML = '<i>click to add title</i>';
			} else {
			b.innerHTML = '<b>' + response + '</b>';
			}
		} else {alert('Error: '+ http.status);}
      }
}
function CheckSpecialChar(str)
{
	//alert(str);
	var spChar = new Array();
	spChar[0] = "*";
	spChar[1] = "#";
	spChar[2] = "&";
	for(var i=0; i<spChar.length;i++)
	{
		if(str.indexOf(spChar[i]) >= 0)
		{
			return true;
		}
	}
	if((str == "") || (str == " "))
	{
		return true;
	}
	return false;
}
function save_title_image(input, response)
{

		var a = document.getElementById("inputaddtitle" + input);
		var b = document.getElementById("addtitlehere" + input);
		var c = document.getElementById("titleimage_input" + input).value;
		if(CheckSpecialChar(c))
		{
			alert("The title name can't have special character or empty!");
			return false;
		}
		a.style.display = 'none';
		b.style.display = 'block';
		b.innerHTML = '<i>Saving....</i>';
		var d = "nameimage=" + encodeURI(c) + "&imageid=" + input;
		script(d,input);
		return false;
	
}

function save_comment_image(input, response, member_id)
{
	var c = document.getElementById("commentimage_input" + input).value;
	if(CheckSpecialChar(c))
	{
		alert("The title name can't have special character or empty!");
		return false;
	}
	var d = "commentimage=" + encodeURI(c) + "&imageid=" + input + "&loginuser=" + member_id;
	scriptComment(d,input);
	return false;
	
}

function DeleteComment(commentId, imageId, member_id)
{
	var http = initobject();
	var param = 'rand=' + new Date().getTime() + '&delcommentid=' + commentId + '&imageid=' + imageId + '&loginuser=' + member_id;
	http.open('GET', url_site + '/xml_result.php?' + param, true);         
	http.send(null);
	http.onreadystatechange=function(){
		displayComment(http,imageId)
	} 
}

function save_tag_image(input, response)
{
	var c = document.getElementById("tag_input" + input).value;
	if(CheckSpecialChar(c))
	{
		alert("The title name can't have special character or empty!");
		return false;
	}
	var d = "tagimage=" + encodeURI(c) + "&imageid=" + input;
	scriptTag(d,input);
	document.getElementById("tag_input" + input).value = '';
	return false;
	
}

function DeleteTagImage(tagId, imageId)
{
	var http = initobject();
	param = 'rand=' + new Date().getTime() + '&deltagid=' + tagId + '&imageid=' + imageId;
	//alert(param);
	http.open('GET', url_site + '/xml_result.php?' + param, true);         
	http.send(null);
	http.onreadystatechange=function(){
		displayTag(http,imageId)
	} 
}

function addtitleimage(id) 
{
	var a = document.getElementById("inputaddtitle" + id);
	var b = document.getElementById("addtitlehere" + id);
	a.style.display = 'block';
	b.style.display = 'none';
}
function canceladdtitleimage(id) 
{
	var a = document.getElementById("inputaddtitle" + id);
	var b = document.getElementById("addtitlehere" + id);
	a.style.display = 'none';
	b.style.display = 'block';
}
function ShowCameraForm(show)
{
	var clickShow = document.getElementById("clickAddCameraProfile");
	var frm = document.getElementById("cameraProfileForm");
	if(show == 'block')
	{
		clickShow.style.display = 'none';
		frm.style.display = 'block';
	}
	else
	{
		clickShow.style.display = 'block';
		frm.style.display = 'none';
	}
}

function SaveCameraProfile()
{
	var profileName = document.getElementById("cameraProfileName");
	if(CheckSpecialChar(profileName.value))
	{
		alert("Can't input special character (&,*,#,...) or empty!");
		return false;
	}
	var d = "campro=" + encodeURI(profileName.value);
	scriptCamera(d);
	return false;
}
function scriptCamera(param){
  var http = initobject();
  param += '&rand=' + new Date().getTime();
  //alert(param);
  http.open('GET', url_site + '/xml_result.php?' + param, true);         
  http.send(null);
  http.onreadystatechange=function(){
  DisplayCameraProfile(http)
  } 
}
function DisplayCameraProfile(http){
        
      if (http.readyState == 4) {
        if ((http.status==200)||(window.location.href.indexOf("http")==-1)){
	        var response = http.responseText;
		    var list = document.getElementById("cameraProfile");
			response = (response).replace(/^\s*|\s*$/g,'');
		    if (response == 1) {
				alert('error when insert camera');
			} else {
				//alert(response);
				list.innerHTML = response;
			}
		} else {alert('Error: '+ http.status);}
      }
}

function VottingForImage(vote,imgId)
{
	var http = initobject();
	var param = 'votting=' + vote + '&voteforimg=' + imgId + '&rand=' + new Date().getTime();
	http.open('GET', url_site + '/xml_result.php?' + param, true);         
	http.send(null);
	http.onreadystatechange=function(){
		DisplayVotting(http,imgId)
	}
}
function DisplayVotting(http,imgId){
        
      if (http.readyState == 4) {
        if ((http.status==200)||(window.location.href.indexOf("http")==-1)){
	        var response = http.responseText;
		    var voteblock = document.getElementById('votting_for_' + imgId);
			response = (response).replace(/^\s*|\s*$/g,'');
		    if (response == 1) {
				alert('error when insert camera');
			} else {
				//alert(response);
				voteblock.innerHTML = response;
			}
		} else {alert('Error: '+ http.status);}
      }
}

