var url = "ajax.php?";
var isWorking = false;

/* Code to rate members */
function rateMember(user_id, rating, field, image_id, redirect, rating_type) {
	  if (!isWorking && http) {
		http.open("GET", url + "task=rateMember&user_id=" + escape(user_id) + "&image_id=" + escape(image_id) + "&rating=" + rating, true);
		http.onreadystatechange = function() {handleRateResponse(field, redirect, rating_type)};
		isWorking = false; // if set to true, updating stops after it finds the right city/state
		http.send(null);   // leaving it false allows for multiple updates
	  }
}

function handleRateResponse(field, redirect, rating_type) {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			// Use the XML DOM to get whether or not the value has been set
			var xmlDocument = http.responseXML; 
			var response = xmlDocument.getElementsByTagName('response').item(0).firstChild.data;
			var rating = xmlDocument.getElementsByTagName('rating').item(0).firstChild.data;
			var average = xmlDocument.getElementsByTagName('average').item(0).firstChild.data;
			var user_id = xmlDocument.getElementsByTagName('user_id').item(0).firstChild.data;
			var image_id = xmlDocument.getElementsByTagName('image_id').item(0).firstChild.data;

			if (!redirect) {
				document.getElementById(field+"L").innerHTML = rating;
				document.getElementById(field+"Avg").innerHTML = average;				
				document.getElementById(field).style.display = 'none';
				document.getElementById(field+"L").style.display = 'inline';
			}

			isWorking = false;
			if (redirect)
				window.location.href='index.php?option=profile&task=rated&rating='+rating+'&user_id='+user_id+'&image_id='+image_id+"&rating_type="+rating_type;
		}
	}
}

/***************************************************************************************************************/
/***************************************************************************************************************/

function changeGender(user_id, gender) {
	  if (!isWorking && http) {
		http.open("GET", url + "task=changeGender&user_id=" + escape(user_id) + "&gender=" + escape(gender), true);
//		http.onreadystatechange = function() {handleGenderResponse(field, redirect)};
		isWorking = false; // if set to true, updating stops after it finds the right city/state
		http.send(null);   // leaving it false allows for multiple updates
	  }
}

/***************************************************************************************************************/
/***************************************************************************************************************/

function changeSite(image_id, site_id) {
	  if (!isWorking && http) {
		http.open("GET", url + "task=changeSite&image_id=" + escape(image_id) + "&site_id=" + escape(site_id), true);
//		http.onreadystatechange = function() {handleGenderResponse(field, redirect)};
		isWorking = false; // if set to true, updating stops after it finds the right city/state
		http.send(null);   // leaving it false allows for multiple updates
	  }
}

/***************************************************************************************************************/
/***************************************************************************************************************/

function changeImageType(image_id, imageType) {
	  if (!isWorking && http) {
		http.open("GET", url + "task=changeImageType&image_id=" + escape(image_id) + "&imageType=" + escape(imageType), true);
//		http.onreadystatechange = function() {handleGenderResponse(field, redirect)};
		isWorking = false; // if set to true, updating stops after it finds the right city/state
		http.send(null);   // leaving it false allows for multiple updates
	  }
}

/***************************************************************************************************************/
/***************************************************************************************************************/

/* Code to check if username already exists */
function checkUsername(username) {
	document.getElementById('email_taken').style.display = 'none';				
	if (!isWorking && http) {
		http.open("GET", url + "task=checkUsername&username=" + escape(username), true);
		http.onreadystatechange = function() {handleUsernameResponse()};
		isWorking = false; // if set to true, updating stops after it finds the right city/state
		http.send(null);   // leaving it false allows for multiple updates
	  }
}

/* Code to check if username already exists */
function checkEmailAvail(email) {
	document.getElementById('email_taken').style.display = 'none';				
	if (!isWorking && http) {
		http.open("GET", url + "task=checkUsername&email=" + escape(email), true);
		http.onreadystatechange = function() {handleUsernameResponse()};
		isWorking = false; // if set to true, updating stops after it finds the right city/state
		http.send(null);   // leaving it false allows for multiple updates
	  }
}

function handleUsernameResponse() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			// Use the XML DOM to get whether or not the value has been set
			var xmlDocument = http.responseXML; 
			var user_id = xmlDocument.getElementsByTagName('user_id').item(0).firstChild.data;
			var username = xmlDocument.getElementsByTagName('username').item(0).firstChild.data;
			var email = xmlDocument.getElementsByTagName('email').item(0).firstChild.data;
		  
			if (user_id > 0) {
				if (username != '')
					document.getElementById('username_warn').style.display = 'block';
				else
					document.getElementById('email_taken').style.display = 'block';					
			} else {
				document.getElementById('username_warn').style.display = 'none';				
				document.getElementById('email_taken').style.display = 'none';				
				// checkUsernameSyntax();
				// checkEmail();
			}
			isWorking = false;
		}
	}
}


/***************************************************************************************************************/
/***************************************************************************************************************/

/* Code to un/publish images */
function publishImage(image_id) {
	  if (!isWorking && http) {
		http.open("GET", url + "task=publishImage&image_id=" + escape(image_id), true);
		http.onreadystatechange = function() {handlePublishResponse()};
		isWorking = false; // if set to true, updating stops after it finds the right city/state
		http.send(null);   // leaving it false allows for multiple updates
	  }
}

function handlePublishResponse() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			// Use the XML DOM to get whether or not the value has been set
			var xmlDocument = http.responseXML; 
			var image_id = xmlDocument.getElementsByTagName('image_id').item(0).firstChild.data;
			var published = xmlDocument.getElementsByTagName('published').item(0).firstChild.data;

			document.getElementById("published_"+image_id).src = "images/publish_"+published+".png";

			isWorking = false;
		}
	}
}

/***************************************************************************************************************/
/***************************************************************************************************************/

/* Code to set images as public/private */
function publicImage(image_id) {
	  if (!isWorking && http) {
		http.open("GET", url + "task=publicImage&image_id=" + escape(image_id), true);
		http.onreadystatechange = function() {handlePublicResponse()};
		isWorking = false; // if set to true, updating stops after it finds the right city/state
		http.send(null);   // leaving it false allows for multiple updates
	  }
}

function handlePublicResponse() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			// Use the XML DOM to get whether or not the value has been set
			var xmlDocument = http.responseXML; 
			var image_id = xmlDocument.getElementsByTagName('image_id').item(0).firstChild.data;
			var isPublic = xmlDocument.getElementsByTagName('public').item(0).firstChild.data;

			document.getElementById("public_"+image_id).src = "images/publish_"+isPublic+".png";

			isWorking = false;
		}
	}
}

/***************************************************************************************************************/
/***************************************************************************************************************/

/* Code to set images as allowRate/private */
function allowRate(image_id) {
	  if (!isWorking && http) {
		http.open("GET", url + "task=allowRate&image_id=" + escape(image_id), true);
		http.onreadystatechange = function() {handleallowRateResponse()};
		isWorking = false; // if set to true, updating stops after it finds the right city/state
		http.send(null);   // leaving it false allows for multiple updates
	  }
}

function handleallowRateResponse() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			// Use the XML DOM to get whether or not the value has been set
			var xmlDocument = http.responseXML; 
			var image_id = xmlDocument.getElementsByTagName('image_id').item(0).firstChild.data;
			var isallowRate = xmlDocument.getElementsByTagName('allowRate').item(0).firstChild.data;

			document.getElementById("allowRate_"+image_id).src = "images/publish_"+isallowRate+".png";

			isWorking = false;
		}
	}
}

/***************************************************************************************************************/
/***************************************************************************************************************/

/* Code to set images as public/private */
function publishComment(comment_id) {
	  if (!isWorking && http) {
		http.open("GET", url + "task=publishComment&comment_id=" + escape(comment_id), true);
		http.onreadystatechange = function() {handleCommentResponse()};
		isWorking = false; // if set to true, updating stops after it finds the right city/state
		http.send(null);   // leaving it false allows for multiple updates
	  }
}

function handleCommentResponse() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			// Use the XML DOM to get whether or not the value has been set
			var xmlDocument = http.responseXML; 
			var comment_id = xmlDocument.getElementsByTagName('comment_id').item(0).firstChild.data;
			var isApproved = xmlDocument.getElementsByTagName('isApproved').item(0).firstChild.data;

			document.getElementById("comment_"+comment_id).src = "images/publish_"+isApproved+".png";

			isWorking = false;
		}
	}
}

/***************************************************************************************************************/
/***************************************************************************************************************/

/* Code to set images as public/private */
function publishBanner(banner_ad_id) {
	  if (!isWorking && http) {
		http.open("GET", url + "task=publishBanner&banner_ad_id=" + escape(banner_ad_id), true);
		http.onreadystatechange = function() {handleBannerResponse()};
		isWorking = false; // if set to true, updating stops after it finds the right city/state
		http.send(null);   // leaving it false allows for multiple updates
	  }
}

function handleBannerResponse() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			// Use the XML DOM to get whether or not the value has been set
			var xmlDocument = http.responseXML; 
			var banner_ad_id = xmlDocument.getElementsByTagName('banner_ad_id').item(0).firstChild.data;
			var published = xmlDocument.getElementsByTagName('published').item(0).firstChild.data;

			document.getElementById("banner_ad_"+banner_ad_id).src = "images/publish_"+published+".png";

			isWorking = false;
		}
	}
}

/***************************************************************************************************************/
/***************************************************************************************************************/

function searchUsername(username) {
	/*	
	if (!isWorking && http && username.length >= 3) {
		http.open("GET", url + "task=searchUsername&username=" + escape(username), true);
		http.onreadystatechange = function() {handleSearchResponse()};
		isWorking = false; // if set to true, updating stops after it finds the right city/state
		http.send(null);   // leaving it false allows for multiple updates
	  }	
	 */
}

function handleSearchResponse() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			// Use the XML DOM to get whether or not the value has been set
			var xmlDocument = http.responseXML; 		  
			var matchList = document.getElementById("matches");
			matchList.style.visibility = "visible";
			matchList.options.length = 0; //reset the states dropdown
			matchList.size = xmlDocument.getElementsByTagName('username').length;

			for (var i = 0; i < matchList.size; i++)
				matchList.options[matchList.options.length] = new Option(xmlDocument.getElementsByTagName('username').item(i).firstChild.data);
			isWorking = false;
		}
	}
}



/***************************************************************************************************************/
/* DO NOT  EDIT BELOW */
/***************************************************************************************************************/

/* HTTP OBJECT USED FOR ALL AJAX CODE */
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
	  xmlhttp.overrideMimeType("text/xml"); 
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}
var http = getHTTPObject(); // HTTP Object for Update