GetWarrantyModule = Class.create();

GetWarrantyModule.prototype = {
	
	'killSwitch': undefined,
	
	'initialize': function() {
		
		// stop the form from being submitted
		Event.observe($('serialnumbercheck'), 'submit', function(evt) { Event.stop(evt); });


		// use this code to disable the module and all its callback methods
		// parent.killSwitch = true; 
		// or you can do it selectively
		// if (location.href.match('/ipod/')) parent.killSwitch = true;

		// kill it if the module html is missing
		if (!$('warrantycheckpsp')) parent.killSwitch = true;

		// don't go any further if killSwitch is on
		if (parent.killSwitch) return;


		// initialize the warranty status module, for both user-initiated and cookie-initiated cases
		// cookie-initiated requests happen automatically on page load, so we must provide callbacks 
		// and handle results and errors even if we don't show the module on the page
		if (warrantyChecker==undefined) {
			// instantiate warranty checker with cookies off (pass true to turn cookies on)
			warrantyChecker = new WarrantyChecker(false);
		}
		var sRequestUrl = 'https://selfsolve.apple.com/warrantyChecker.do';
		this.oWarranty = warrantyChecker.processRequest( 'serialnumber', 'getwarrantysubmitbutton', this.showErrors, this.showSpinner, errMsgData, this.getWarrantyStatus, this.onFailure, sRequestUrl);


		// ok now we're good to go
		this.renderKeys();
		$('warrantycheckpsp').show();
		
		var snCookie = cookieJar.getCookie('ac_ags');
		var params = location.href.toQueryParams();
		if (params.newid || snCookie==undefined || snCookie=='') {
			$('serialnumbercheck').show();
		} else {
			$('cookiespinner').show();
			var cookieletVal = cookieJar.getCookieletValue(snCookie, "id");
			//warrantyChecker.submitRequestFromUrl(cookieletVal, warrantyChecker.WCResponse, warrantyChecker.defaultClientFailureCB);
			warrantyChecker.submitRequestFromUrl(cookieletVal, warrantyChecker.WCResponse, this.showErrors);
		}

	},

	'renderKeys': function() {

		// access keys as an array based on language
		var lang = ACWPod.getLang();
		var keyObj = eval('keys.' + lang);
		if (keyObj == null) keyObj = keys.en; // default to en if lang not found

		// look for all elements of type=key and update with named key
		aElem = $$('[type=key]');
		for(var i=0; i<aElem.length; i++) {
			var keyName = aElem[i].readAttribute('name');
			aElem[i].update(keyObj[keyName]);
		}
		
		// set error messages
		errMsgData.errEmpty = keyObj['wc.check.err.front.serialnumberrequired'];
		errMsgData.errLength = keyObj['wc.check.err.front.serialnumbercharacterlengthinvalid'];
		errMsgData.errChar = keyObj['wc.check.err.front.serialnumbercharacterinvalid'];
	},

	'showSpinner': function() {

		$('warrantycheckspinner').show();
	},
	
	'showErrors': function(msg) {
		$('serialnumber').value = "";
		$('warrantycheckspinner').hide();
		$('cookiespinner').hide();
		$('serialnumbercheck').show();
		
		var snCookie = cookieJar.getCookie('ac_ags');
		if (snCookie==undefined || snCookie=='') {
			// show error if no cookie
			$('errorDiv').show();
			$('error').update(msg);
		} 
		else {
			// clear the cookie which caused the error
			cookieJar.deleteCookie('ac_ags', "/", ".apple.com", null);
		}
	},
	
	'getWarrantyStatus': function(responseObj) {

		if (parent.killSwitch) return;

		var json = responseObj;
		
		// update product information
		var prodImg = '<img src="' + json.getProductImageUrl() + '">';
		$('productimage').update(prodImg);
		$('productname').update(json.getProductDescription());
		if(json.getIsIMEINum()) {
			$('imeiLabel').show();
			$('productsn').update(json.getAPIMEINum());
		}
		else {
			$('snLabel').show();
			$('productsn').update(json.getSerialNumber());
		}

		// check if iphone and not activated
		if (json.getIsIphoneFlag() && !json.getIphoneActive()) {
			
			$('registration-icon').writeAttribute('src', 'http://images.apple.com/support/applecare/images/warranty_status_yellow.png');
			$('activation-false').show();
			$('registration-text').update(json.getIphoneNotActivatedContent());
			$('serviceoptions-button').show();
			Event.observe('serviceoptions-button', 'click', function() {
				window.location.href=json.getIphoneNotActivatedBtnLink();
			});
			$('phone').hide();
			$('hardware').hide();
			$('app').hide();
		}
		else if (!json.getIsRegistered()) {
			// product is not registered
			$('registration-icon').writeAttribute('src', 'http://images.apple.com/support/applecare/images/warranty_status_red.png');
			$('registration-false').show();
			$('registration-button').show();
			Event.observe('registration-button', 'click', function() {
				window.location.href ='https://selfsolve.apple.com/GetProductDetails.do?productRegister=Y&country=' + json.getCountryCode() + '&id='+ ((json.getIsIMEINum()) ? json.getAPIMEINum() : json.getSerialNumber());
			});
			$('phone').hide();
			$('hardware').hide();
			$('app').hide();
			$('registration-text').update(json.getRegisteredOrNotRegisteredText('long'));
		}
		else { 
			// registered
			// no need to show registration message
			$('registration').hide();
		}

		// phone coverage
		$('phone-subheader').update(json.getPhoneSupportSubHeader());
		$('phone-text').update(json.getPhoneSupportCoverageText('short'));
		if (json.getPhoneSupportHasCoverage()) {
			$('phone-icon').writeAttribute('src', 'http://images.apple.com/support/applecare/images/warranty_status_green.png')
			$('phone-expiration').update(json.getPhoneSupportCoverageEndDate());
		}
		else {
			$('phone-icon').writeAttribute('src', 'http://images.apple.com/support/applecare/images/warranty_status_yellow.png')
			$('phone-expiration').up('div').hide();
		}
		if (json.getPhoneSupportCoverageValue()=='90D') {
			$('phone-more').show();
		}
		Event.observe('phone-button', 'click', function()
		{
			window.location.href=json.getPhoneSupportLink(); 
		});

		// hardware coverage
		$('hardware-subheader').update(json.getHWRepairSubHeader());
		$('hardware-text').update(json.getHardwareSupportCoverageText('short'));
		if (json.getHWSupportHasCoverage()) {
			$('hardware-icon').writeAttribute('src', 'http://images.apple.com/support/applecare/images/warranty_status_green.png')
			$('hardware-expiration').update(json.getHWSupportCoverageEndDate());
		} 
		else {
			$('hardware-icon').writeAttribute('src', 'http://images.apple.com/support/applecare/images/warranty_status_yellow.png')
			$('hardware-expiration').up('div').hide();
		}
		if (json.getHWSupportCoverageValue()=='LI') {
			$('hardware-more').show();
		}
		Event.observe('hardware-button', 'click', function()
		{ 
			window.location.href='https://selfsolve.apple.com/GetWarranty.do?sn=' + json.getSerialNumber() + '&cn=' + json.getCountryCode();
		});
		
		// app eligibity
		$('app-subheader').update(json.getEligibilitySubHeader());
		if (json.getIsAPPEligible()) {
			$('app-icon').writeAttribute('src', 'http://images.apple.com/support/applecare/images/warranty_status_yellow.png')
			var id = json.getSerialNumber();
			if(json.getIsIMEINum())
				id = json.getAPIMEINum();
			$('app-text').update(json.getEligibilityText('t1', 'long'));
			Event.observe('app-button', 'click', function()
			{ 
				window.location.href='https://selfsolve.apple.com/AgrOfferFlow.do?country=' + json.getCountryCode()  + '&id=' + id + '&AGR_TYPE=' + json.getAgreementPurchaseType() + '&AGR_TEMP_ID=' + json.getAPPTemplateId();
			});
		}
		else if (json.getHWSupportCoverageValue()=='PP') {
			$('app-icon').writeAttribute('src', 'http://images.apple.com/support/applecare/images/warranty_status_green.png')
			// covered by APP, hide button
			$('app-button').hide();
		} 
		else {
			// hide the entire row
			$('app').hide();
		}

		// reset UI
		$('warrantycheckspinner').hide();
		$('cookiespinner').hide();
		$('errorDiv').hide()
		$('serialnumbercheck').hide()

		// show product & coverage info
		$('product').show();
		$('results').show();

	},

	onFailure: function() { 

		if (parent.killSwitch) return;

		// werver error
		$('warrantycheckspinner').hide();
		$('cookiespinner').hide();
		//alert('Server error. Please try again.');
		$('serialnumber').activate();
	},
	
	checkAnotherSN: function() {
		
		// use browser detection to force a reload in IE6 & 7
		// since we will lose newID=Y in the url, we also clear the cookie, if any
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { // test for MSIE x.x;
			var IEVersion = new Number(RegExp.$1) // capture x.x portion and store as a number
		 	if (IEVersion < 8) {
				cookieJar.deleteCookie('ac_ags', "/", ".apple.com", null);
				window.location.reload(true);
			}
		} 
		else {
			// just reload the page normally like normal people
			window.location.href = 'index.html?newID=Y';
		}
	}

}

var getWarrantyModule, warrantyChecker;
var errMsgData = { 'errEmpty' : '', 'errLength' : '', 'errChar' : '' };


Event.observe(window, 'load', function() {
	
	// *** need omniture tracking here?

	// create and initialize the page
	if (getWarrantyModule == undefined) {
		getWarrantyModule = new GetWarrantyModule();
	}

});
