function init() {
$(function() {
		$( "#dialog:ui-dialog" ).dialog( "destroy" );
		
		$( "#dialog-form" ).dialog({
			autoOpen: false,
			height: 450,
			width: 350,
			modal: true,
			buttons: {
				"Cancel": function() {
					$( this ).dialog( "close" );
				},
				"Send":function() {
					sendContact();
				}
			}
		});
		
		$( "#dialog-form-trial" ).dialog({
			autoOpen: false,
			height: 450,
			width: 350,
			modal: true,
			buttons: {
				"Cancel": function() {
					$( this ).dialog( "close" );
				},
				"Send":function() {
					sendTrialRequest();
				}
			}
		});
		$("#dialog-message" ).dialog({
			autoOpen: false,
			buttons: {
				Ok: function() {
					$( this ).css( "visibility", "hidden" )
					$( this ).dialog( "close" );
				}
			}
		});
		$("#dialog").dialog({
	   	 	autoOpen: false,
	   	 	height: 400,
	   	 	width:600,
	   	 	buttons: { 
		   	 	"Compute": function() { 
			   	 	var response = QuestionnaireHelper.submit('1') ;
   					if(response=== false) {
	   					return;
   					}

					line = new Array();
					ticks = new Array();
   					for(x in response){
	   					line.push(response[x]);
						ticks.push(x);
	   				}
   					$('#dialog').html('<div id="chart" style="height:300px;width:500px; "></div>');
   					plot3 = $.jqplot('chart', [line], {
   					    legend:{show:true, location:'ne'},
   					    title:'Test Maturity Assessment',
   					    seriesDefaults:{
   					        renderer:$.jqplot.BarRenderer, 
   					        rendererOptions:{barPadding: 6, barMargin:15}, 
   					        shadowAngle:135},
   					    series:[
   					        {label:'Scores'}
   					    ],
   					    axes:{
   					        xaxis:{renderer:$.jqplot.CategoryAxisRenderer,ticks:ticks}, 
   					        yaxis:{min:0,
	   					        tickOptions:{ 
   					            formatString:'%.0f' } }
   					    }
   					});
   	 			},
   	 			"Close":function() {
   	 				$('#dialog').html("");
	   	 			questionnaire = new Questionnaire();
	   				questionnaire.load(1);
	   				document.getElementById("dialog").appendChild(questionnaire.getAsHTMLEntity());
	 				$(this).dialog("close"); 
   	 			}
			}
	});
	  
   	questionnaire = new Questionnaire();
	questionnaire.load(1);
	document.getElementById("dialog").appendChild(questionnaire.getAsHTMLEntity());
	QuestionnaireHelper.register(questionnaire);
	
	var URLParam = document.URL.split('?');
	if(URLParam.length == 2){
		var param = URLParam[1].split('=');
		if(param.length==2 && param[0]=="poll"){
			if(param[1]=="true") {
				$("#dialog").dialog("open");
			}
		}
	}
	});
}

function openQuestionnaire() {
	$("#dialog").dialog("open");
}

function sendContact() {
	var fName = document.getElementById('firstname').value;
	if($.trim(fName).length == 0) {
		return false;
	}
	
	var lName = document.getElementById('lastname').value;
	if($.trim(lName).length == 0) {
		return false;
	}
	
	var email = document.getElementById('email').value;
	if($.trim(email).length == 0) {
		return false;
	}
	
	var comment = document.getElementById('comment').value;
	if($.trim(comment).length == 0) {
		return false;
	}

	$.ajax({
	  type: 'POST',
	  url: "process.php",
	  data: { fName: fName, lName: lName, email:email,comment:comment},
	  success: function(data){
		if(data.error) {
			alert(data.error);
		}
		else {
			document.getElementById("contactForm").reset();
			$( "#dialog-form" ).css( "visibility", "hidden" );
			$( "#dialog-message" ).html(data.message);
			$( "#dialog-message" ).dialog("open");
			$( "#dialog-message" ).css( "visibility", "visible" );
			$( "#dialog-form" ).dialog( "close" );
		}
	  },
	  dataType: "json"
	});
}

function sendTrialRequest() {
	var fName = document.getElementById('firstname-trial').value;
	if($.trim(fName).length == 0) {
		alert('Firstname is mandatory');
		return false;
	}
	
	var lName = document.getElementById('lastname-trial').value;
	if($.trim(lName).length == 0) {
		alert('Lastname is mandatory');
		return false;
	}
	
	var email = document.getElementById('email-trial').value;
	if($.trim(email).length == 0) {
		alert('Email is mandatory');
		return false;
	}
	
	var company = document.getElementById('company-trial').value;
	
	$.ajax({
	  type: 'POST',
	  url: "process.php",
	  data: { fName: fName, lName: lName, email:email,company:company},
	  success: function(data){
		if(data.error) {
			alert(data.error);
		}
		else {
			document.getElementById("trialForm").reset();
			$( "#dialog-form-trial" ).css( "visibility", "hidden" );
			$( "#dialog-message" ).html(data.message);
			$( "#dialog-message" ).dialog("open");
			$( "#dialog-message" ).css( "visibility", "visible" );
			$( "#dialog-form-trial" ).dialog( "close" );
		}
	  },
	  dataType: "json"
	});
}

function loadForm(type) {
		if(type == "contact") {
			$( "#dialog-form" ).dialog( "open" );
			$( "#dialog-form" ).css( "visibility", "visible" )
		}
		else {
			$( "#dialog-form-trial" ).dialog( "open" );
			$( "#dialog-form-trial" ).css( "visibility", "visible" );
		}
}
