//Christopher C Reding
//01/22/10
//ccreding@gmail.com


var text = "";
//add domain to list
function addDomain(){
		var domain = $("#domain").val();
		
		//set base value of domain
		mydomain = domain;
		
		//clear the domain input
		$("#domain").val("");
		
		//Here we add the domain through an append giving it a class associated to the domain
		if(domain != ''){
		$("#capture").append("<span>" + mydomain + " <button id=\"remove\" class=\"right-bttn\" title=\"delete\" onclick=\"deleteDomain('" + domain + "'); $(this).parent().remove(); downloadDoc(); return false;\" >Remove</button></span>").hide().fadeIn("slow");

		 $('#view').appendDom([{
     	 tagName : 'allow-access-from',
     	 domain : domain
    	}]); 
		
		//set the text variable to the crossdomain file
	 	 text = "<cross-domain-policy>" + $("#view").html()  + "</cross-domain-policy>";
		}
		downloadDoc();
		return false
	}
//End add function

	

//This function removes a domain from the list using the generated class from the prior domain
function deleteDomain(domain){
	//remove the domain by loping through the attributes
	 $("#view").find("allow-access-from").each(function(){
			 if($(this).attr("domain") === domain){	
			 	var removeD = $(this).attr("domain");
				$(this).remove();
			 }
   });
	
}
	 
//Here we use an ajax request to a php page to process the xmldoc
function downloadDoc(){
	if($("#view").html() === ""){
		$("#result").val("");
		$("#download").hide();
		}else{
		text = "<cross-domain-policy>" + $("#view").html()  + "</cross-domain-policy>";
		$("#download").show();
		jQuery.ajax({
		url: "xmlgen.php",
		type: "POST",
		contentType: "text/xml",
		processData: false,
		data: text,
		success: function( data ) {
			//alert(data)
			//SET DOWNLOAD
			//populate the textarea
			$("#result").val(data);
			
		}

  		 });
	}
}

function copyXML(){
		$.copy($("#result").val());
		 alert("Your crossdomain.xml has been copied to your clipboard<br />" + $("#result").val());
			 }
		

	

<!---END SCRIPTS--->