/**
 * Copyright Notice
 * This file contains proprietary information of Valspar Corporation
 * Copying or reproduction without prior written approval is prohibited.
 * Copyright (c) 2011
 */

/**
 *
 * File Name		:	msds.js
 * Type             :   JavaScript file
 *
 *----------------------------------------------------------------------------
 *                        Modification Log
 *----------------------------------------------------------------------------
 * $Log: $
 *----------------------------------------------------------------------------
 */

/**
 * The following section should not be changed
 */

function MSDS(contextPath, format, requestType, language, item) {
	if (format && format != null)
		this.format = format;
	else
		this.format = "xml";

	if (requestType && requestType != null)
		this.requestType = requestType;
	else
		requestType = "upc-code";

	if (item && item != null)
		this.itemValue = item;
	else
		this.itemValue = undefined;

	if (language && language != null)
		this.language = language;
	else
		this.language = "EN";

	this.statusMessage = undefined;
	this.statusCode = undefined;
	this.documentUrl = undefined;
	this.onerror = this.errorHandler;
	this.onsuccess = this.successHandler;
	this.object = undefined;

	if (contextPath && contextPath != null)
		this.contextPath = contextPath;
	else
		this.contextPath = "/wservices";
};

/**
 * Constructor
 * 
 * @param verifyUrl -
 *            Url where the verification will happen
 * @param documentUrl -
 *            Url where the document can be retrieved
 * @param format -
 *            Return format of messages, currently supports json and xml
 * @param requestType -
 *            upc-code or item-number
 */
MSDS.noop = function() {
	return this;
};

MSDS.prototype.setLanguage = function(l) {
	this.language = l;
};

MSDS.prototype.getLanguage = function() {
	return this.language;
};

MSDS.prototype.setDocumentUrl = function(d) {
	this.documentUrl = d;
};

MSDS.prototype.getDocumentUrl = function() {
	return this.documentUrl;
};

MSDS.prototype.setFormat = function(f) {
	this.format = f;
};

MSDS.prototype.getFormat = function() {
	return this.format;
};

MSDS.prototype.setRequestType = function(r) {
	this.requestType = r;
};

MSDS.prototype.getRequestType = function() {
	return this.requestType;
};

MSDS.prototype.setItemValue = function(v) {
	this.itemValue = v;
};

MSDS.prototype.getItemValue = function() {
	return this.itemValue;
};

MSDS.prototype.setStatusCode = function(c) {
	this.statusCode = c;
};

MSDS.prototype.getStatusCode = function() {
	return this.statusCode;
};

MSDS.prototype.setStatusMessage = function(m) {
	this.statusMessage = m;
};

MSDS.prototype.getStatusMessage = function() {
	return this.statusCode;
};

MSDS.prototype.setObject = function(o) {
	this.object = o;
};

MSDS.prototype.getObject = function() {
	return this.object;
};

MSDS.prototype.getContextPath = function() {
	return this.contextPath;
};

MSDS.prototype.setContextPath = function(c) {
	this.contextPath = c;
};

/**
 * This method returns a link to retrieve the MSDS from DB
 */
MSDS.prototype.getMsds = function() {
	var msdsObj = this;
	try {
		if (msdsObj.getItemValue() != undefined) {
			jQuery.get(msdsObj.getVerifyUrl(), {
				type : msdsObj.getRequestType(),
				item : msdsObj.getItemValue(),
				format : msdsObj.getFormat(),
				ts : new Date().getTime()
			}, function(data) {
				msdsObj.setStatusCode(data.status);
				msdsObj.setStatusMessage(data.status_message);
				if (data.status == '200') {
					msdsObj.setReturnUrl(msdsObj.documentUrl + '?id='
							+ data.encoded_item);
					msdsObj.onsuccess.call(msdsObj);
				}
				else {
					this.setReturnUrl(undefined);
					this.onerror.call(msdsObj);
				}
			});
		}
		else
			alert("Item to be processed is undefined! Please set the itemValue property.");
	} catch (e) {
		alert(e.message);
	}
};

/**
 * This method check for MSDS if it exists in the filesystem
 */
MSDS.prototype.getMsdsByUpcFile = function(u) {
	if (u != null)
		this.setItemValue(u);
	var msdsObj = this;
	try {
		if (msdsObj.getItemValue() != undefined) {
			jQuery.post("/wservices/xml/msds/getmsdsbyupc", {
				upc : this.getItemValue(),
				lang : this.language
			}, function(data, status) {
				msdsObj.setStatusMessage(status);
				msdsObj.setStatusCode(status);
				if (status == 'success') {
					node = data.getElementsByTagName("fault");
					msdsObj.object = data;
					if (node && node.length > 0)
						msdsObj.onerror();
					else
						msdsObj.onsuccess();
				}
				else {
					msds.object = undefined;
					msdsObj.setDocumentUrl(undefined);
					msdsObj.onerror();
				}
			});
		}
		else
			alert("Item to be processed is undefined! Please set the itemValue property.");
	} catch (e) {
		alert(e.message);
	}
};

/**
 * Default success Handler that will re-direct the page to the MSDS
 */
MSDS.prototype.successHandler = function() {
	if (this.getFormat() == 'xml') {
		x = this.getObject().getElementsByTagName("msds")[0];
		if (x) {
			x = this.getObject().getElementsByTagName("msds")[0].attributes;

			document.location.href = this.getContextPath()
					+ x.getNamedItem("uri").nodeValue;
		}
		else {
			alert("Could not parse object");
		}
	}
	else
		if (this.getFormat() == "json") {
			alert("json processing not implemented yet!");
		}
};

/**
 * Default Error Handler
 */
MSDS.prototype.errorHandler = function() {
	if (this.getObject()) {
		node = this.getObject().getElementsByTagName("msg");
		if (node && node.length > 0) {
			alert(node[0].childNodes[0].nodeValue);
		}
		else {
			alert("The requested MSDS could not be retrieved from the server.");
		}
	}
	else {
		alert("The request could not complete. Status returned : "
				+ this.statusCode);
	}
};
