	// JavaScript Document containing all of the website's AJAX calls
	
	
	//Define the xmlhttp's based on the browser
	var xmlhttp=null;
	var xmlhttp2=null;
	var xmlhttp3=null;
	if (window.XMLHttpRequest){
		//If IE7, Mozilla, Safari, etc: Use native object
		xmlhttp = new XMLHttpRequest();
		xmlhttp2 = new XMLHttpRequest();
		xmlhttp3 = new XMLHttpRequest();
	} else {
		if (window.ActiveXObject){
			//If IE6 or IE5
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
			xmlhttp2 = new ActiveXObject("Microsoft.XMLHTTP");
			xmlhttp3 = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	function checkUsername(clicked) {
	
		if (clicked == true) {
		
			if (document.getElementById("username1").value == "") {
				document.getElementById("user1").className = "error";
			} else if (document.getElementById("username1").value.length < 4 || document.getElementById("username1").value.length > 12) {
				document.getElementById("user1").className = "error";
			} else {
	
				xmlhttp.open("GET","step1_check_user.php?user_name="+document.getElementById("username1").value+'&salt='+(Math.random()*100),true);
				xmlhttp.send(null);
				
				xmlhttp.onreadystatechange = function() {
					if (xmlhttp.readyState==4) {
						if (xmlhttp.responseText > 0) {
							document.getElementById("check_user_button").innerHTML = "<a href='javascript:checkUsername(true);' class='smallButton red'><img src='images/button_cap_red.gif' />Not Available</a>";
							document.getElementById("user1").className = "error";
							show_check_button = false;
						} else {
							document.getElementById("check_user_button").innerHTML = "<a href='javascript:checkUsername(true);' class='smallButton green'><img src='images/button_cap_green.gif' />Available</a>";
							document.getElementById("user1").className = "";
							document.getElementById("register_messages").innerHTML = "";
							show_check_button = false;
						}
					}
				}
			}
		
		} else {
			//alert("check user 2...");
			if (username_error == false) {
				xmlhttp.open("GET","step1_check_user.php?user_name="+document.getElementById("username1").value+'&salt='+(Math.random()*100),true);
				xmlhttp.send(null);
			
				xmlhttp.onreadystatechange = function() {
					//alert("checking username..."+ xmlhttp.readyState);
					if (xmlhttp.readyState==4) {
						if (xmlhttp.responseText > 0) {
							document.getElementById("check_user_button").innerHTML = "<a href='javascript:checkUsername(true);' class='smallButton red'><img src='images/button_cap_red.gif' />Not Available</a>";
							document.getElementById("user1").className = "error";
							username_error = "not_available";
							show_check_button = false;
						} else {
							document.getElementById("check_user_button").innerHTML = "<a href='javascript:checkUsername(true);' class='smallButton green'><img src='images/button_cap_green.gif' />Available</a>";
							document.getElementById("register_messages").innerHTML = "";
							document.getElementById("user1").className = "";
							show_check_button = false;
						}
						checkEmailStepOne();
					}
				}
			} else {
				checkEmailStepOne();
			}
		}
	}
	
	function checkEmailStepOne() {
	
		if (email_error == false) {
		
			xmlhttp2.open("GET","step1_check_email.php?email="+document.getElementById("email").value+'&salt='+(Math.random()*100),true);
			xmlhttp2.send(null);
				
			xmlhttp2.onreadystatechange = function() {
				//alert("checking email..."+xmlhttp2.readyState);
				if (xmlhttp2.readyState==4) {
					//alert(xmlhttp2.responseText);
					if (xmlhttp2.responseText > 0) {
						email_error = "not_available";
						submitStepOne();
					} else {
						document.getElementById("email1").className = "";
						document.getElementById("email2").className = "";
						email_error = false;
						if (cap_error == false && username_error == false && password_error == false && is_error == false ) {
							checkImageCode();
						} else {
							submitStepOne();
						}
					}
				}
			}
		} else {
			submitStepOne();
		}
	}
	
	function checkImageCode() {
		//alert("checking image code...");
		if (cap_error == false) {
		
			xmlhttp3.open("GET", 'includes/image_check.php?code=' + escape(document.getElementById("image_code").value)+'&salt='+(Math.random()*100), true);
			xmlhttp3.send(null);
				
			xmlhttp3.onreadystatechange = function() {
				if (xmlhttp3.readyState==4) {
					if(xmlhttp3.responseText != 1) {
						cap_error = "mismatch";				
					}
				submitStepOne();
				}
			}
		} else {
			submitStepOne();
		}
	}
	
	function sendMacEmail() {
		var email = document.getElementById("mac_address").value;	
		xmlhttp.open("GET","mac_email.php?email="+email,true);
		xmlhttp.send(null);
		//openMacEmail();
		alert("Thank you for your interest. We'll keep you posted!");
	}
	
	function sendConfirmEmail() {
		xmlhttp.open("GET","register_email.php?message="+Message,true);
		xmlhttp.send(null);
		alert("The SceneCaster team has been notified. Please be patient, your account should be activated soon!");
	}
	
	function sendEmail(to, from, subject, message) {
		var params = "to="+to+"&from="+from+"&subject="+subject+"&message="+message;
		
		xmlhttp.open("POST","includes/send_email.php",true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.send(params);
		
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState==4) {
				if(xmlhttp.responseText == "1"){
					//alert("Email has been sent.");
					document.getElementById('container_content').innerHTML="<p><strong>Email has been sent</strong></p>";
				} else {
					//alert("Email failed to send.");
					document.getElementById('container_content').innerHTML="<p><strong>Email failed to send</strong></p>";
				}
			}
		}
	}
	
	function saveRating(value) {
 
		var params = "project_id="+project_id+"&user_id="+user_id+"&rating="+(value+1)/2.0;
		xmlhttp.open("POST","scenecasts_rate.php",true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.send(params);
		
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState==4) {
				if(xmlhttp.responseText == "1"){
					freezeRating();
				} else {
					alertObject({'yes':["killAlert()","Ok"], 'no':[null,null], 'cancel':[null,null]},'Error','Rating could not be saved');
				}
			}
		}	
	}
	
	function saveComment() {
 		
		var comment = document.getElementById("comments").value;
		if(comment == "") {
			alertObject({'yes':["killAlert()","Ok"], 'no':[null,null], 'cancel':[null,null]},'Error','You must type something to save a comment!');
		} else {
			var params = "project_id="+project_id+"&user_id="+user_id+"&comment="+comment+"&salt="+(Math.random()*100);
			xmlhttp.open("POST","scenecasts_comment.php",true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.send(params);
			
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState==4) {
					if(xmlhttp.responseText == "1"){
						getReviews(project_id);
						document.getElementById("comments").value = "";
					} else {
						alertObject({'yes':["killAlert()","Ok"], 'no':[null,null], 'cancel':[null,null]},'Error','Comment could not be saved');
					}
				}
			}
		}
	}
	
	function saveChange(data, field) {
		var params = "data="+data+"&field="+field+"&project_id="+project_id;

		xmlhttp.open("POST","scenecasts_edit_details.php",true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.send(params);
		
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState==4) {
				if(xmlhttp.responseText == "1"){
					alertObject({"yes":["killAlert(); eip_fieldFX()","Ok"], "no":[null,null], "cancel":[null,null]},'Saved','Your changes have been saved.');
				} else {
					alertObject({"yes":["killAlert(); eip_fieldFX()","Ok"], "no":[null,null], "cancel":[null,null]},'Oops','Update failed.');
				}
			}
		}
	}
	
	function favoriteScene(project, action) {

		xmlhttp.open("GET","favorite.php?project_id="+project+"&action="+action,true);
		xmlhttp.send(null);
		
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState==4) {
				if(xmlhttp.responseText == "1") {
					if(action=="delete") {
						alertObject({"yes":["killAlert(); eip_fieldFX()","Ok"], "no":[null,null], "cancel":[null,null]},'Removed','Project has been removed from your favorites.');
						if(document.getElementById("fav_button")){
							$("fav_button").innerHTML = "<a href='javascript:favoriteScene("+project+",\"add\")' title='Add to Favorites'><img src='images/scene_fav.gif' alt='' />Add Fave</a>";
						}
						if(document.getElementById(project)){
							var thumbnail = document.getElementById(project);
							thumbnail.parentNode.removeChild(thumbnail);
							
							var favorites = getElementsByClass("thumbnail_listing", $("fav_list"));
							if(favorites.length == 0) {
								$("fav_list").innerHTML = "<p>You have no favorite scenes yet.</p><p>Why not <a href='explorer.php'>choose some now?</a></p>";
							}
						}
						
					} else {
						alertObject({"yes":["killAlert(); eip_fieldFX()","Ok"], "no":[null,null], "cancel":[null,null]},'Added','Project has been added to your favorites.');
						if($("fav_button")){
							$("fav_button").innerHTML = "<a href='javascript:favoriteScene("+project+",\"delete\")' title='Remove from Favorites'><img src='images/scene_removefav.gif' alt='' />Remove Fave</a>";
						}
					}
				} else {
					alertObject({"yes":["killAlert(); eip_fieldFX()","Ok"], "no":[null,null], "cancel":[null,null]},'Oops','Request failed.');
				}
			}
		}
	
	}
	
	
	function get_scenes(rowstart, filter, time) {
		switch(true) {
		case (filter == "toprated"):
			title = "Top Rated Scenes";
			break;
		case (filter == "mostviewed"):
			title = "Most Viewed Scenes";
			break;
		default:
			title = "Most Recent Scenes";
			break;
		}
		
		xmlhttp.open("GET","explorer_getscenes.php?rowstart="+rowstart+"&filter="+filter+"&timescope="+time+"&salt="+(Math.random()*100), true);
		xmlhttp.send(null);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState==4) {
				//document.getElementById("filter_title").innerHTML = title;
				document.getElementById("explorer_content").innerHTML = xmlhttp.responseText;
			}
		}
	}
	
	
	function search_scenes(rowstart) {
		search_phrase = document.getElementById("search").value;
		xmlhttp.open("GET","explorer_search_scenes.php?rowstart="+rowstart+"&search_phrase="+search_phrase+"&salt="+(Math.random()*100), true);
		xmlhttp.send(null);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState==4) {
				//document.getElementById("filter_title").innerHTML = "Search Results";
				document.getElementById("explorer_content").innerHTML = xmlhttp.responseText;
			}
		}
	}
	
	function submitProfileInfo() {

		var email       = document.getElementById("email_field").value;
		var f_name      = document.getElementById("first_name_field").value;
		var l_name      = document.getElementById("last_name_field").value;
		var month       = document.getElementById("birth_month_field").value;
		var day         = document.getElementById("birth_day_field").value;
		var year        = document.getElementById("birth_year_field").value;
		var male        = document.getElementById("male_field").checked;
		var female      = document.getElementById("female_field").checked;
		var street      = document.getElementById("street_field").value;
		var city        = document.getElementById("city_field").value;
		var province    = document.getElementById("state_field").value;
		var postal      = document.getElementById("postal_zip_field").value;
		var country     = document.getElementById("country_field").value;
		var phone       = document.getElementById("phone_field").value;
		var updates     = document.getElementById("updates_field").checked;
		var news		= document.getElementById("news_field").checked;
		
		var params = "email="+email+"&fname="+f_name+"&lname="+l_name+"&month="+month+"&day="+day+"&year="+year+"&male="+male+"&female="+female+"&street="+street+"&city="+city+"&province="+province+"&postal="+postal+"&country="+country+"&phone="+phone+"&updates="+updates+"&news="+news;
		
		xmlhttp.open("POST","myprofile_update.php",true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.send(params);
		
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState==4) {
				if(xmlhttp.responseText == "1"){
					alertObject({"yes":["killAlert(); eip_fieldFX()","Ok"], "no":[null,null], "cancel":[null,null]},'Saved','Your changes have been saved.');
				} else {
					alertObject({"yes":["killAlert(); eip_fieldFX()","Ok"], "no":[null,null], "cancel":[null,null]},'Oops','Update failed.');
				}
			}
		}
	}
	
	function submitNewPassword() {
		
		var password = document.getElementById("new_password").value;
		var params = "new_password="+password;
		xmlhttp.open("POST","myprofile_password.php",true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.send(params);
		
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState==4) {
				if(xmlhttp.responseText == "1"){
					alertObject({"yes":["killAlert(); eip_fieldFX()","Ok"], "no":[null,null], "cancel":[null,null]},'Saved','Your password has been changed.');
				} else {
					alertObject({"yes":["killAlert(); eip_fieldFX()","Ok"], "no":[null,null], "cancel":[null,null]},'Oops','Update failed.');
				}
			}
		}
	}
	
	function submitBio() {
		
		var bio = document.getElementById("bio").value;
		var params = "bio="+bio;
		xmlhttp.open("POST","myprofile_bio.php",true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.send(params);
		
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState==4) {
				if(xmlhttp.responseText == "1"){
					alertObject({"yes":["killAlert(); eip_fieldFX()","Ok"], "no":[null,null], "cancel":[null,null]},'Saved','Your bio has been updated.');
				} else {
					alertObject({"yes":["killAlert(); eip_fieldFX()","Ok"], "no":[null,null], "cancel":[null,null]},'Oops','Update failed.');
				}
			}
		}
	}
	
	function getReviews(pid) {
		var params = "project_id="+pid+"&salt="+(Math.random()*100);
		xmlhttp3.open("POST","getReviews.php",true);
		xmlhttp3.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp3.send(params);
		xmlhttp3.onreadystatechange = function() {
			if (xmlhttp3.readyState==4) {
				document.getElementById("scene_comments").innerHTML = xmlhttp3.responseText;
				//showRating("rateThis");
			}
		}
	}
	
	function getRating(pid, ratable) {
		var params = "project_id="+pid;
		xmlhttp2.open("POST","getRating.php",true);
		xmlhttp2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp2.send(params);
		xmlhttp2.onreadystatechange = function() {
			if (xmlhttp2.readyState==4) {
				document.getElementById("scene_data").innerHTML = xmlhttp2.responseText;
				if(ratable == "yes") {
					showRating("rateScene");
				}
			}
		}
	}
	
	function deleteComment(pid, review) {
		var params = "project_id="+pid+"&text="+review+"&salt="+(Math.random()*100);
		xmlhttp2.open("POST","delete_comment.php",true);
		xmlhttp2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp2.send(params);
		xmlhttp2.onreadystatechange = function() {
			if (xmlhttp2.readyState==4) {
				if(xmlhttp2.responseText == "1") {
					getReviews(pid);
				} else {
					alertObject({"yes":["killAlert(); eip_fieldFX()","Ok"], "no":[null,null], "cancel":[null,null]},'Oops','Delete failed.');
				}
			}
		}
	}
	
	function html_entity_decode( string ) {
	    // http://kevin.vanzonneveld.net
	    // +   original by: john (http://www.jd-tech.net)
	    // +      input by: ger
	    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	    // *     example 1: html_entity_decode('Kevin &amp; van Zonneveld');
	    // *     returns 1: 'Kevin & van Zonneveld'
	 	
	    var ret, tarea = document.createElement('textarea');
	    tarea.innerHTML = string;
	    ret = tarea.value;
	    return ret;
	}
	
	function getGoogleMetaData(instanceId, mid) {
		var params = "mid=" + mid + "&salt=" + (Math.random()*100);
		xmlhttp2.open("GET","../screen_scrape.php?" + params,true);
		xmlhttp2.send(null);
		xmlhttp2.onreadystatechange = function() {
			if (xmlhttp2.readyState==4) {

			    var metaData = xmlhttp2.responseText;		    
			    
			    metaData = metaData.split("~;~");
			    
			    var name 		= metaData[0];
			    var description = metaData[1];
			    var authorName 	= metaData[2];
			    var authorID 	= metaData[3];
			    
			    var tags = "";
        
                for(var i = 4; i < metaData.length; i++)
                {
                    tags += unescape(metaData[i]);
                    if(i != metaData.length - 1)
                        tags += ",";
                }
			    
			    document.view22RTE.SetMetadata(instanceId, "type",			"google");
                document.view22RTE.SetMetadata(instanceId, "id",			mid);
                document.view22RTE.SetMetadata(instanceId, "name",			html_entity_decode(name));
                document.view22RTE.SetMetadata(instanceId, "tags",			html_entity_decode(tags));
                document.view22RTE.SetMetadata(instanceId, "authorName",	authorName);
                document.view22RTE.SetMetadata(instanceId, "authorID",		authorID);
                
                if(description != ""){
                	document.view22RTE.SetMetadata(instanceId, "description", html_entity_decode(description));
                }
			}
		}
	}
	
	function getInternalMetaData(instanceId, objectId)
	{
	    var params = "objectId=" + objectId + "&salt=" + (Math.random()*100);
	    xmlhttp2.open("GET","../internal_metadata.php?" + params,true);
		xmlhttp2.send(null);
		xmlhttp2.onreadystatechange = function() 
		{
		    if (xmlhttp2.readyState==4) 
		    {
			    var metaData = xmlhttp2.responseText;		    
			    
			    metaData = metaData.split(",");
			    
			    var name        = metaData[0];
			    var tags        = metaData[1];
                var description = metaData[2];
                var url         = metaData[3];
			    
			    document.view22RTE.SetMetadata(instanceId, "type",  "internal");
                document.view22RTE.SetMetadata(instanceId, "id",    objectId);
                document.view22RTE.SetMetadata(instanceId, "name",  name);
                document.view22RTE.SetMetadata(instanceId, "tags",  tags);
                
                if(description != "")
                    document.view22RTE.SetMetadata(instanceId, "description", description);
                    
                if(url != "")
                    document.view22RTE.SetMetadata(instanceId, "url", url);
		    }
		}
	}
	
	function getSceneName(projectId)
	{
	    var params = "projectId=" + projectId + "&salt=" + (Math.random()*100);
	    xmlhttp2.open("GET","../getProjectNameById.php?" + params,true);
		xmlhttp2.send(null);
		xmlhttp2.onreadystatechange = function() 
		{
		    if (xmlhttp2.readyState==4) 
		    {
			    document.getElementById('form_scene').search.value = xmlhttp2.responseText;
		    }
		}
	}
	
	function deleteCheck(pid, uid) {
		alertObject({"yes":["deleteScene("+pid+", "+uid+"); killAlert();","Yes"], "no":[null,null], "cancel":["killAlert();","No"]},'Delete','Are you sure you want to delete this project?');
	}
	
	function deleteScene(pid, uid) {
		
		var params = "project_id="+pid+"&user_id="+uid+"&salt="+(Math.random()*100);
		xmlhttp.open("POST","myscenes_delete.php",true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.send(params);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState==4) {
				alertObject({"yes":["killAlert(); document.location='myscenes.php';","Ok"], "no":[null,null], "cancel":[null,null]},'Scene Deleted','Scene Deleted');
			}
		}
	}
	
	function sendReport(pid) {
		
		var Message = document.getElementById("report").value;
		var params = "project_id="+pid+"&message="+Message+"&salt="+(Math.random()*100);
		
		xmlhttp.open("POST","scenecasts_send_report.php",true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.send(params);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState==4) {
				if(xmlhttp.responseText == "1") {
					alertObject({"yes":["killAlert();","Ok"], "no":[null,null], "cancel":[null,null]},'Report Sent','Your report has been sent.');
				} else {
					alertObject({"yes":["killAlert();","Ok"], "no":[null,null], "cancel":[null,null]},'Report Failed','Sorry we couldn\'t send your report.');
				}
			}
		}
	}
	