﻿
var messageSuccess = "<h1 class=\"mainTitle01\"><span>Warranty</span></h1><div class='Success'>Your request for a warranty claim has been forwarded to our service division. We will get back to you ASAP.</div>";
var messageFailed = "<h1 class=\"mainTitle01\"><span>Warranty</span></h1><div class='Failed'>There was an error while processing your data. Please refresh this page in a few minutes and try again.</div>";
var messageWaiting = "<div class='Waiting'>Please wait while we forward your request. This may take about a minute.</div>";

var requestWarranty = new Request.JSON({
	url: "Requests/MhiRequest.aspx",
	autoCancel: true,
	ref: 'ClaimWarranty',
	method: 'get',
	headers: { "Content-type": "application/json" },
	secure: true
});

requestWarranty.addEvent('onComplete', function(jsonObject, responseText) {
	var maincontent = $("mainArea01");
	maincontent.empty();
	maincontent.set('html', responseText == "ok" ? messageSuccess : messageFailed);
});

var validationIds = new Array(
		"FirstName",
		"LastName",
		"WhoAreYou",
		"Telephone",
		"Address",
        "Suburb",
		"State",
		"Postcode",
		"IndoorModelNo",
		"IndoorSerialNo",
		"OutdoorModelNo",
		"OutdoorSerialNo",
		"InstallationDate",
		"ErrorCode",
		"InstallerName",
		"Email",
		"FaultDescription"
    );

var validationPatterns = new Array(
		"",
		"",
		"",
		"",
		"",
		"",
		"",
		"",
		"",
		/[A-Za-z0-9]{11}/,
		"",
		/[A-Za-z0-9]{11}/,
		/^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/,
		"",
		"",
	/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i,
		""
    );    


    

var questions = new Array(
        "If the unit has a wireless remote control have you checked/replaced the batteries?",
        "Have you de-energized (power off) the unit for a minimum of 10 minutes and re-started the unit?",
        "Have you cleaned the filter?",
        "Have you contacted the installer and told them of the problem?",
        "Do have proof of purchase documentation to show the authorized warranty agent?",
        "I understand that if a part/component of the air-conditioner is not repaired or replaced by the authorized warranty agent then it is not covered by the warranty. A tax invoice will be issued to me for the authorized warranty agent's time which I agree to pay promptly."
    );

var noAnswers = new Array(
        "Please check/replace batteries &amp; recheck the unit.",
        "Please de-energized the unit for a minimum of 10 minutes and recheck the unit.",
        "Please clean the filter and recheck the unit.",
        "Please contact the installer and explain the problem.",
        "You need proof of purchase documentation in order to claim warranty.",
        "You must agree to payment terms in order to continue."
    );

var questionFormat = "<span>{question}</span><span class='Answers'><a class='answer' href='javascript:AskQuestion(true);'>{yes}</a></span><span class='Answers'><a class='answer' href='javascript:AnsweredNo();'>{no}</a></span>";

var currentQuestion = -1;
var requestData = "";

function AddAnswer(answer, classname) {
	classname = classname || "";
	var el = $("Questions").getElements(".Question").getLast();
	if (el) {
		el.getElement('span').adopt(
                new Element("strong", {
			'html': answer,
			'class': classname
		})
       );
	}
}

function RemoveYesNo() {
	$("Questions").getElements(".Answers").each(function(item, index, array) {
		item.destroy();
	});
}

function AnsweredNo() {

	AddAnswer(currentQuestion < questions.length - 1 ? "No" : "I Disagree", "No");
	RemoveYesNo();

	var div = new Element("div", {
		'class': 'FormRow AnswerNo',
		'html': noAnswers[currentQuestion]
	});

	div.inject($("Questions"), 'bottom');

	$('NextButtonContainer').setStyle('display', 'block');

	currentQuestion--;

}

function AskQuestion(sayYes) {

	var invalidFields = 0;
    var moreFields = new Array();
	requestData = "&CompanyName="  + escape($("CompanyName").value);
    requestData += "&CompanyPhone="  + escape($("CompanyPhone").value);
    requestData += "&CompanyAddress="  + escape($("CompanyAddress").value);
    requestData += "&CompanySuburb="  + escape($("CompanySuburb").value);
    requestData += "&InstallerPhoneNo="  + escape($("InstallerPhoneNo").value);
    requestData += "&InstallerAddress="  + escape($("InstallerAddress").value);
    requestData += "&InstallerSuburb="  + escape($("InstallerSuburb").value);

	validationIds.each(function(item, index, array) {

		var velement = $(item);

		var valid = false;
		var textbox = velement.get('tag').match(/input|textarea/);
		valid = textbox ? (velement.value != '') : (velement.selectedIndex > 0);

		if (valid && validationPatterns[index]) {
			valid = velement.value.match(validationPatterns[index]);
		}

		invalidFields += valid ? 0 : 1;

		var label = velement.getParent().getElement('label');
		if (label) {
			label.setStyle('color', valid ? '#ccc' : '#cc0000');
		}

		requestData += "&" + item + "=" + escape(velement.value);
	});
    
     
	$('FieldsError').setStyle('display', invalidFields > 0 ? 'block' : 'none');

	if (invalidFields == 0) {

		if (sayYes) {
			AddAnswer(currentQuestion < questions.length ? "Yes" : "I Agree", "Yes");
		}
		
		$('NextButtonContainer').setStyle('display', 'none');

		currentQuestion++;

		var Questions = $("Questions");
		if (currentQuestion == 0) {
			Questions.empty();
		} else {
			RemoveYesNo();
		}

		if (currentQuestion < questions.length) {

			var q = questions[currentQuestion];
			var qHtml = questionFormat.substitute({
				question: q,
				yes: currentQuestion < questions.length - 1 ? "Yes" : "I Agree",
				no: currentQuestion < questions.length - 1 ? "No" : "I Disagree"
			});

			var div = new Element("div", {
				'class': 'FormRow Question',
				'html': qHtml
			});

			div.inject(Questions, 'bottom');

		} else {
			$("WarrantyForm").destroy();
			var div = new Element("div", {
				'class': 'FormRow Question',
				'html': messageWaiting
			});
			div.inject(Questions, 'bottom');
			requestWarranty.send({ 'data': "c=ClaimWarranty" + requestData + "&time=" + new Date() });
		}

	} else {

	}

}

function NextButtonClicked() {

	AskQuestion();

	//

}

function InstallerChanged(value) {
    if (value) {    
        $("InstallerPhoneNoRow").style.display='block';
        $("InstallerAddressRow").style.display='block';
        $("InstallerSuburbRow").style.display='block';
    } else {
        $("InstallerPhoneNoRow").style.display='none';
        $("InstallerAddressRow").style.display='none';
        $("InstallerSuburbRow").style.display='none';
    }
}


function WhoAreYouSelected(value) {

    if (value=='Retailer' || value=='ServiceAgent'){
        $("CompanyNameRow").style.display='block';
        $("CompanyPhoneRow").style.display='block';
        $("CompanyAddressRow").style.display='block';
        $("CompanySuburbRow").style.display='block';
    } else{
        $("CompanyNameRow").style.display='none';
        $("CompanyPhoneRow").style.display='none';
        $("CompanyAddressRow").style.display='none';
        $("CompanySuburbRow").style.display='none';
    }

}
