/*
PROJECT: mysticseaport.org
SCRIPT NAME: jsrsFunctions.js
PURPOSE: jsrs rpc and callback functions
PROGRAMMER: G Szabo (gszabo@globalcloud.net)
CREATION DATE: 5/4/2004
VERSION HISTORY:
*/

//global vars
delim = "|";
// collectionRoot = "rosenfeld";

function jsrsInit(collection) {
	collectionRoot = collection;
}

// ******** jsrs rpc functions **************

function fcnFetchCatgDesc(obj) {
	var val = new Array();
	theParam = obj.value;
	val[0] = theParam;
	jsrsExecute( '/jsrs/jsrsRPC.cfm', fcnFetchCatgDescCallback, 'fetchCatgDesc', val, 0); 
}

function fcnFetchProduct(obj) {
	var val = new Array();
	theParam = obj.value;
	val[0] = theParam;
	jsrsExecute( '/jsrs/jsrsRPC.cfm', fcnFetchProductCallback, 'fetchProduct', val, 0); 
}

function fcnFetchPrice(obj) {
	var val = new Array();
	theParam = obj.value;
	val[0] = theParam;
	jsrsExecute( '/jsrs/jsrsRPC.cfm', fcnFetchPriceCallback, 'fetchPrice', val, 0); 
}

//called from mysticseaport.js, fcnValidateLogin()
function fcnJSRSValidateLogin(aParams) {
	jsrsExecute( '/jsrs/jsrsRPC.cfm', fcnJSRSValidateLoginCallback, 'validateLogin', aParams, 0);	
}

function fcnJSRSProcessOrder(aParams) {
	jsrsExecute( '/jsrs/jsrsRPC.cfm', fcnJSRSProcessOrderCallback, 'processOrder', aParams, 0);		
}

function fcnJSRSChangeShipType(aParams) {
	jsrsExecute( '/jsrs/jsrsRPC.cfm', fcnJSRSChangeShipTypeCallback, 'changeShipType', aParams, 0);
}

function fcnJSRSAddShopProduct(aParams) {
	jsrsExecute( '/jsrs/jsrsRPC.cfm', fcnJSRSAddShopProductCallback, 'addShopProduct', aParams, 0);		
}

function fcnJSRSReviewBit(intLogId, strSortOn) {
	var val = new Array();
	param1 = intLogId.toString();
	param2 = strSortOn.toString();
	val[0] = param1;
	val[1] = param2;
	jsrsExecute('/jsrs/jsrsRPC.cfm', fcnJSRSReviewBitCallback, 'flipImageLogBit', val, 0);
}

function fcnJSRSCheckEmailAddy(strEmail) {
	var val = new Array();
	val[0] = strEmail.value;
	jsrsExecute('/jsrs/jsrsRPC.cfm', fcnJSRSCheckEmailAddyCallback, 'checkEmailAddy', val, 0);	
}

// ******** jsrs callback functions **************

function fcnFetchCatgDescCallback(optsStr) {
	document.getElementById("divCatgDesc").innerHTML = optsStr;
	document.getElementById("divButton").innerHTML = '<table><tr><td><input type="button" class="submit" name="cancelButton" value="  Cancel  " onclick="fcnBtnCancel();"></td><td> <input type="button" class="submit" name="nextButton" value="  Next >>  " onclick="fcnBtnNext();"></td></tr></table>';
}

function fcnFetchProductCallback(optsStr) {
	var strHTMLArr = new Array();
	var strHTML = "";
	aCallback = optsStr.split(delim); 
	
	if(aCallback[0] == "UNDEFINED") {	
		document.getElementById("divProduct").innerHTML = '<select class="font1"></select>'; 
		document.getElementById("divDescription").innerHTML = '';
		document.getElementById("divPrice").innerHTML = '';
		document.getElementById("divQuantity").innerHTML = '';
		document.getElementById("divButton").innerHTML = '';			
	} else {
		strHTMLArr[0] = '<select name="intProductId" size="1" class="font1" onChange="fcnFetchPrice(this);"><option value="0">Select One</option>';
		for (i=1; i<=aCallback.length; i++) {
	        if(i % 2 == 0) {
			//value of loop is even
				strHTMLArr[i] = aCallback[i-1] + '</option>';
	        }
	        else {
				//value of loop is odd
				strHTMLArr[i] = '<option value="' + aCallback[i-1] + '">';
	   		}
		}	
		strHTMLArr[i++] = '</select>';
		strHTML = strHTMLArr.join(""); 
		document.getElementById("divProduct").innerHTML = strHTML; 
		document.getElementById("divPrice").innerHTML = '';
		document.getElementById("divDescription").innerHTML = '';
		document.getElementById("divQuantity").innerHTML = '';
		document.getElementById("divButton").innerHTML = '';	
	}
}

function fcnFetchPriceCallback(optsStr) {
	var strHTMLArr = new Array();
	var strHTML = "";
	var aCallback = optsStr.split(delim);
	var strDesc = aCallback[2];
	
	if(aCallback[0] == "UNDEFINED") {
		document.getElementById("divPrice").innerHTML = '';
		document.getElementById("divQuantity").innerHTML = '';
		document.getElementById("divButton").innerHTML = '';		
	} else {
		strHTMLArr[0] = 'Member Price: ';
		strHTMLArr[1] = aCallback[0];
		strHTMLArr[2] = '<br>';
		strHTMLArr[3] = 'Non Member Price: ';
		strHTMLArr[4] = aCallback[1];
		strHTML = strHTMLArr.join("");
		document.getElementById("divPrice").innerHTML = strHTML;
		document.getElementById("divDescription").innerHTML = strDesc;	
		document.getElementById("divQuantity").innerHTML = '<input type="Text" size="3" maxlength="3" id="intQuantity" name="intQuantity" value="1" onblur="fcnFixIntByObj(this);" class="font1">';
		document.getElementById("divButton").innerHTML = '<table><tr><td><input type="Submit" class="submit" name="cancelButton" value="  Cancel  "></td><td> <input type="submit" class="submit" name="nextButton" value="  Next >>  "></td></tr></table>';
	}
}

function fcnJSRSValidateLoginCallback(optsStr) {
	var aCallback = optsStr.split(delim);
	
	if(optsStr == 0) {
		alert("Invalid Username or Password.");
	} else {
		collectionRoot = aCallback[1];
		location.href="/index.cfm?fuseaction=" + collectionRoot + ".checkout&step=1";
	}
}

function fcnJSRSProcessOrderCallback(optsStr) {
	var aCallback = optsStr.split(delim);
	var result = aCallback[0];
	var respMsg = aCallback[1];
	
	collectionRoot = aCallback[2];
	
	if(result == 0 || result == 126) {
		location.href = "/index.cfm?fuseaction=" + collectionRoot + ".checkout&step=3";
	} else {
		alert("We're sorry but our payment gateway has reported a problem with your transaction.  The message from the payment gateway is:\n\n" + respMsg + "\n\nAfter clicking OK, you will be directed back to the checkout screen so that you can correct the problem.");
//TODO CH - remove comment
		location.href = "/index.cfm?fuseaction=" + collectionRoot + ".checkout&step=2";
	}
}

function fcnJSRSChangeShipTypeCallback(optsStr) {
	var aCallback = optsStr.split(delim);
	
	collectionRoot = aCallback[2];
	
	location.href="/index.cfm?fuseaction=" + collectionRoot + ".checkout&step=2";
}

function fcnJSRSAddShopProductCallback(optsStr) {
	var aCallback = optsStr.split(delim);
	var rtn = aCallback[0];
	
	collectionRoot = aCallback[1];
	
	if (parseInt(rtn) == 1) {
		location.href = "/index.cfm?fuseaction=" + collectionRoot + ".cart&mode=0";
	} else {
		alert("Failed to add cart item.");
	}
}

function fcnJSRSReviewBitCallback(optsStr) {
	var aCallback = optsStr.split(delim);
	
	if(parseInt(aCallback[0]) == 1) {
		alert("Item Marked as Reviewed.  Log Output will now Refresh.");
		location.href="/admin/index.cfm?fuseaction=verity.imageLog&sortAttribute=" + aCallback[1];
	} else {
		alert("Sorry, Mark Reviewed Failed.  Please try again later.  If this problem persists, please notify Technical Support.");	
	}
}

function fcnJSRSCheckEmailAddyCallback(optsStr) {
	if(parseInt(optsStr) == 1) {
		alert("The email address you have entered is already registered in our system.  Email addresses must be unique.");
		document.getElementById("vchEmail").focus();
	}
}