﻿// This javascript file contains implementation for common ajax and
// XML related functionality.
//
var UPSAjaxObject = new Object();

// Generic Key-Value pair object.
UPSAjaxObject.KeyValuePair = function(k,v){
	this.key = k;
	this.value = v;
}

UPSAjaxObject.KeyValuePair.prototype = {
	initialize:function(k,v){
		this.key = k;
		this.value = v;
	},
	
	getKey:function(){
		return this.key;
	},
	
	getValue:function(){
		return this.value;
	},
	
	toString:function(){
		return "Key: " + this.key + ", Value: " + this.value;
	}
};

UPSAjaxObject.AjaxEngine = function(){};

UPSAjaxObject.AjaxEngine.prototype = 
{
	getXmlHTTPTransport:function()
	{
		if (typeof XMLHttpRequest!="undefined")
		{
			var xmlhttp = new XMLHttpRequest();
            if (xmlhttp.overrideMimeType) 
                xmlhttp.overrideMimeType("text/xml");
			return xmlhttp;
		}
		else if(typeof ActiveXObject != "undefined")
		{
			try
			{
				var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				return xmlhttp;
			}
			catch(e)
			{
				return null;
			}
		}
		return null;
	}
};

// Request Object.
UPSAjaxObject.AjaxRequest = function()
{
	this.reqParams = new Array();
	this.reqHeader = new Array();
	this.reqBody = null;
	this.reqTransport = null;
	this.reqTraget = "";
		
	// Add default request header.
	this.addRequestHeader(new UPSAjaxObject.KeyValuePair("User-Agent", "GreenField UPS"));
};

UPSAjaxObject.AjaxRequest.prototype = 
{
	setRequestTransport:function(obTransport)
	{
		this.reqTransport = obTransport;
	},
	
	getRequestTransport:function(){
		return this.reqTransport;
	},
	
	setTypeGET:function(){
		this.requestType = 'GET';
	},
	
	setTypePOST:function(){
		this.requestType = 'POST';
	},
	
	getRequestType:function(){
		return this.requestType;
	},
	
	setRequestTarget:function(strTargetUrl){
		this.reqTarget = strTargetUrl;
	},
	
	getRequestTarget:function(){
		return this.reqTarget;
	},
	
	setRequestCompleteCallBack:function(callBackFn){
		this.oncomplete = callBackFn;
	},
	
	setRequestLoadedCallBack:function(callBackFn){
		this.onloaded = callBackFn;
	},
	
	addRequestHeader:function(hdr){
		if (!(hdr instanceof UPSAjaxObject.KeyValuePair)){
			alert('Value should be of UPSAjaxObject.KeyValuePair type');
		}
		this.reqHeader.push(hdr);
	},
	
	addRequestParameter:function(param){
		if (!(param instanceof UPSAjaxObject.KeyValuePair)){
			alert('Parameter should be of UPSAjaxObject.KeyValuePair type');
		}
		this.reqParams.push(param);
	},
	
	addRequestContent:function(reqContent){
		this.reqBody = reqContent;
	},
	
	createRequestBody:function(){
		if (this.getRequestType() == 'POST')
		{
			this.reqTransport.setRequestHeader("Content-Type", "text/xml");
			if (null != this.reqBody)
			{
				return this.reqBody;
			}
			
		}
		else
		{
			var iParamCount = this.reqParams.length;
			var idx = 0;
			var strPostBody = new String();
			
			for(idx = 0; idx < iParamCount; idx++)
			{
				if (idx != 0)
				{
					strPostBody += "&";
				}
				var entry = this.reqParams[idx];
				strPostBody += (entry.key + "=" + entry.value);
			}
			
			return strPostBody;
		}
	},
	
	openRequest:function(async){
		if (null == this.reqTransport)
		{
			alert('No request transport specified');
			return;
		}

		this.reqTransport.open(this.getRequestType(), this.getRequestTarget(), async);
	},
	
	sendRequest:function(){
		if (null == this.reqTransport)
		{
			alert('No request transport specified');
			return;
		}

		var reqContent = this.createRequestBody();
		this.reqTransport.send(reqContent);
	},
	
	abortRequest:function(){
		if (null == this.reqTransport)
		{
			return;
		}
		
		this.reqTransport.abort();
	}
};

// Response object.
UPSAjaxObject.AjaxResponse = function(){
	this.status = 200;
	this.statusMsg = "OK";
	this.responseBody = null;
};

UPSAjaxObject.AjaxResponse.prototype = {
	setStatusCode:function(val){
		this.status = val;
	},
	
	getStatusCode:function(){
		return this.status;
	},
	
	setStatusMessage:function(val){
		this.statusMsg = val;
	},
	
	getStatusMessage:function(){
		return this.statusMsg;
	},
	
	getResponseBody:function(){
		return this.responseBody;
	},
	
	parseXmlResponse:function(obResponseNode){
		var responseDocument = UPSXmlEngine.prototype.createXmlDocument("", "");
		responseDocument.setDocument(obResponseNode);
		
		var statusNode = responseDocument.getElementFromParent(responseDocument.getDocument().documentElement, "status");
		if (null != statusNode)
		{
			this.status = responseDocument.getAttributeValue(statusNode, "code");
			this.statusMsg = responseDocument.getAttributeValue(statusNode, "msg");
		}
		
		this.responseBody = responseDocument.getElementFromParent(responseDocument.getDocument().documentElement, "responseBody");
	},
	
	toString:function(){
		return "Status: " + this.getStatusCode() + ", Status Message: " + this.getStatusMessage();
	}
};

//Pam Crome
UPSAjaxObject.GetAutoIdentifier = function(){
	this.Year = "0";
	this.maker = "0";
	this.model = "0";
	this.style = "0";
};

UPSAjaxObject.GetAutoIdentifier.prototype = {
	setYearId:function(val){
		this.year = val;
	},
	
	getYearId:function(){
		return this.year;
	},
	
	setMakerId:function(val){
		this.maker = val;
	},
	
	getMakerId:function(){
		return this.maker;
	},
	setModelId:function(val){
		this.model = val;
	},
	
	getModelId:function(){
		return this.model;
	},
	setStyleId:function(val){
		this.style = val;
	},
	
	getStyleId:function(){
		return this.style;
	},
	
	toString:function(){
		return "year:" + this.getYearId() + ",maker:" + this.getMakerId() + ",model:" + this.getModelId() + ",style:" + getStyleId() ;
	},
	
	toXmlNode:function(xmlDocument){
		
		var xmlNode = xmlDocument.createElementNode("AutoIdentifier", "");
		if (null != xmlNode)
		{
			xmlDocument.addAttributeToNode(xmlNode, "year", this.getYearId());
			xmlDocument.addAttributeToNode(xmlNode, "maker", this.getMakerId());
			xmlDocument.addAttributeToNode(xmlNode, "model", this.getModelId());
			xmlDocument.addAttributeToNode(xmlNode, "style", this.getStyleId());
		}
		return xmlNode;
	}
	
};


//Pam SpecialQuestion
UPSAjaxObject.GetSpecialQIdentifier = function(){
	this.answerID = "0";
	this.childAnswerID = "0";
};

UPSAjaxObject.GetSpecialQIdentifier.prototype = {
	setAnswerId:function(val){
		this.answerID = val;
	},
	
	getAnswerId:function(){
		return this.answerID;
	},
	
	setChildAnswerID:function(val){
		this.childAnswerID = val;
	},
	
	getChildAnswerID:function(){
		return this.childAnswerID;
	},
	toString:function(){
		return "Answer:" + this.getAnswerId() + ",ChildAnswer:" + this.getChildAnswerID();
	},
	
	toXmlNode:function(xmlDocument){
		
		var xmlNode = xmlDocument.createElementNode("SpecialQIdentifier", "");
		if (null != xmlNode)
		{
			xmlDocument.addAttributeToNode(xmlNode, "answer", this.getAnswerId());
			xmlDocument.addAttributeToNode(xmlNode, "childanswer", this.getChildAnswerID());
		}
		return xmlNode;
	}
	
};
//Traffic Save Panelist Answer Value.
UPSAjaxObject.ProfileObject = function(){
	this.ProfileTypeID=0;
	this.PanelistID=0;
	this.QuestionID=0;
	this.AnswerID=0;
	this.AnswerValue="";
};

UPSAjaxObject.ProfileObject.prototype = {
	setProfileTypeId:function(val){
		this.ProfileTypeID=val;
	},
	getProfileTypeId:function(){
		return this.ProfileTypeID;
	},
	setPanelistId:function(val){
		this.PanelistID=val;
	},
	getPanelistId:function(){
		return this.PanelistID;
	},
	setQuestionId:function(val){
		this.QuestionID=val;
	},
	getQuestionId:function(){
		return this.QuestionID;
	},
	
	setAnswerId:function(val){
		this.AnswerID=val;
	},
	getAnswerId:function(){
		return this.AnswerID;
	},
	
	setAnswerValue:function(val){
		this.AnswerValue=val;
	},
	getAnswerValue:function(){
		return this.AnswerValue;
	},
	
	toString:function(){
		return "ProfileTypeID: " + this.getProfileTypeId() + ", PanelistID: " + this.getPanelistId() + ", QuestionID: " + this.getQuestionId() + ", AnswerID: " + this.getAnswerId() + ", AnswerValue: " + this.getAnswerValue();
	},
	
	toXmlNode:function(xmlDocument){
		var xmlNode = xmlDocument.createElementNode("Profile", "");
		if (null != xmlNode)
		{
			xmlDocument.addAttributeToNode(xmlNode, "profiletypeid", this.getProfileTypeId());
			xmlDocument.addAttributeToNode(xmlNode, "panelistid", this.getPanelistId());
			xmlDocument.addAttributeToNode(xmlNode, "questionid", this.getQuestionId());
			xmlDocument.addAttributeToNode(xmlNode, "answerid", this.getAnswerId());
			xmlDocument.addAttributeToNode(xmlNode, "answervalue", this.getAnswerValue());
		}
		return xmlNode;
	}
};
// creating global ajax engine
var g_oAjaxEngine = new UPSAjaxObject.AjaxEngine();


