// AJAX Handler

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();
	 } catch (e) {
	  xmlhttp=false
	 }
	}
	function myXMLHttpRequest() {
	  var xmlhttplocal;
	  try {
	    xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
	 } catch (e) {
	  try {
	    xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
	  } catch (E) {
	    xmlhttplocal=false;
	  }
	 }

	if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
	 try {
	  var xmlhttplocal = new XMLHttpRequest();
	 } catch (e) {
	  var xmlhttplocal=false;
	  alert('couldn\'t create xmlhttp object');
	 }
	}
	return(xmlhttplocal);
}

function photos_show(id) {
	new Effect.toggle('res-photos','blind', {duration:0.5});
	if(!$('res-photos').visible()) {
		photos_load(id);
	}
}

function photos_load(id) {
	var theDIV = document.getElementById('res-photos');
	theDIV.innerHTML = '<div class="loading"></div>';
	
    xmlhttp.open('get', 'includes/functions/_photosload.php?id='+id);
    xmlhttp.onreadystatechange = photos_handleLoad;
    xmlhttp.send(null);	
}
function photos_handleLoad() {
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			response = xmlhttp.responseText;
			
			var resultDiv = document.getElementById('res-photos');
			resultDiv.innerHTML = response;
		} else {
			alert("Error reading server response.");
		}
	}
}

function photos_loadparent(id) {
	var theDIV = window.parent.document.getElementById('res-photos');
	theDIV.innerHTML = '<div class="loading"></div>';
	
    xmlhttp.open('get', 'includes/functions/_photosload.php?id='+id);
    xmlhttp.onreadystatechange = photos_handleLoadparent;
    xmlhttp.send(null);	
}
function photos_handleLoadparent() {
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			response = xmlhttp.responseText;
			
			var resultDiv = window.parent.document.getElementById('res-photos');
			resultDiv.innerHTML = response;
		} else {
			alert("Error reading server response.");
		}
	}
}

function photos_setDefault(id, photo) {
	var theDIV = document.getElementById('res-photos');
	theDIV.innerHTML = '<div class="loading"></div>';
	
	nocache = Math.random();
	xmlhttp.open('post', 'includes/functions/_photosset.php', true);
	xmlhttp.onreadystatechange = photos_handleSet;
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('id='+id+'&photo='+photo+'&nocache = '+nocache);
}
function photos_handleSet() {
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			response = xmlhttp.responseText;
			if (response.indexOf("ERRNO") >= 0
								 || response.indexOf("error") >= 0
								 || response.length == 0) {
				alert(response.length == 0 ? "Server error." : response);
				return;
			}
			
			if(response == 0) {
				var resultDiv = document.getElementById('res-photos');
				resultDiv.innerHTML = response;
			} else {
				photos_load(escape(response).replace(/%20/g,''));
			}

		} else {
			alert("Error reading server response.");
		}
	}
}

function photos_confirmDelete(id, photo) {
	if(!$('res-photos-delete').visible()) {
		new Effect.toggle('res-photos-delete','blind', {duration:0.5});
	}
	var theDIV = document.getElementById('res-photos-delete');
	theDIV.innerHTML = '<div class="loading"></div>';
	
    xmlhttp.open('get', 'includes/functions/_photosconfirm.php?id='+id+'&photo='+photo);
    xmlhttp.onreadystatechange = photos_handleConfirm;
    xmlhttp.send(null);	
}
function photos_handleConfirm() {
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			response = xmlhttp.responseText;
			if (response.indexOf("ERRNO") >= 0
								 || response.indexOf("error") >= 0
								 || response.length == 0) {
				alert(response.length == 0 ? "Server error." : response);
				return;
			}
			
			var resultDiv = document.getElementById('res-photos-delete');
			resultDiv.innerHTML = response;

		} else {
			alert("Error reading server response.");
		}
	}
}

function photos_Delete(id, photo) {
	var theDIV = document.getElementById('res-photos-delete');
	theDIV.innerHTML = '<div class="loading"></div>';
	
    xmlhttp.open('get', 'includes/functions/_photosconfirm.php?id='+id+'&photo='+photo+'&action=delete');
    xmlhttp.onreadystatechange = photos_handleDelete;
    xmlhttp.send(null);	
}
function photos_handleDelete() {
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			response = xmlhttp.responseText;
			if (response.indexOf("ERRNO") >= 0
								 || response.indexOf("error") >= 0
								 || response.length == 0) {
				alert(response.length == 0 ? "Server error." : response);
				return;
			}
			
			response_array = response.split('|');
			var resultDiv = document.getElementById('res-photos-delete');
			resultDiv.innerHTML = response_array[2];
			if(response_array[1] > 0) {
				photos_load(response_array[1]);
			}

		} else {
			alert("Error reading server response.");
		}
	}
}
function photos_Upload(upload_field) {
	// this is just an example of checking file extensions
    // if you do not need extension checking, remove 
    // everything down to line
    // upload_field.form.submit();

    var re_text = /\.jpg|\.jpeg|\.png|\.gif|\.bmp/i;
    var filename = upload_field.value;

    /* Checking file type */
    if (filename.search(re_text) == -1)
    {
        alert("File does not have an image (jpg, jpeg, png, gif, bmp) extension");
        upload_field.form.reset();
        return false;
    }

    upload_field.form.submit();
	var theDIV = document.getElementById('upload_status');
	theDIV.innerHTML = '<div class="loading"></div>';
    return true;
}

function setfieldValue(field, value) {
	var theField = document.getElementById(field);	
	theField.value = value;

}

function updateRating(id, value) {
	var ele = document.getElementById(id);
	ele.style.width = value+'px';
	
}

function emailfriend_send(id) {
	var theDIV = document.getElementById('email-status');
	theDIV.innerHTML = '<div class="loading"></div>';
	
	var email = encodeURI(document.getElementById('emailaddress').value);
	nocache = Math.random();
	
	xmlhttp.open('post', 'includes/functions/_emailfriend.php', true);
	xmlhttp.onreadystatechange = email_handle;
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('id='+id+'&email='+email+'&nocache = '+nocache);
}
function email_handle() {
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			response = xmlhttp.responseText;
			
			var resultDiv = document.getElementById('email-status');
			resultDiv.innerHTML = response;
		} else {
			alert("Error reading server response.");
		}
	}
}