function checkSave(form){

	if (checkFields(form)){
	
		var jsonObj = new Object;
		jsonObj.cmd = form.cmd.value;
		jsonObj.amount = form.amount.value;
		jsonObj.id = 0;
		if (form.cmd.value == '_donations'){
			jsonObj.first = form.first_name.value;
			jsonObj.last = form.last_name.value;
			jsonObj.company = form.company.value;
			jsonObj.email = form.email.value;
			jsonObj.phone = form.phone.value;
			jsonObj.honor = form.honor.value;
			jsonObj.recurring = '';
			for (var i=0; i<form.recurring.length; i++){
				if (form.recurring[i].checked){
					jsonObj.recurring = form.recurring[i].value;
				}
			}
			jsonObj.apply = new Array;
			
			for (var i=0; i<form.apply.length; i++){
				jsonObj.apply[i] = new Object;
				jsonObj.apply[i].name = form.apply[i].value;
				jsonObj.apply[i].checked = form.apply[i].checked;
			}
		}
		
		if (form.cmd.value == '_xclick'){
			jsonObj.event = form.item_name.options[form.item_name.selectedIndex].value;
			
			jsonObj.type1 = form.quantity_1.options[form.quantity_1.selectedIndex].value;
			
			try{
			
			jsonObj.type2 = form.quantity_2.options[form.quantity_2.selectedIndex].value;
			jsonObj.type3 = form.quantity_3.options[form.quantity_3.selectedIndex].value;
			}
			catch(e){}
		}
		
		var jsonStr = JSON.stringify(jsonObj);
		
   		url = '/php/save-form.php?j=' + encodeURIComponent(jsonStr);
		xmlGet(url, function(){ saveForm(form);});
	}
	
	return false
}

function saveForm(form){
	if (req.readyState == 4){
		if (req.status == 200){
		
			var jsonStr = req.responseText;
			var jsonObj = JSON.parse(jsonStr);
		
			if (jsonObj.id > 0){
				// good to go
				form.custom.value = jsonObj.id;
				form.submit();
			}else{
				alert("A problem occured while setting up your transaction, please try again.");
			}
		}
	}
}

function showPopUp(){

	txtObj = document.getElementById('popup-text');
	txtObj.innerHTML = 'Search For A Weather.com Location Id';
	
	questionObj = document.getElementById('question-container');
	
	while (questionObj.hasChildNodes()){
		questionObj.removeChild(questionObj.lastChild);
	}
	
	elm = document.createElement('li');
	elm.className = 'as-label';
	elm.innerHTML = 'Location:';
	questionObj.appendChild(elm);
	
	elm = document.createElement('li');
	elm.className = 'as-field';
	elm.innerHTML = '<input type="text" name="loc" value="' + document.dataform.lName.value + '"><br><button type="button" onclick="checkLocation()"><img src="/admin/images_admin/black-dot.png">&nbsp;Search</button>';
	questionObj.appendChild(elm);
	
	elm = document.createElement('li');
	elm.className = 'as-submit';
	elm.innerHTML = '<button type="button" onclick="checkLocation()"><img src="/admin/images_admin/black-dot.png">&nbsp;Search</button>';
	//questionObj.appendChild(elm);
	
	showWindow('fade-container', 'popup-container');

}

function checkLocation(){

	form = document.popupform;

	rc = document.getElementById('response-container');
	rc.style.display = 'none';

	l = document.getElementById('loading');
	l.style.display = 'block';
    
   	url = '/admin/location-lookup.php?l=' + encodeURIComponent(form.loc.value);
	xmlGet(url, function(){ locationLookup();});

}

function locationLookup(){
	if (req.readyState == 4){
		if (req.status == 200){
		
			l = document.getElementById('loading');
			l.style.display = 'none';
			
			rc = document.getElementById('response-container');
			while (rc.hasChildNodes()){
				rc.removeChild(rc.lastChild);
			}
			
			rc.style.display = 'block';
			
			var locObj = eval('(' + req.responseText + ')');
			
			if (locObj.length == 0){
				// no locations found
				var elm = document.createElement('li');
				elm.className = 'as-submit';
				elm.innerHTML = 'No Locations Found.';
				rc.appendChild(elm);
			}else{
				for (var i=0; i<locObj.length; i++){
					var loc = locObj[i];
					var elm = document.createElement('li');
					elm.className = 'as-label';
					elm.innerHTML = loc.name;
					rc.appendChild(elm);
					
					elm = document.createElement('li');
					elm.className = 'as-content';
					elm.innerHTML = '<a href="#" onclick="useId(\'' + loc.id + '\')">' + loc.id + '</a>';
					rc.appendChild(elm);
				}
			}
		}
	}
}

function useId(id){

	document.dataform.lLocId.value = id;
	hideWindow();

}

function applyCoupon(btn){

	btn.style.cursor = 'wait';

	form = btn.form;

   	url = '/store/apply-coupon.cgi?o=' + encodeURIComponent(form.oId.value) + '&c=' + encodeURIComponent(form.code.value);
	xmlGet(url, function(){ checkCoupon(btn);});

}

function checkCoupon(btn){
	if (req.readyState == 4){
		if (req.status == 200){
			btn.style.cursor = 'auto';
			form = btn.form;
			if (req.responseText == "FAIL"){
				document.getElementById('coupon-header').innerHTML = '<strong>The code you entered (' + form.code.value + ') is not valid</strong>';
			}else{
				window.location = 'http://' + window.location.hostname + '/store/summary.cgi';
			}
		}
	}
}

function checkOrg(btn){

	form = btn.form;
	
	if ((form.oId.value == "") || (isNaN(form.oId.value))){
		return checkFields(form, 'N');
	}
	
	btn.style.cursor = 'wait';
       
   	url = '/store/validate-organization-code.cgi?o=' + encodeURIComponent(form.oId.value);
	xmlGet(url, function(){ checkSignUp(btn);});

}

function checkSignUp(btn){
	if (req.readyState == 4){
		if (req.status == 200){
			
			btn.style.cursor = 'auto';
			checkFields(form, req.responseText); // this function is included in a separate file.
		}
	}
}

function login(btn){

	form = btn.form;
	
	if (form.sId.value.length != 6){
		alert("Please enter your six digit participant code.");
		form.sId.focus();
		return false;
	}
	
	btn.style.cursor = 'wait';
	obj = document.getElementById('login-popup');
	obj.style.cursor = 'wait';
	
	url = '/participant-login/login.cgi?u=' + encodeURIComponent(form.sId.value) + '&p=' + encodeURIComponent(form.password.value);
	xmlGet(url, function(){ checkLogin(btn);});
}

function checkLogin(btn){
	if (req.readyState == 4){
		if (req.status == 200){
			
			btn.style.cursor = 'auto';
			
			obj = document.getElementById('login-popup');
			obj.style.cursor = 'auto';
			
			if (req.responseText == 'FAIL'){
				txt_fld = document.getElementById('login-txt');
				txt_fld.innerHTML = '<strong>The code and password you provided is not valid.  Please try again.</strong>';
			}else{
				// success
				document.cookie="participant=" +escape(req.responseText);
				window.location = 'http://' + window.location.hostname + '/participant-login/index.cgi';
			}
        }
    }
}

function emailPassword(btn){

	form = btn.form;

    btn.style.cursor = 'wait';
    
	obj = document.getElementById('login-popup');
	obj.style.cursor = 'wait';
	

	url = '/participant-login/forgot-password.cgi?e=' + encodeURIComponent(form.email.value);
	xmlGet(url, function(){ emailSent(btn);});

}

function emailSent(btn){
	if (req.readyState == 4){
		if (req.status == 200){
			
			btn.style.cursor = 'auto';
			
			obj = document.getElementById('login-popup');
			obj.style.cursor = 'auto';
			
			txt_fld = document.getElementById('password-txt');
			if (req.responseText == 'SENT'){
				switchWindow('password-sent-popup');
				txt_fld.innerHTML = '<strong>Forgot Password?</strong> Please enter your email address to have it sent to you.';
			}else{
				txt_fld.innerHTML = '<strong>Your e-mail address was not found.</strong>';
			}
		}
    }
}



