/*
 *
 * Copyright (C) 2009 EmbedOne.
 * Contact: EmbedOne Software Information (info@embedone.com)
 *
 * Commercial Usage
 * Licensees holding valid EmbedOne Commercial licenses may use this file
 * in accordance with the EmbedOne Commercial License Agreement provided
 * with the Software or, alternatively, in accordance with the terms
 * contained in a written agreement between you and EmbedOne.
 *
 * GNU General Public License Usage
 * Alternatively, this file may be used under the terms of the GNU
 * General Public License version 3.0 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.  Please review the following information to
 * ensure the GNU General Public License version 3.0 requirements will be
 * met: http://www.gnu.org/copyleft/gpl.html.
 *
 * If you are unsure which license is appropriate for your use, please
 * contact the EmbedOne sales department at sales@embedone.com.
 *
 */

var objTxtBug = new oTxtBug();

// Creating an object with multiple constants
function oTxtBug(){
	this.iTitle = "<h1>Report a Bug</h1>";
	this.iSubNav ="<ul> "
					+"</ul>"
					;
	this.iContent = '<textarea name="data" id="data" rows="10" cols="80">Name:\nE-mail:\nOrganization:\nMessage:\n-----------------------------------------------\nThank you for submitting this error to us. Please write short description of the problem to the space above.</textarea>';
/*	with logsthis.iActions = '<div id="send_params"><input id="iAutoSubmit" type="checkbox">Automatic e-mail</input><br><input id="iLogSubmit" type="checkbox" checked=true>Attach logs</input><br></div><input name="apply_changes" type="submit" id="button" value="Submit Report" onclick="mBugReport();" /> ' 
					;*/
	this.iActions = '<div id="send_params"><input id="iLogSubmit" type="checkbox" checked=true>Attach logs</input><br></div><input name="apply_changes" type="submit" id="button" value="Submit Report" onclick="mBugReport();" /> ' 
					;
	this.iFooter = '<div id="copyright"> '
			+"<p>Copyright &copy; 2009 EmbedOne. All rights reserved.</p> "
			+"</div>"
			;
}


// this method waits till the TO comes to 0
var vMailCallTO = 0;
function mSendFinnish( dResult ){

	clearTimeout(vMailCallTO);
	mSetWrapper( numConstWrapperStateMain );
	
	var vResultMessage="";
	if( dResult ){
		vResultMessage = "Report was sent to Embedone! \n";
		if( vLogAttached ){
			vResultMessage = vResultMessage + "Logs were attached! \n";
		}else{
			vResultMessage = vResultMessage + "Logs were NOT attached!  \n";		
		}
	}else{
		vResultMessage = "Error while sending the report. Please try again later!\n";
	}
	vResultMessage = vResultMessage + "Total bytes sent: " + dSentBytes;
	document.getElementById("data").value = vResultMessage;
}

function mMailTimeout(dCallback, dRemaining){
	var vInterval = 500;

		if( dRemaining == 0 ){
			eval( dCallback );
	
		} else {
	
			dRemaining = dRemaining - vInterval;
			if( dRemaining < 0 ){
				dRemaining = 0;
			}
			vMailCallTO=setTimeout('mMailTimeout("'+dCallback+'",'+ dRemaining +')', vInterval);
		}
}


function mLimitData(dLen){
	if( document.getElementById("data").value.length > dLen ){
		document.getElementById("data").value=document.getElementById("data").value.substring(0, dLen);
	}
}

var dReportBody;
var dReportDone;
var dReportPart = 1000;
var dSentBytes = 0;
var vLogAttached=false;
function mSendReport( dSessionId ){
//	alert("got id" + dSessionId);
	if( (dReportBody.length - dReportDone) > dReportPart ){
		mSendData( "mSendReport", dSessionId, null, dReportBody.substr( dReportDone, dReportPart));
		dReportDone = dReportDone + dReportPart;
	}else{
		//alert("Sending last");
		mSendData( "mSendFinnish", dSessionId, "end", dReportBody.substr( dReportDone, dReportBody.length - dReportDone));	
		dReportDone = dReportDone + (dReportBody.length - dReportDone);
	}
}


function mSendData( dCallback, dSession, dCommand ,dText ){
	var aa = "http://www.meshcom.com/firmware-eval/report.php";

	var url = aa+"?callback="+dCallback;
	if( dSession != null ){
		url = url +"&sid="+dSession;
	}
	url = url + "&mail_command="+dCommand+"&text="+encodeURIComponent(dText);
    
    var script = document.createElement("script");        
    script.setAttribute("src",url);
    script.setAttribute("type","text/javascript");                
    document.body.appendChild(script);
}

	
function mBugReport(){
	var data_message =  document.getElementById("data").value;

	
//	var body_message= mPreProcessLine(data_message, true);
	var body_message= data_message;
	vLogAttached = false;
	if( document.getElementById("iLogSubmit").checked ){
		vLogAttached = true;
/*
		body_message = body_message + "STATLOG: %0D" + encodeURIComponent(mPreProcessLine(vOldStatData)) + "%0D%0D";
		body_message = body_message + "CONFLOG: %0D" + encodeURIComponent(mPreProcessLine(mCreateRawText( objNewKeys )), false) + "%0D%0D";
		body_message = body_message + "DIFFLOG: %0D" + encodeURIComponent(mPreProcessLine(mCreateRawText( mComposeReviewObject())), false) + "%0D%0D";
		*/
		body_message = body_message + "\n\nSTATLOG:\n" + vOldStatData + "\n\n";
		body_message = body_message + "CONFLOG:\n" + mCreateRawText( objNewKeys ) + "\n\n";
		body_message = body_message + "DIFFLOG:\n" + mCreateRawText( mComposeReviewObject()) + "\n\n";

		}
	dSentBytes = body_message.length;
	dReportBody = body_message;
	dReportDone = 0;
	mSetWrapper( numConstWrapperStateWaiting );
	mSendData( "mSendReport", null, null, "");
	vMailCallTO=setTimeout('mMailTimeout("mSendFinnish(false)",'+ 30000 +')', 500);
}


function mPreProcessLine(dLine, dUseSeparatorDetection){
		var retLine="";
		if( dLine == null ){
			return retLine;
		}

		// ugly browser dependent part
		if( dUseSeparatorDetection){
			if( navigator.appName == "Microsoft Internet Explorer"){
				vLines = dLine.split('\r\n');
			}else{
				vLines = dLine.split('\n');
			}
		}else{
				vLines = dLine.split('\n');		
		}

		for( var vCkl = 0; vCkl < vLines.length; vCkl = vCkl + 1 ){
			retLine = retLine + vLines[vCkl]+"%0D";
		}
		
		return retLine;
}

