function SLW_SIGNUP_FORM(formType/*int*/, emailFormContainerId/*str*/, fullFormContainerId/*str*/, 
							emailSubmitImage/*str*/, fullFormSubmitImage/*str*/, debugOn/*bool*/, testOn/*bool*/) {

	/*form type*/
	var FT_FULL 	= 1
	var FT_EMAIL	= 2;
	
	/*PHP FILE TO PROCESS REQUESTS*/
	var AJAX_LOAD_URL		= "/common/SLWSF/SLWSFLoad.php";
	var AJAX_VALIDATE_URL 	= "/common/SLWSF/SLWSFValidate.php";
	var AJAX_SUBMIT_URL 	= "/common/SLWSF/SLWSFSubmit.php";

	//set passed in variables
	var _initialFormType		= formType;
	var _currFormType			= formType;
	var _emailFormContainerId	= emailFormContainerId;
	var _emailFormContainer		= null;
	var _fullFormContainer		= null;
	var _fullFormContainerId	= fullFormContainerId;
	var _emailSubmitImage		= emailSubmitImage;
	var _fullFormSubmitImage	= fullFormSubmitImage;
	var _debugOn				= debugOn;
	var _testOn					= testOn;
	
	/*status div*/
	var _statusDivId 	= "__slwsf_status";
	var _statusDiv		= null;
	var _statusDivCSS	= "slwsf_status-box";
	/*submit div*/
	var _submitDivId	= "__slwsf_submit";
	var _submitDiv		= null;
	var _submitDivCSS	= "slwsf_submit-box";
	var _submitDivImg	= "/common/SLWSF/work.gif"; //temporarily hold the image name, then we're going to switch to image object
	/*debug textarea*/
	var _debugTAreaId	= "__slwsf_debug";
	var _debugTArea		= null;
	/*loading div*/
	var _loadingDivId	= "__slwsf_loading";
	var _loadingDiv		= null;
	var _loadingDivCSS	= "slwsf_loading-box";
	var _loadingDivImg	= "/common/SLWSF/load.gif"; //temporarily hold the image name, then we're going to switch to image object
		
	/*private variables*/
	var _selectedID 	= '';
	var _validating 	= false;
	var _stopscript		= false;
	var _interval 		= null;
	var _ajaxObj		= new AJAX();
	var _loadedFormDef	= new Array();
	var _newsid			= 0;
	var _customOnSubmit	= '';;

	//for email only form
	var _emailFormDef = new Array("__slwsf_email_address_o:r:Email Address");
	//for full form
	var _fullFormDef = new Array("__slwsf_gender:r:Title", "__slwsf_first_name:r:First Name", "__slwsf_last_name:r:Last Name", "__slwsf_address:r:Address", 
								"__slwsf_post_code:r:Post Code", "__slwsf_city:r:City", "__slwsf_prname:r:Province", "__slwsf_province::",
								"__slwsf_area1:r:Home Phone Area Code", "__slwsf_prefix1:r:Home Phone Exchange", "__slwsf_suffix1:r:Home Phone Suffix", 
								"__slwsf_area2::Cell Phone Area Code", "__slwsf_prefix2::Cell Phone Exchange", "__slwsf_suffix2::Cell Phone Suffix", 
								"__slwsf_month:r:Birth Month", "__slwsf_day:r:Birth Day", "__slwsf_year:r:Birth Year", "__slwsf_email_address:r:Email Address");

	this.__SLWSF_initialize			= _initialize;
	this.__SLWSF_setSelectedID		= _setSelectedID;
	this.__SLWSF_validateData		= _runAJX_ValidateField;
	this.__SLWSF_submitForm			= _submitForm;
	this.__SLWSF_setCustomOnSubmit	= _setCustomOnSubmit;
	this.__SLWSF_resetForm			= _resetForm;

	function _initialize() {
		var src = _submitDivImg;
		_submitDivImg = new Image();
		_submitDivImg.src = src;
		
		var src = _loadingDivImg;
		_loadingDivImg = new Image();
		_loadingDivImg.src = src;

		if (_emailFormContainerId) {
			_emailFormContainer = getEID(_emailFormContainerId);
			_emailFormContainer.innerHTML = '';
		}
		if (_fullFormContainerId) { 
			_fullFormContainer		= getEID(_fullFormContainerId);
			_fullFormContainer.innerHTML = '';
		}
		
		_loadFormDef();
		
		//create status display
		_statusDiv = createDIV(_statusDivId, _statusDivCSS, "");
		document.body.appendChild(_statusDiv);
		
		//create debug text box
		if (_debugOn) {
			var debugDiv = createDIV("", "", "");
			document.body.appendChild(debugDiv);
			debugDiv.innerHTML = "<textarea id=\"" + _debugTAreaId + "\" style=\"width:100%;height:500px\"></textarea>";
			_debugTArea = getEID(_debugTAreaId);
		}

		_runAJX_GetHTMLForm(_currFormType, '');
	};	
	
	function _setCustomOnSubmit(customFnc) {
		_customOnSubmit = customFnc;
	}
	
	function _loadFormDef() {
		var fieldDefList = _currFormType == FT_FULL ? _fullFormDef : _emailFormDef;
		
		for (var i=0;i<fieldDefList.length;i++) {
			var fieldDef = fieldDefList[i].split(':');

			_loadedFormDef[fieldDef[0]] 			= new Object();
			_loadedFormDef[fieldDef[0]].id 			= fieldDef[0];
			_loadedFormDef[fieldDef[0]].caption 	= fieldDef[2];
			_loadedFormDef[fieldDef[0]].prev_value	= '';
			_loadedFormDef[fieldDef[0]].required 	= fieldDef[1]=='r';
		}
	}

	function _runAJX_GetHTMLForm(formType, prevFormType) {
		var action = formType == FT_FULL ? "load_fullform" : "load_emailform";
		var ajaxForm = 'session=' + session + '&action=' + action;

		_showLoading();
		
		_debugDump('http://' + document.domain + AJAX_LOAD_URL + '?' + ajaxForm);
		_ajaxObj.getData(AJAX_LOAD_URL, ajaxForm, _proAJX_LoadHTMLForm, prevFormType);
	}

	function _proAJX_LoadHTMLForm(form, prevFormType) {
		_hideLoading();
		
		if (_currFormType == FT_EMAIL) {
			form = form.replace("#SUBMIT_IMG#", _emailSubmitImage);
			_emailFormContainer.innerHTML = form;
		}
		else {
			if (prevFormType == FT_EMAIL) {
				var ea = getEID(_selectedID).value;
			}

			form = form.replace("#SUBMIT_IMG#", _fullFormSubmitImage);
			_fullFormContainer.innerHTML = form;
			
			if (prevFormType == FT_EMAIL) {
				var email = getEID('__slwsf_email_address');
				email.className = "input-text-readonly";
				email.value		= ea;
				email.readOnly  = true;
			}
			
			if (_fullFormSubmitImage == '') {
				var currButton 		= getEID('__slwsf_submit_full');
				var buttonParent	= currButton.parentNode;
				var newButton 		= document.createElement("input");
				
				newButton.setAttribute("type", "button");
				newButton.onclick = currButton.onclick;
				newButton.value = "Submit";
				newButton.style.marginTop = '10px';
				newButton.id = newButton.name = "__slwsf_submit_full";

				buttonParent.removeChild(currButton);
				buttonParent.appendChild(newButton);
				currButton = null;
			}
		}
	}	

	function _setSelectedID(id) {
		_selectedID = id;
	};

	function _showStatus(message, parentObj) {
		var currtop = 16;

		_statusDiv.innerHTML = message;

		_statusDiv.style.left = setLeft(parentObj) + _statusDiv.offsetWidth + 'px';
		if (parentObj.offsetWidth > 80) _statusDiv.style.width = parentObj.offsetWidth + 'px';
		else _statusDiv.style.width = '200px';

		if (parentObj.offsetParent) {
			do {
				currtop += parentObj.offsetTop;
			} while (parentObj = parentObj.offsetParent);
		}

		_statusDiv.style.top = currtop+'px';
		
		_statusDiv.style.display = 'block';
	}

	function _hideStatus() {
		_statusDiv.style.display = "none";
	}			
	
	function _showLoading() {
		_loadingDiv = createDIV(_loadingDivId, _loadingDivCSS, '');
		_loadingDiv.appendChild(_loadingDivImg);
		_loadingDiv.style.display = "block";

		if (_currFormType == FT_EMAIL) {
			_emailFormContainer.appendChild(_loadingDiv);
		}
		else {
			_fullFormContainer.appendChild(_loadingDiv);
		}
	}
	
	function _hideLoading() {
		_loadingDiv.style.display = "none";
		if (_currFormType == FT_EMAIL) {
			_emailFormContainer.removeChild(_loadingDiv);
		}
		else {
			_fullFormContainer.removeChild(_loadingDiv);
		}
		_loadingDiv = null;
	}
	
	function _runAJX_ValidateField(action) {
		var ajaxFnc 	= null;
		var ajaxForm 	= '';
		var input		= getEID(_selectedID);
		var run 		= true;
		var fieldDef	= _loadedFormDef[_selectedID];

		if (!_validating) { 
			if (input.value.trim()) {

				ajaxFnc = action == "validate_postal" ? _proAJX_PostCodeField : _proAJX_Field;  

				run = (input.value != fieldDef.prev_value); 

				var ajaxForm = 	'session=' + session + '&action=' + action + '&data=' + input.value;

				if (run) {
					_showStatus("Validating " + fieldDef.caption, input);
					_validating = true;
					_stopscript = false;
					_debugDump('http://' + document.domain + AJAX_VALIDATE_URL + '?' + ajaxForm);
					_ajaxObj.getData(AJAX_VALIDATE_URL, ajaxForm, ajaxFnc, _selectedID);
				}		
			}
		} 
	}		

	function _proAJX_Field(result, lastID) {
		var lastInput 		= getEID(lastID);
		var lastFieldDef	= _loadedFormDef[lastID];
		
		_validating = false;
		_hideStatus();

		if (result == 'BAD') {
			_stopscript = true;
			alert("Value entered for " + lastFieldDef.caption + " is invalid!");
				
			lastInput.value = '';
			lastInput.focus();
		}
		else {
			_stopscript = false;
		}

		_debugDump(result);
		lastFieldDef.prev_value = lastInput.value;
	}				

	function _proAJX_PostCodeField(result, lastID) {
		var lastInput 		= getEID(lastID);
		var lastFieldDef	= _loadedFormDef[lastID];
		var postalData		= result.parseJSON();

		_validating = false;
		_hideStatus();

		if (postalData) {
			getEID('__slwsf_post_code').value	= postalData.post_code;
			getEID('__slwsf_city').value 		= postalData.city;
			getEID('__slwsf_province').value 	= postalData.abbreviation;
			getEID('__slwsf_prname').value 		= postalData.province;
			getEID('__slwsf_area1').value 		= postalData.area_code;
			getEID('__slwsf_prefix1').focus();
		}
		else {
			alert("Value entered for Postal Code is invalid!");
			getEID('__slwsf_post_code').value 	= '';
			getEID('__slwsf_city').value 		= '';
			getEID('__slwsf_province').value 	= '';
			getEID('__slwsf_prname').value 		= '';
			getEID('__slwsf_area1').value 		= '';

			getEID('__slwsf_post_code').focus();
			_stopscript = true;
		}
		
		_debugDump(result);
		lastFieldDef.prev_value = lastInput.value;
	}

	function _submitForm(type) {
		clearTimeout(_interval);
		if (!_validating && !_stopscript) _runAJX_submitForm();
		else {
			_interval = setTimeout("slwsf.__SLWSF_submitForm('" + type + "')", 100);
		}
	}
	
	function _runAJX_submitForm() {
		var input 		= null;
		var value 		= '';
		var jsonStr	 	= '';
		var ajaxForm	= '';
		var action		= '';
		var button		= null;

		for (field in _loadedFormDef) {
			value = '';
			field = _loadedFormDef[field];
			input = getEID(field.id);

			if (input) {
				if (input.type == 'text' || input.type == 'select-one' || input.type == 'hidden') {
					value = input.value;
					if (field.required && !value) {
						alert('Missing value for ' + field.caption + '!');
						input.focus();
						return(false);;
					}
				}
				else if (input.type == 'radio') {
					input = document.getElementsByName(field.id);
					var checked = false;

					for (j=0;j<input.length;j++) {
						checked = !(!input[j].checked && !checked);
						if (checked) {
							value = input[j].value
							break;
						}
					}

					if (field.required && !checked) {
						alert('Missing value for ' + field.caption + '!');
						return(false);;
					}
				}

				jsonStr += field.id.replace('__slwsf_', '') + ':' + value.toJSONString() + ',';
			}
		}

		if (_currFormType == FT_FULL) {
			button = getEID("__slwsf_submit_full");
			button.style.display = "none";
			_submitDiv = createDIV(_submitDivId, _submitDivCSS, '');
			_submitDiv.appendChild(_submitDivImg);
			button.parentNode.appendChild(_submitDiv);
		}

		action = (_currFormType == FT_FULL) ? "submit_fullform" : "submit_emailform";
		ajaxForm = 'session=' + session + '&action=' + action + '&data={' + jsonStr.substring(0, jsonStr.length-1) + '}' + '&sid=' + _newsid;
		_debugDump('http://' + document.domain + AJAX_SUBMIT_URL + '?' + ajaxForm);
		_ajaxObj.getData(AJAX_SUBMIT_URL, ajaxForm, _proAJX_submitForm, _selectedID);
	}		

	function _proAJX_submitForm(jsonData) {
		var result = jsonData.parseJSON()
		
		if (_currFormType == FT_EMAIL) {
			if (result.action == "continue") {
				if (result.pixel == 1) {
					_firePixel();
				}
				if (_customOnSubmit) _customOnSubmit();
				//switch to full form
				_newsid	= result.sid;
				_currFormType = FT_FULL
				_loadFormDef();
				_runAJX_GetHTMLForm(FT_FULL, FT_EMAIL);
			}
			else {
				_redirectURL(result.url);
			}
		}
		else _redirectURL(result.url);
	}
	
	function _firePixel() {
		var ifrm = document.createElement("IFRAME");
		var debug = (_debugOn || _testOn);
		ifrm.setAttribute("src", "/common/SLWSF/SLWSFPixel.php?session=" + session+'&debug='+_debugOn);
		
		var border = (_debugOn || _testOn ? 1 : 0) + 'px';
		
		ifrm.setAttribute("frameborder", border);
		
		ifrm.style.width 	= (_debugOn || _testOn ? 350 : 1) + 'px';
		ifrm.style.height	= (_debugOn || _testOn ? 70 : 1) + 'px';
		
		document.body.appendChild(ifrm);
	}
	
	function _redirectURL(url) {
		_debugDump(url);
		if (!_debugOn) document.location = url;
	}
	
	function _resetForm() {
		if (_initialFormType == FT_EMAIL) {
			_fullFormContainer.innerHTML = '';
			_currFormType = FT_EMAIL;
			_loadFormDef();		
		}
	}
	
	function _debugDump(text) {
		if (_debugOn) _debugTArea.innerHTML += text + chr(13) + chr(13);
	}
};
