// JavaScript Document

function resetFields(cleartext) {
	var ffl = "YourName,YourEmail,FriendsName,FriendsEmail,Note";
	ffs = ffl.split(",");
	for (i=0; i < ffs.length;i++) {
		var f = $(ffs[i]);
		if (f) f.style.backgroundColor = 'white';
		if (cleartext) f.value = '';
	}
}

function showProgress() {
	hideOverlay();
	var obj = $('s2afoverlay');
	if (!obj) return false;
	var progress_img = new Image;
	progress_img.src = 'images/thumbprogress.gif';
	var newDiv = document.createElement("div");
	newDiv.style.textAlign = 'center';
	newDiv.style.paddingTop = '65px';
	newDiv.style.width = '432px';
	newDiv.style.height = '105px';
	newDiv.appendChild(progress_img);
	obj.appendChild(newDiv);
	obj.style.display='';
}

function showThanks() {
	hideOverlay();
	var obj = $('s2afoverlay');
	if (!obj) return false;
	var newDiv = document.createElement("div");
	newDiv.style.textAlign = 'center';
	newDiv.style.paddingTop = '65px';
	newDiv.style.width = '432px';
	newDiv.style.height = '105px';
	newDiv.innerHTML = '<b style="font-size: 14px;">Thank You<b><br /> Your message has been sent.<br />&nbsp;<br /><a style="font-size: 11px; color: #6699cc;" href="#" onclick="return hideOverlay();">Send Another</a>';
	obj.appendChild(newDiv);
	obj.style.display='';
}


function showMsg(msgtype) {
	var msgEl = $('s2afmessages');
	if (!msgEl) return true;
	removeChildren(msgEl);
	if (msgtype == 'valerr') {
		msgEl.innerHTML = '<span style="color: red; font-weight: bold; text-align: center;"><b>Error</b></span><br /><span style="color: red; text-align: left;">The fields highlighted in red are in error. Please correct and resubmit.</span>';
	}
	else if (msgtype == 'init') {
		msgEl.innerHTML = "Use the form at left to tell a friend about this item. (Your Email and Friend's Email are required)";
	}
	msgEl.style.display = '';
}

function flagFields(msg) {
	resetFields(false);
	for (i=0; i < msg.length;i++) {
		var x = msg[i].split("^");
		var msgobj = $(x[0]+'_err');
		var obj = $(x[0]);
		if (obj) obj.style.backgroundColor = '#fee0e6';
	}
}

function hideOverlay() {
	var msgEl = $('s2afoverlay');
	if (!msgEl) return true;
	removeChildren(msgEl);
	msgEl.style.display = 'none';
	return false;
}

function parseResponse(rsp) {
	if (rsp && rsp.ERROR == '') {
		hideOverlay();
		if (rsp.CONTENTTYPE == "sub") {
			if (rsp.VALERRORS.length) {
				flagFields(rsp.VALERRORS);
				showMsg('valerr');
			}
			else {
				resetFields(true);
				showMsg('init');
				showThanks();
			}
		}
	} 
	else {
		alert(rsp);
	}
}

function handleError(rsp) {
	alert(rsp);
}

function s2afcreatePayload(act) {
	// YourName
	var obj = $('YourName');
	if (!obj) return false;
	this.yourname = obj.value;
	// YourEmail
	var obj = $('YourEmail');
	if (!obj) return false;
	this.youremail = obj.value;
	// FriendsName
	var obj = $('FriendsName');
	if (!obj) return false;
	this.friendsname = obj.value;
	// FriendsEmail
	var obj = $('FriendsEmail');
	if (!obj) return false;
	this.friendsemail = obj.value;
	// Product Id
	var obj = $('s2afPid');
	if (!obj) return false;
	this.pid = obj.value;
	// Note
	var obj = $('Note');
	if (!obj) return false;
	this.note = obj.value;
	// Content Type
	this.contenttype = "sub";
}

function s2afsendPayload(pld) {
	//alert(JSON.stringify(pld));
	var payload = new Array();
	payload['s2af'] = JSON.stringify(pld);
	var url = 'Send2AFriend.cfm?timeStamp=' + new Date().getTime();
	HTTP.post(url,payload,parseResponse,handleError);
	return true;
}

function s2afSubprepAndSend() {
	var pldVal = new s2afcreatePayload();
	showProgress();
	s2afsendPayload(pldVal);
	return true;
}

function s2afinit() {
	showMsg('init');
}

