<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<?php 
	$project = $this->data['project'];
	$tracker = $this->data['tracker'];

	/* Instanciate group and ArtifatcTypeHTML objects */

	$group = group_get_object($project);
	$ath  = new ArtifactTypeHtml($group, $tracker);
	
	if(isset($this->oauth_auth_header)) {
		$oauth_auth_header = $this->oauth_auth_header;
	}
	if(isset($this->build_url)) {
		$build_url = $this->build_url;
	}
	if (isset($this->build_number)) {
		$build_number = $this->build_number;
	}
	if (isset($this->auth_timestamp)) {
		$auth_timestamp = $this->auth_timestamp;
	}
	
?>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>OSLC Creation UI</title>

		<script type="text/javascript" src="/scripts/jquery/jquery.js"></script>
		<script type="text/javascript" src="/plugins/oslc/scripts/json.js"></script>
		<script language="JavaScript" type="text/javascript">/* <![CDATA[ */

jQuery(function(){
	//remove warnings about authentication if any
	jQuery(".login_warning_msg").remove();
	jQuery(".file_attachments").remove();
	jQuery('input:hidden').remove();

	
	var project = "<?php echo $project;?>";
	var tracker = "<?php echo $tracker;?>";
	var URL = '<?php echo $this->baseUrl(); ?>' + '/cm/project/' + project + '/tracker/' + tracker;
	var oauth_auth_header = '<?php if (isset($oauth_auth_header)) echo $oauth_auth_header;?>';
	var build_url = '<?php if (isset($build_url)) echo $build_url;?>';
	var build_number = '<?php if(isset($build_number)) echo $build_number;?>';
	var postdata;
	var auth_timestamp =  '<?php if (isset($auth_timestamp)) echo $auth_timestamp; ?>';
	var error = false;

	if(build_number) {
		jQuery("#tracker-summary").val('Hudson Build #' + build_number + ' Failure');
		jQuery("#tracker-description").val('Follow the build url for more details about the failure: ' + build_url);
	}

	jQuery('input[type="submit"]').remove();
	jQuery(document).ready(function() {
		var input = document.createElement("input");
		input.type = "submit";
		input.name = "submit";
		jQuery("body").append(input);
	});

	jQuery('input[type="submit"]').click(function(){
		createBugData();
		if (checkTimeInterval(auth_timestamp)) {
			jQuery.ajax({
				type: "POST",
				url: URL,
				data: jQuery.toJSON(postdata),
				contentType:"application/x-oslc-cm-change-request+json",
				beforeSend: function(xhr) {
					if (oauth_auth_header != '') {
						xhr.setRequestHeader("Authorization", oauth_auth_header);
					}
				},
				success: function(data) {
					alert("The url of the bug just created is: " + data);
				},
				error: function(jqXHR, textStatus, errorThrown) {
    	           	var response = jQuery.parseJSON(errorThrown);
    	           	var errmsg, errcode;
     	          	for (var key in response) {
						var obj = response[key];						   
						if(key=="oslc_cm:Error") {
							for (var i in obj) {
								if(i=="oslc_cm:statusCode")	{
									errcode = obj[i];
								}else if(i=="oslc_cm:message") {
									errmsg = obj[i];
								}
							}
							var errtext = "Error " + errcode + ": " + errmsg;
							alert(errtext);
						}
					}
				}
			});
		} else {
			alert ("You have exceeded the timeframe for a bug report creation. Hurry up next time !!!!");
		}
	}


	);

	var checkTimeInterval = function(auth_timestamp) {
		var currentDate = new Date();
		var time = currentDate.getTime();
		//alert(auth_timestamp);
		if(!auth_timestamp) return true;
		//alert("Time= "  + time + "  auth_time= " + (auth_timestamp * 1000));
		if((time - (auth_timestamp * 1000)) < 300000) {
			return true;
		} else {
			return false;
		}
	}
	
	var convertToByteArray = function(data) {
		var bin=[] ;
		for (var i=0; i<data.length; i++){
			bin.push(data.charCodeAt(i));
		}
		return bin;
	}

	var createBugData = function() { 
		error = false;

		postdata = {};
		var temp = jQuery("#tracker-summary").val();
		if(temp=="")	{
			alert("Bug Title cannot be empty!");
			error = true;
			return;
		}else	{
			postdata['dcterms:title'] = temp;
		}

		temp = jQuery("#tracker-description").val();
		if(temp=="")	{
			alert("Bug description cannot be empty!");
			error = true;
			return;
		}else	{
			postdata['dcterms:description'] = temp;
		}

		temp = jQuery("#tracker-priority").val();
		if(temp!="none selected")	{
			postdata['helios_bt:priority'] = temp;
		}
	}

	function respondWithPostMessage(/*string*/ response) {
		window.parent.postMessage("oslc-response:" + response, "*");
			//TODO if window is not parented window.postMessage to be used
			//which shud be ignored
	}

	function refresh() {
		jQuery(".textclass").val('');
		
		jQuery(".listclass").each(function(item, index, array)	{
			item.selectedIndex = 0;
		});	
	}
});

	/* ]]> */</script>
    </head>
    <body bgcolor="#e8e6e6">
        <!--<h1><?php echo strtoupper($project); ?>: Creation UI for OSLC</h1><hr/>
         <p>some details of the query and blah blah to be added here</p> -->
<?php 
	require_once 'tracker/build_submission_form.php';
	artifact_submission_form($ath, $group);
?>
    </body>
</html>
