// JavaScript Document

var PETITION_NOT_READY = 0;
var PETITION_READY = 1;

/**
 *
 */
function Petition() {
    /** */
    this.petitionState = PETITION_NOT_READY;
    /** */
	this.setPetitionState = pet_setPetitionState;
	/** */
	this.getPetitionState = pet_getPetitionState;
    
    /** */
    this.firstName   = "";
    /** */
    this.lastName   = "";
    /** */
	this.email    = "";
	
    /** */
    this.subscriptionProvider = "";
    /** */
	this.phoneMake = "";
	/** */
	this.town = "";
	/** */
	this.country = "";
	/** */
	this.requiresVideoNotification = false;
    
    /** */
    this.petitionHtml   = "";
	
	/** */
	this.recipientEmail = ROBS_EMAIL;
	
	/** */
	this.sendPetition   = pet_send;
	/** */
	this.getPetitionLetterByState = pet_getPetitionLetterByState;
	/** */
	this.getPetitionLetter = pet_getPetitionLetter;
}

/**
 *
 */
function pet_setPetitionState(aValue) {
    this.petitionState = aValue;
}

/**
 *
 */
function pet_getPetitionState() {
    return this.petitionState;
}

/**
 *
 */
function pet_send() {
    //
    //
    var retValue = false;
	//
	//
	var petitionLetter = this.getPetitionLetter(this.firstName + " " + this.lastName, this.email, this.subscriptionProvider, this.phoneMake);
	//
	// check input data
	if(petitionLetter && petitionLetter.length>0) {
	    //
	    //
	    var letterHtmlHead = "<html><head><title>Unpluggit petition letter</title></head><body>";
	    var letterHtmlStats = "<input type=\"hidden\" id=\"FIRST_NAME\" value=\"" + this.firstName + "\" />";
	    letterHtmlStats += "<input type=\"hidden\" id=\"LAST_NAME\" value=\"" + this.lastName + "\" />";
	    letterHtmlStats += "<input type=\"hidden\" id=\"SP\" value=\"" + this.subscriptionProvider + "\" />";
	    letterHtmlStats += "<input type=\"hidden\" id=\"PHONE_MAKE\" value=\"" + this.phoneMake + "\" />";
	    letterHtmlStats += "<input type=\"hidden\" id=\"TOWN\" value=\"" + this.town + "\" />";
	    var letterHtmlTail = "</body></html>";
	    var entireMessageBody = letterHtmlHead + petitionLetter + letterHtmlStats + letterHtmlTail;
	    //
		// create a send email command
		var decodedCmdSet = "";
		decodedCmdSet =  "<COMMANDS>";
		decodedCmdSet += "<COMMAND CLASS_NAME=\"com.tenduke.services.messaging.SendEmail\" CONTENT_TYPE=\"text/html\" ACCOUNT_ASSOCIATION=\"" + encode64("system") + "\" >";
		//
		// the email command contains the email spec that defines the message
	    var emailSpec = "<EMAIL_SPECIFICATION>";
	    emailSpec += "<RECIPIENTS>";					
	    emailSpec += "<RECIPIENT>" + encode64(this.recipientEmail) + "</RECIPIENT>";
	    emailSpec += "</RECIPIENTS>";
	    emailSpec += "<SUBJECT>" + encode64("Unpluggit petition from: " + this.email) + "</SUBJECT>";
	    emailSpec += "<BODY>" + encode64(entireMessageBody) + "</BODY>";
	    emailSpec += "</EMAIL_SPECIFICATION>";
	    //
	    // the email spec must be base64 encoded
		decodedCmdSet += encode64(emailSpec);
		decodedCmdSet += "</COMMAND>";
		decodedCmdSet += "</COMMANDS>";
		//
		// send async request
		var emailReq = initRequest("/servlets/XMLCommandServlet", "POST");
		emailReq.setRequestHeader("Content-Type", "text/xml; charset=\"UTF-8\"");
		try {
			emailReq.send(decodedCmdSet);
			retValue = true;
		}
		catch(cmdSendE) {
			emailReq = null;
		}
	}
	//
	//
	return retValue;
}

/**
 * Use the internal state of an instance to create the petition letter.
 */
function pet_getPetitionLetterByState() {
    var petitionLetter = this.getPetitionLetter(this.firstName + " " + this.lastName, this.email, this.subscriptionProvider, this.phoneMake);
    return petitionLetter;
}

/**
 * Gets petition letter content.
 */
function pet_getPetitionLetter(name, email, serviceProvier, phoneMake){
    //
    // create the petition letter by current date
    var currentDate = new Date();
    var petitionLetterHtml = PETITION_LETTER_HTML.replace(/DATE_INSERT/, "" + currentDate.toLocaleDateString());
    petitionLetterHtml = petitionLetterHtml.split("PHONE_COMPANY_INSERT").join(serviceProvier);
    petitionLetterHtml = petitionLetterHtml.split("PHONE_MAKE_INSERT").join(phoneMake);
    petitionLetterHtml = petitionLetterHtml.replace(/NAME_INSERT/, name);
    petitionLetterHtml = petitionLetterHtml.replace(/EMAIL_INSERT/, email);
    petitionLetterHtml = petitionLetterHtml.replace(/TOWN_INSERT/, this.town);
    petitionLetterHtml = petitionLetterHtml.replace(/COUNTRY_INSERT/, this.country);
    return petitionLetterHtml;
}
