// Render the StyleSheet
document.writeln("<style>");
document.writeln(".jqmFW {display: none;position: fixed;top: 17%;left: 50%;margin-left: -200px; font-family:verdana,sans-serif;font-size: 11px;background-color: #E7E7E7;color: #000000; border:1px solid #6699CC; padding:0px;}");
document.writeln(".FWin {width: 385px;}");
document.writeln(".FTab {width: 100%;}");
document.writeln(".FB_MsgLenError {margin:0px;font-size:10px;color:red;}");
document.writeln("#femail {font-family:verdana,sans-serif;font-size: 11px;}");
document.writeln("#fmessage { width:370px; font-family:verdana,sans-serif;font-size: 11px;}");
document.writeln(".FHead { width: 50%; color:#ffffff; font-family:verdana,sans-serif;font-size: 11px; background-color:#6699CC;}");
document.writeln(".FBody { padding-top:8px; padding-left:8px; text-align:left; width: 100%; font-family:verdana,sans-serif; font-size: 11px;}");
document.writeln("* html .jqmFW {width:238px; position: absolute;top: expression((document.documentElement.scrollTop || document.body.scrollTop) + Math.round(17 * (document.documentElement.offsetHeight || document.body.clientHeight) / 100) + 'px');}");
document.writeln(".jqmOverlay { background-color: #262626; }");
document.writeln("a.jqmClose, .jqmClose { color: #ffffff;}");
document.writeln(".groovybutton {background-image:url(/.lib/images/back03.gif);border:1px solid #DDDDDD;font-family:Verdana,sans-serif;font-size:11px;height:20px;}");
document.writeln("</style>");

// Failsafe catch if serviceURL is not defined
if ( typeof(serviceURL) == "undefined")
	var serviceURL = 'http://192.168.10.93/mpsdk/processDownload.php'; 

// Failsafe catch in case thisUrl is not defined
var FB_thisUrlNotSet = typeof(thisURL) == "undefined";
if (FB_thisUrlNotSet)
	var thisURL = document.location.protocol + "//" + document.location.hostname + document.location.pathname;

var file_name='';

var FB_Required = { // validate in reverse for focus order as onscreen...
	fmessage:true,
	ftitle:false,
	furl:false,
	femail:false};

var FB_Localized = {
		feedbackhdr:'Download File',
		feedbacksaveerror:'Sorry the system is busy, try again later.',
		feedbacksent:'Thanks. Now you can Download file.',
		feedbackemail:'Please input your email address (Optional):',
		feedbackmessage:'File Path:',
		feedbacksubmit:'Submit',
		feedbackclose:'Close',
		feedbacklengthTooLong:'(Feedback too long please shorten)',
		urlToDownload:'',
		feedbacktotallen:1900
	};
var FB_T = function(t){return FB_Localized[t]||t;}

// run after page is entirely done loading
$(document).ready(function() {

	$(document.body).prepend(
			  '<div class="jqmFW" id="fb2">'
			+ '	<div class="FWin" id="fbm">'
			+ '	<table class=FTab cellpadding=5 cellspacing=0>'
			+ '	<tr><td class="FHead" align=left><b>' + FB_T('feedbackhdr') + '</b>&nbsp;&nbsp;</td><td align=right class="FHead"><a href="#" class="jqmClose" style="color: #ffffff;">' + FB_T('feedbackclose') + '</a></td></tr>'
			+ '	<tr><td colspan="2" class="FBody">'
			+ '		<div id="FB_Download1" class="FB_MsgLenError">&nbsp;</div>'
			+ '		<div>' + FB_T('feedbackemail') + '</div><input type="text" size="40" maxlength="255" id="femail"><input type="hidden" id="ftitle"><input type="hidden" id="furl"><br><br>'
			+ '		<div>' + FB_T('feedbackmessage') + '</div><input type="text" id="fmessage" size="40" readOnly>'
			+ '		<br><br><input id="link_id" type="button" class="groovybutton" onclick="javascript:processFeedback();" value="Download File"><br>'
			+ '	</td><tr>'
			+ '	<tr><td class="FHead" align=left colspan=2>Your use of this webpage is governed by the <a href="http://softwarecommunity.intel.com/articles/eng/1517.htm" target="_Blank">terms of use</a>  for this website.</td></tr>'
			+ '	</table>'
			+ '	</div>'
			+ '</div>'
	);
	$('#FB_LenError').html(FB_T('feedbacklengthTooLong'));
	$('#FB_LenError').hide();
	
	
});


function ead(fileName, feedbackURL)
{
	if ( arguments.length == 0)
	{
		$("#ftitle").val(document.title);
		$("#furl").val(thisURL);
	}
	else
	{
		$("#fmessage").val(fileName);
		//file_name = fileName;
		//$('#FB_DownloadPre').html("<center><strong>Please provide us your email address.<br>OR</strong></center>");
		//$('#FB_Terms').html("");
		//$('#FB_Download').hide();
	}
	

	//$("#fmessage").val("");
	$("#fb2").jqm().jqmShow();
}

function getClose(link)
{
	var lnk=document.getElementById("link_id");
	window.location = link;
	$("#fb2").jqmHide();
}	



//User clicked on the Submit Feedback Button
function processFeedback()
{
	// P1=encodeURIComponent()
	// P2=
	// P3=
	serviceURL = 'processDownload.php';
	

	var err;
	for (var j in FB_Required) {
		$('#' + j).val($.trim($('#' + j).val()));
		$('#' + j + 'err').remove();
		if ( (j == "femail" && $("#femail").val()!="")) 
		{
			if ( (j == "femail" && !isValidEmail($("#femail").val()))) {
				err = j;
				$('#' + j).prev().append('<font id=' + j + 'err' + ' color=red><br>* Invalid email address.</font>');
			}
		}
	}

	var sendQS = "";
	sendQS += ("P2=" + escape($('#fmessage').val()));
	sendQS += ("&P1=" + escape($('#femail').val()));

	//alert(sendQS);

	if (err!=null) $('#' + err).focus();
	if (sendQS.length>FB_T('feedbacktotallen')) { err=true; $('#FB_LenError').show(); }
	if (err!=null) return;
	$('#FB_Download').show();
	doHttpRequest(serviceURL , sendQS);
	$("#femail").val("");
	$('#FB_DownloadPre').html("<center><strong>Thank you for providing us with your email address.<br></strong></center>");
	//processFeedbackCallback();
	fileName = escape($('#fmessage').val());
	//alert(fileName);
	getClose("download_file.php?file="+ fileName);
	//$('#FB_Downloadform').html("");
	//document.location(sendQS);
	
}

function processFeedbackCallback()
{
	
}

// very simple email check
function isValidEmail(str) {
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function getData1(url, getString) {
	doHttpRequest(url);
}




	function doHttpRequest(url, getString) {  // This function does the AJAX request
 
	//alert("asdsad");
	//  Set our destination PHP page "ajaxpost.php"…
	http.open("POST", url, true);
	http.onreadystatechange = getHttpRes;

	// Make our POST parameters string…
	var params = getString;

	//alert(params);

	// Set our POST header correctly…
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	// Send the parms data…
	http.send(params);

	//res = http.responseText;

	}

	function getHttpRes() {
		if (http.readyState == 4 && http.status == 200) { 
			res = http.responseText;  // These following lines get the response and update the page
			//document.write(res);
		}
	}

	function getXHTTP( ) {
	var xhttp;
	try {   // The following "try" blocks get the XMLHTTP object for various browsers…
      xhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
 		 // This block handles Mozilla/Firefox browsers...
	    try {
	      xhttp = new XMLHttpRequest();
	    } catch (e3) {
	      xhttp = false;
	    }
      }
    }
	return xhttp; // Return the XMLHTTP object
	}

	var http = getXHTTP(); // This executes when the page first loads.
	
