// field error checking
var aErrors=new Array();
var nErrors = 0;

var HomeReqUrl = "/_request_ajaxxml.php?";

var digits = "0123456789";
var lettersAndNumbersChar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-";
var phoneNumberDelimiters = "";
var minDigitsInPhoneNumber = 10;
var minDigitsInZipNumber = 5;
var minUserNameNumber = 6;

var MasterCard;
var MASTERCARD;
var Amex;
var AMEX;
var Visa;
var VISA;
var Discover;
var DISCOVER;

var REG_USER_PASS;
var REG_USER_PASS2;
var REG_USER_NAME;
var REG_USER_EMAIL;
var REG_USER_LASTNAME;
var REG_USER_FIRSTNAME;
var REG_CHURCH_SIZE;
var REG_USER_CHURCH;
var REG_USER_CVC;
var REG_USER_CHOLDER;
var REG_USER_COUNTRY;
var REG_USER_ADDRESS1;
var REG_USER_ADDRESS2;
var REG_USER_CITY;
var REG_USER_STATE;
var REG_USER_ZIP;
var REG_USER_HOW;
var DENOM;
var REG_REDEEM_CODE;


function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
};


function _set_vars() {
	REG_REDEEM_CODE=document.getElementById('reg_redeem_code');

	MasterCard = "MasterCard";
	MASTERCARD = document.getElementById(MasterCard);
	Amex = "Amex";
	AMEX = document.getElementById(Amex);
	Visa = "Visa";
	VISA = document.getElementById(Visa);
	Discover = "Discover";
	DISCOVER = document.getElementById(Discover);

	REG_USER_PASS=document.getElementById('reg_user_pass');
	REG_USER_PASS2=document.getElementById('reg_user_pass2');
	REG_USER_NAME=document.getElementById('reg_user_name');
	REG_USER_EMAIL=document.getElementById('reg_user_email');
	REG_USER_LASTNAME=document.getElementById('reg_user_lastname');
	REG_USER_FIRSTNAME=document.getElementById('reg_user_firstname');
	REG_CHURCH_SIZE=document.getElementById('reg_church_size');
	REG_USER_CHURCH=document.getElementById('reg_user_church');
	REG_USER_CVC=document.getElementById('reg_user_cvc');
	REG_USER_CHOLDER=document.getElementById('reg_user_cholder');
	REG_USER_COUNTRY=document.getElementById('reg_user_country');
	REG_USER_ADDRESS1=document.getElementById('reg_user_address1');
	REG_USER_ADDRESS2=document.getElementById('reg_user_address2');
	REG_USER_CITY=document.getElementById('reg_user_city');
	REG_USER_STATE=document.getElementById('reg_user_state');
	REG_USER_ZIP=document.getElementById('reg_user_zip');
	REG_USER_HOW=document.getElementById('reg_user_how');
	DENOM=document.getElementById('denom');

}



function verify_password(id1, id2) {
	// need an id1 and id2 from a form.
	// *******************************************************************************
	// Check Password
	// *******************************************************************************

	var invalid = " "; // Invalid character is a space
	var minLength = 6; // Minimum length

	var errorFound = 0;	// want a local variable to keep track of errors... we will return it
					// at the end.


	var id1 = $("#" + id1);
	var id2 = $("#" + id2);
	var pw1 = id1.val();
	var pw2 = id2.val();

	aErrors.length = 0;
	nErrors = 0;

	// check for a value in both fields.
	if (pw1 == '' || pw2 == '') {
		aErrors[++nErrors] = 'Please enter your password, then confirm your password.';
		id1.css('background',"#ff5544");
		id2.css('background',"#ff5544");
		errorFound += 1;
	}

	// check for minimum length
	if (pw1.length < minLength) {
		aErrors[++nErrors] = 'Your password must be at least ' + minLength + ' characters long. Please try again.';
		id1.css('background',"#ff5544");
		id2.css('background',"#ff5544");
		errorFound += 1;
	}
	// check for spaces
	if (pw1.indexOf(invalid) > -1) {
		aErrors[++nErrors] = 'Sorry, spaces are not allowed.';
		id1.css('background',"#ff5544");
		id2.css('background',"#ff5544");
		errorFound += 1;
	}
	else {
	  if (pw1 != pw2) {
		aErrors[++nErrors] = 'You did not enter the same new password twice. Please re-enter your password.';
		id1.css('background',"#ff5544");
		id2.css('background',"#ff5544");
		errorFound += 1;
	  }
	  else {
	    // looks good!
		id1.css('background',"");
		id2.css('background',"");
	  }
	}


	if(aErrors.length > 0) {
		var szErrors = "";
		var szLineNumber = "";
		for(x = 1; x < aErrors.length; x++) {
			if(aErrors.length > 2) {
				szLineNumber = x + ") ";
			}
			szErrors = szErrors + szLineNumber + aErrors[x] + '\n';
		}
		alert(szErrors);
		return false;
	}
	return true;
}


function new_register_submit($form_type) {
	aErrors=new Array();
	nErrors = 0;

	$isRegister = 0;
	$isAddMembership = 0;
	$isTrial = 0;
	$isProfile = 0;

	if($form_type == '') {
		$form_type = "add_membership";
		$isRegister = 1;
	}

	if($form_type == 'add_membership')
		FormName = document.getElementById('submitform_reg');
	else
		FormName = document.getElementById($form_type);

	// set our 'global vars'... these are not getting passed correctly for some reason...
		_set_vars();


	if($form_type == 'form_reg_trial') {
		$isTrial = 1;
		$isRegister = 1;
	}

	if($form_type == 'profile') {
		$isProfile = 1;
	}

	if($form_type == 'add_membership') {
		$isAddMembership = 1;
	}

	var Product_Selected = 0;

	errorMsg = "";
	errorFound = 0;

	if( ($isRegister) | ($isAddMembership) ) {
		var $fieldName;
		var $fieldValue;
		var $call_args;


		$new_args = "";
		for(i=0; i<FormName.elements.length; i++) {
			$fieldName = FormName.elements[i].name;
			//alert($fieldName);
			if($fieldName == 'cb_TOTALMEDIA') {
				OldStyle = 0;
			}
			if($fieldName.substr(0,3) == 'cb_') {
				$fieldValue = FormName.elements[i].checked;
				if($fieldValue = FormName.elements[i].checked == true) {
					Product_Selected = 1;
				}
			} else {
				$fieldValue = FormName.elements[i].value;
			}
			$new_args += ',"' + $fieldName + '":"' + $fieldValue + '"';
		}
		$call_args = $new_args;
		//alert($call_args);

		// *******************************************************************************
		// Make sure a purchase option is selected
		// *******************************************************************************
		if( (Product_Selected < 1) & (!$isTrial) ) {
			aErrors[++nErrors] = 'Please select a membership package.';
		}
	}


	if($isRegister) {
	   // *******************************************************************************
	   // Check Password
	   // *******************************************************************************

	    var invalid = " "; // Invalid character is a space
	    var minLength = 6; // Minimum length
	    var pw1 = REG_USER_PASS.value;
	    var pw2 = REG_USER_PASS2.value;
	    // check for a value in both fields.
	    if (pw1 == '' || pw2 == '') {
			aErrors[++nErrors] = 'Please enter your password, then confirm your password.';
			REG_USER_PASS.style.background = "#ff5544";
			REG_USER_PASS2.style.background = "#ff5544";
			errorFound += 1;
	    }
	    // check for minimum length
	    if (REG_USER_PASS.value.length < minLength) {
			aErrors[++nErrors] = 'Your password must be at least ' + minLength + ' characters long. Please try again.';
			REG_USER_PASS.style.background = "#ff5544";
			REG_USER_PASS2.style.background = "#ff5544";
			errorFound += 1;
	    }
	    // check for spaces
	    if (REG_USER_PASS.value.indexOf(invalid) > -1) {
			aErrors[++nErrors] = 'Sorry, spaces are not allowed.';
			REG_USER_PASS.style.background = "#ff5544";
			REG_USER_PASS2.style.background = "#ff5544";
			errorFound += 1;
	    }
	    else {
		  if (pw1 != pw2) {
			aErrors[++nErrors] = 'You did not enter the same new password twice. Please re-enter your password.';
			REG_USER_PASS.style.background = "#ff5544";
			REG_USER_PASS2.style.background = "#ff5544";
			errorFound += 1;
		  }
		  else {
		    // looks good!
			REG_USER_PASS.style.background = "";
			REG_USER_PASS2.style.background = "";
		  }
	    }
	}



	if($isRegister) {
	   // *******************************************************************************
	   // check username
	   // *******************************************************************************
		var reqUrl;
		var szArgs;
		var UNAME = eval(REG_USER_NAME);
		if( (!UNAME.value)  || (UNAME.value == "Select a Username") ) {
			aErrors[++nErrors] = 'Please enter a valid Username.';
			REG_USER_NAME.style.background = "#ff5544";
			errorFound += 1;
		} else {
			username = UNAME.value;
			usernameLength = username.length;
			if (checkValidChar(username) == false) {
				aErrors[++nErrors] =  'Please enter a valid Username.';
				REG_USER_NAME.style.background = "#ff5544";
				errorFound += 1;
			}  else {
				reqUrl = HomeReqUrl;
				szArgs = "action=username_check&json=1&uname=" + username;
				$.ajax({
					type: "POST",
					async: false,
					url: reqUrl,
					data: szArgs,
					success: function(data){
						//alert(data);
						aMsg = eval("(" +  data + ")");
						if(aMsg['available'] == 'true') {
							// everything looks good, set no errors
							REG_USER_NAME.style.background = "";
						} else  {
							//alert(username);
							//alert(data);
							aErrors[++nErrors] = 'Username you provide is currently in use. Please try another.';
							REG_USER_NAME.style.background = "#ff5544";
							errorFound += 1;
						}
					}
				});
			}
		}
	}

	if($isRegister) {
		// *******************************************************************************
		// Check Email
		// *******************************************************************************

		EML = REG_USER_EMAIL;
		//var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!isValidEmailAddress(EML.value)) {
			aErrors[++nErrors] = 'Please enter a valid email address to continue.';
			REG_USER_EMAIL.style.background = "#ff5544";
			errorFound += 1;
		} else {
			ckemail = EML.value;
			lnemail = EML.length;

			reqUrl = HomeReqUrl;
			szArgs = "action=check_email&json=1&email=" + ckemail;

			$.ajax({
				type: "POST",
				async: false,
				url: reqUrl,
				data: szArgs,
				success: function(data){
					aMsg = eval("(" +  data + ")");
					if(aMsg['available'] == 'true') {
						// everything looks good, set no errors
						REG_USER_NAME.style.background = "";
					} else  {
						aErrors[++nErrors] = 'Your email address has been used with another account, please check and try again.';
						REG_USER_EMAIL.style.background = "#ff5544";
						errorFound += 1;
					}
				}
			});
		}
	}


	if($isRegister) {
		// *******************************************************************************
		// Check Credit Card Type
		// *******************************************************************************
		input1 = eval(MASTERCARD);
		input2 = eval(AMEX);
		input3 = eval(VISA);
		input4 = eval(DISCOVER);
		selected = false;

		//if (!input.value) {
		if (input1.checked)
			selected = true;
		else if (input2.checked)
			selected = true;
		else if (input3.checked)
			selected = true;
		else if (input4.checked)
			selected = true;

		if (selected == false) {
			aErrors[++nErrors] = 'Please select a Credit Card Type.';
			errorFound += 1;
		}
	}


	if($isAddMembership) {
	} else {
		errorFound += field_check(REG_USER_LASTNAME,'Please enter your last name.');
		errorFound += field_check(REG_USER_FIRSTNAME,'Please enter your first name.');

		errorFound += field_check(REG_CHURCH_SIZE,'Please select your church size.');
		errorFound += field_check(DENOM,'Please select a denomination.');
		errorFound += field_check(REG_USER_CHURCH,'Please enter your church or facility name.');

		if($isRegister) {
			errorFound += field_check(REG_USER_CVC,'Please provide the Credit Card Security Code.');
			errorFound += field_check(REG_USER_CHOLDER,'Please provide the proper credit card holder name.');
		}

		errorFound += field_check(REG_USER_ADDRESS1,'Please Provide The Billing Address To Your Credit Card.');
		errorFound += field_check(REG_USER_CITY,'Please Enter Your City.');


		errorFound += field_check(REG_USER_ZIP,'Please Enter Your ZIP/Postal Code.');
		errorFound += field_check(REG_USER_COUNTRY,'Please Select a Country.');
		errorFound += field_check(REG_USER_HOW,'Please Select How You Heard About Us.');


		// only US, CA, and GB require states.
		var my_country = $('#reg_user_country').val();
		switch(my_country) {
			case "US":
			case "GB":
			case "CA":
				errorFound += field_check(REG_USER_STATE,'Please Select/Enter your State.');
				break;
		}
	}


	// if we have errors.... display the message...
	// and return false...
	// returning false prevents the form from firing.
	if(aErrors.length > 0) {
		var szErrors = "";
		var szLineNumber = "";
		for(x = 1; x < aErrors.length; x++) {
			if(aErrors.length > 2) {
				szLineNumber = x + ") ";
			}
			szErrors = szErrors + szLineNumber + aErrors[x] + '\n';
		}
		alert(szErrors);
		return false;
	}


   // *******************************************************************************
   // SUBMIT FORM IF EVERYTHING IS OK
   // *******************************************************************************
	FormName.submit();
	return false;
}


	/* update text box via js */
	function set_txt_val(obj,text) {
	// get the object id for the select box
		var myselect=document.getElementById(obj);

		if(!myselect) return false;
		if(text != '')
			myselect.value=text;
	return true;
}

	/* update radio selections via js */
	function set_radio_val(obj,text) {
	// get the object id for the select box
		var myselect;
            var mylist = obj.split(",");

		for(var $x=0;$x<mylist.length;$x++) {
			if(mylist[$x] == text) {
				myselect = document.getElementById(mylist[$x]);
				if(myselect)
					myselect.checked = 1;
			}
		}
	return true;
}

	/* update select box via js */
	function set_selectbox_selection(select_box,choice) {
		// get the object id for the select box
			var myselect=document.getElementById(select_box);

			if(!myselect) return false;
		// loop through the options...
			if(myselect.options) {
				for (var i=0; i<myselect.options.length; i++){
					if(myselect.options[i].value == choice) {
						myselect.options[i].selected=true;
						break;
					}
				}
			}
		return true;
	}


	function isGreater(one,two) {
		if(one > two) return true;
		return false;
	}

	function isNumeric(data){
		return parseFloat(data)==data;
	}



	//
	// FormTotal
	// 	should = the price of all products.
	//	sub-Total = FromTotal
	//	paint the Sub-Total
	//
	// remove any discount if supplied
	// 	GrossTotal = sub-Total - discount
	//	paint the discount
	//
	// apply flexpay stuff.
	//	(GrossTotal / SplitCount) + SplitFee
	//	paint the total
	//

	function display_total() {
		var DiscountUsed = 0;
		var GrossFormTotal = 0;
		var sub_total;
		var sync_rate;
		var base_rate;
		var rc = -1;
		var rc2;


		// need todays date for stuff below...
		var now = new Date();
		var today = now.getDay() + "/" + now.getMonth() + "/" + now.getFullYear();

		// verify coupon
			if(SavedCoupon == '') {
			} else {
				CouponOK = 0;
				rc = simple_coupon_verify('submitform_reg');
				if(CouponOK == 0) {
					update_coupon('reset',SavedCoupon);
				}
			}

		// handle the discount.
			if(isGreater(FormTotal,Discount)) {
				// no negative discount
				GrossFormTotal = FormTotal - Discount;
				DiscountUsed = Discount;
			} else {
				// negative discount.
				DiscountUsed = FormTotal;
				GrossFormTotal = 0;
			}

			Formatted_Discount = "(" + parseFloat(DiscountUsed).toFixed(2) + ")";
			$('#discount_amount').html(Formatted_Discount);


		$('#hidden_form_amount').val(GrossFormTotal);
		$('#hidden_form_fee').val(SplitFee);
		$('#hidden_form_splitcount').val(SplitCount);

		sub_total = FormTotal;

		if(SplitCount == '1') {
			$('#selected_tot').html('$' + GrossFormTotal.toFixed(2));

			var split_sync_charge = $('#hidden_sync_charge').val() / SplitCount;
			var split_base_charge = $('#hidden_base_charge').val() / SplitCount;

			$('#id_STILLS_display').html(split_sync_charge.toFixed(2))
			$('#id_TOTALMEDIA_display').html(split_base_charge.toFixed(2))

			$('#total_text_line').html('Total');
		} else {
			//(GrossFormTotal);
			//alert(SplitCount);

			GrossFormTotal = GrossFormTotal / SplitCount;
			sub_total = FormTotal / SplitCount;


			var split_sync_charge = $('#hidden_sync_charge').val() / SplitCount;
			var split_base_charge = $('#hidden_base_charge').val() / SplitCount;
			var split_disc_charge = (DiscountUsed / SplitCount);



			$('#id_STILLS_display').html(split_sync_charge.toFixed(2))
			$('#id_TOTALMEDIA_display').html(split_base_charge.toFixed(2))

			$('#discount_amount').html("(" + parseFloat(split_disc_charge).toFixed(2) + ")");

			GrossFormTotal += SplitFee;

			if(SplitCount == '12')
				$('#total_text_line').html('12 Monthly Payments of&nbsp;&nbsp;');
			else
			if(SplitCount == '4')
				$('#total_text_line').html('4 Quarterly Payments of&nbsp;&nbsp;');
			else
			if(SplitCount == '2')
				$('#total_text_line').html('2 Semi-Annual Payments of&nbsp;&nbsp;');
			else
				$('#total_text_line').html('Total');

			$('#selected_tot').html("<strong>" + "$" + GrossFormTotal.toFixed(2) + "</strong>");
		}

		if(isGreater(FormTotal,0)) {
			$('#sub_total').html(sub_total.toFixed(2));
			$('.tr_SUBTOTAL').show();
		} else {
			$('#sub_total').html('0.00');
			$('.tr_SUBTOTAL').hide();
		}

		if(FormTotal == '0') {
			$('#dateExpires').hide();
		} else {
			/*
			if(myStartDate == '')
				expText = "Expires: " + myEndDate;
			else
			*/
			var OldExpText =  myStartDate;
			var NewExpText =  '<strong>&nbsp;&nbsp;&nbsp;'+ myEndDate + '</strong>';


			if(OldExpText == '') {
				$('#OlddateExpires').html('');
				$('#NewdateExpires').html('');
			} else {
				$('#OlddateExpires').html('Current Expiration: ');
				$('#OlddateExpires').append(OldExpText);

				$('#NewdateExpires').html('<strong>New Expiration: </strong>');
				$('#NewdateExpires').append(NewExpText);
			}

			$('#ExpirationDates').show();
		}

		// show values
			if(isGreater(DiscountUsed,0)) {
				$('.tr_DISCOUNT').show();
			} else {
				$('.tr_DISCOUNT').hide();
			}
	}


	function simple_coupon_verify(formName) {
		//alert("coupon Verify");
		// see if a coupon is valid...
		// return true IF good... otherwise return false.
		var FormName = document.getElementById(formName);
		var RC = 0;

		var $fieldName;
		var $fieldValue;


		// set our 'global vars'... these are not getting passed correctly for some reason...
			_set_vars();

		var COUPON = REG_REDEEM_CODE;

		var Product_Selected = 0;

		if (!COUPON.value) {
			RC = 0;
			alert("no coupon value");
			return RC;
		}

		couponCode = COUPON.value;
		couponCodeLength = couponCode.length;

		$new_args = "";
		for(i=0; i<FormName.elements.length; i++) {
			$fieldName = FormName.elements[i].name;
			if($fieldName.substr(0,3) == 'cb_') {
				$fieldValue = FormName.elements[i].checked;
				if($fieldValue = FormName.elements[i].checked == true) {
					Product_Selected = 1;
				}
			} else {
				$fieldValue = FormName.elements[i].value;
			}

			$new_args += ',"' + $fieldName + '":"' + $fieldValue + '"';
		}
		$new_args = '{"dummy":"dummy"' + $new_args + '}';
		$call_args = $new_args;

		if(Product_Selected == 1) {
			$call_function = HomeReqUrl;
			$.ajax({
			   type: "POST",
			   async: false,
			   url: $call_function,
			   data: "action=redeem_coupon&json=1&DATA=" + $call_args,
			   success: function(data){
				   //alert(data);
							if(data.substr(0,1) == "{") {
								var aMsg = eval("(" + data + ")");
								if(aMsg['msg'] == null) {
									RC = 1;
									CouponOK = RC;
									//alert("Coupon is FINE.");
									return CouponOK; // coupon is valid... we return true;
								}
							}
						}
			 });
		} else {
			RC = 0;
			return RC;
		}
		if(RC == 0)
		return RC;
	}


	function update_coupon(amount,coupon_name) {
		// set our 'global vars'... these are not getting passed correctly for some reason...
			_set_vars();

		Discount = amount;

		if(amount == 'check') {
			alert(CouponOK);
		} else if(amount == 'reset') {
			alert("removing coupon");
			$('.tr_DISCOUNT').hide();
			Discount = 0;
			SavedCoupon = "";
			$('#reg_coupon').val('');
			return display_total();
		} else {

			if(coupon_name == "") {
				alert("name is empty... not updating...");
				return false;
			}


			//("reducing by: " + amount );
			if(isNumeric(amount)) {
				SavedCoupon = coupon_name;
				return display_total();
			} else {
				alert("Error: Discount " + Discount + " is not numeric.");
			}
		}
	}


	function simple_coupon_redeem(formName) {
		//alert(formName);
		var FormName = document.getElementById(formName);
		var $fieldName;
		var $fieldValue;

		// set our 'global vars'... these are not getting passed correctly for some reason...
			_set_vars();

		var COUPON = REG_REDEEM_CODE;

		var Product_Selected = 0;

		if($('.tr_DISCOUNT').is(':visible')){
			if (!COUPON.value) {
				// user has a coupon active... but wants to remvove it...
				update_coupon('reset','');
			} else {
				alert ('Coupon has been entered... Only one discount is allowed per transaction.');
			}
			return false;
		}
		else
		if (!COUPON.value) {
			alert ('The coupon appears to be blank. If you do not have a coupon code, simply skip this field.');
			return false;
		} else {
			couponCode = COUPON.value;
			couponCodeLength = couponCode.length;

			$new_args = "";
			for(i=0; i<FormName.elements.length; i++) {
				$fieldName = FormName.elements[i].name;
				if($fieldName.substr(0,3) == 'cb_') {
					$fieldValue = FormName.elements[i].checked;
					if($fieldValue = FormName.elements[i].checked == true) {
						Product_Selected = 1;
					}
				} else {
					$fieldValue = FormName.elements[i].value;
				}

				$new_args += ',"' + $fieldName + '":"' + $fieldValue + '"';
			}
			$new_args = '{"dummy":"dummy"' + $new_args + '}';
			$call_args = $new_args;

			if(Product_Selected == 1) {
				//alert($call_args);
				$call_function = HomeReqUrl;
				//alert(HomeReqUrl);

				$.ajax({
				   type: "POST",
				   async: false,
				   url: HomeReqUrl,
				   data: "action=redeem_coupon&json=1&DATA=" + $call_args,
				   success: function(data){
					   //alert(data);
						if(data.substr(0,1) == "{") {
							var aMsg = eval("(" + data + ")");
							if(aMsg['msg'] == null) {
								//alert("coupon data follows");
								//alert(aMsg['discount']);
								//alert(aMsg['coupon_used']);
								//$aMsg['coupon_used'] = COUPON.value;
								//alert("coupon data follows");
								//alert(aMsg['discount']);
								//alert(aMsg['coupon_used']);
								//alert(COUPON.value);
								update_coupon(aMsg['discount'],COUPON.value);
								//update_coupon(aMsg['discount'],aMsg['coupon_used']);
							} else {
								alert(aMsg['msg']);
							}
						} else {
							alert(data);
						}
					}
				});
			} else {
				alert ('No product has been selected. Please select a product.');
				return false;
			}
			return false;
		}
	}


/*
 * <span id="reg_user_state_span">
 * <select name="reg_user_state" id="reg_user_state">
 * <option value="">-- US State --</option>
 * <option value="AL">Alabama</option>
 * <option value="AK">Alaska</option>
 * <option value="WI">Wisconsin</option>
 * <option value="WY">Wyoming</option>
 * </select>
 * </span>
 */
 // move from notlogin...
function createStateMenu(pid, sid, ssid) {
	PID = document.getElementById(pid);
	SID = document.getElementById(sid);
	SSID = document.getElementById(ssid);

	value = PID.value;
	// go ahead and make the check request
	var reqUrl = "/aj/";
	var szdata = "dummy=dummy/function:get_states/country:" + value;

	$.ajax({
	   type: "GET",
	   async: false,
	   url: reqUrl,
	   data: szdata,
	   success: function(data){
			$('#reg_user_state_div').html(data);
		}
	});
	return false;
}

	function createcountry() {
		var reqUrl = "/aj/";
		var szdata = "dummy=dummy/function:get_countries";

		$.ajax({
		   type: "GET",
		   async: false,
		   url: reqUrl,
		   data: szdata,
		   processData: false,
		   success: function(data){
				$('#country_select').html(data);
				$('#reg_user_country').attr('size',15);
			}
		});
		return false;
	}

	function refreshcountry() {
	    pid = "reg_user_country";
	    PID = document.getElementById(pid);
	    sid = "reg_user_state";
	    ssid = "reg_user_state_span";
	    var input = eval(PID);


		$('#reg_user_country').attr('size',1);

	    if (!input.value) {
		  return false;
	    }
	    else {
		  createStateMenu(pid, sid, ssid);
		  return true;
	    }
	}


	function show_payment_options(whycalled) {





		/// Draw Up Bookmark Modul
		///////////////////////////////////////
		$("body").prepend("<div id='PaymentsModul'></div>");
		$('#PaymentsModul').fadeIn("slow");
		$('body').prepend("<div id='PaymentsMenu'></div>");
		//$('#PaymentsMenu').css('height','500px').css('background','none').css('border',1);
		//$('#PaymentsMenu').show();
		//$close_link = "<a  href=\"\" onclick=\"return close_payment_options()\">Closeme</a>";

		var reqUrl = "/aj/payment_options.php";
		var szArgs;

		if(whycalled == 'overview')
			szArgs = "FormTotal=0";
		else
		if(whycalled == 'sync_charge')
			szArgs = "FormTotal=sync_charge";
		else
			szArgs = "FormTotal=" + FormTotal + "&SplitFee=" + SplitFee;

		$.ajax({
			type: "POST",
			async: false,
			url: reqUrl,
			data: szArgs,
			success: function(data){
				$('#PaymentsMenu').html(data);
			}
		});

		return false;
	}




	function close_payment_options() {
		$('#PaymentsModul').remove().fadeOut ("slow");
		$('#PaymentsMenu').remove();
		return false;
	}




	//
	// handles user payment selection.
	// 12 months, 4 quarters, etc.
	// popus up the I Agree box as needed.
	//
	function select_monthly_option(monthly) {
		$('#split_pay_2').hide();
		$('#split_pay_4').hide();
		$('#split_pay_12').hide();

		if(monthly) {
			var mySelectedIndex;
			mySelectedIndex = $('#payment_select option:selected').val();
			$('#radio2').attr("checked","checked");
			$('#split_pay_' + mySelectedIndex).show();
			$('.split_pay').show();
			$('#i_agree_button').hide();
			$('#i_agree_checkbox').attr('checked', false);
		} else {
			$('.split_pay').hide();
			$('#i_agree_button').show();
		}
		return false;
	}


	//
	// when the 'Proceed' button on the payment is selected,
	// we process it here.
	//
	function choose_payment_option() {
		var rc;

		var mySelectedIndex;
		var myRadioButton1;
		var myRadioButton2;

		var my_sync_fee;
		var my_base_fee;

		// record what was selected.
			myRadioButton1 = $('#radio1').attr("checked");
			myRadioButton2 = $('#radio2').attr("checked");
			mySelectedIndex = $('#payment_select option:selected').val();

		// close the window... we are done with it.
			rc = close_payment_options();

		$('.tr_SPLITFEE').hide();
		if(myRadioButton2) {
			// split payments
				SplitFee = 5;
				SplitCount = mySelectedIndex;
				$('#hidden_form_splitcount').val(SplitCount);
				$('.tr_SPLITFEE').show();
				$('#splitfee_amount').html(SplitFee.toFixed(2));
				/*
				my_sync_fee = parseFloat($('#hidden_sync_charge').val()) / SplitCount;
				my_base_fee = parseFloat($('#hidden_base_charge').val()) / SplitCount;


				$('#id_STILLS_amt').html(my_base_fee.toFixed(2));
				$('#id_MOTIONS_amt').html(my_base_fee.toFixed(2));
				$('#id_TOTALMEDIA_amt').html(my_base_fee.toFixed(2));

				$('#id_STILLS_display').html(my_sync_fee.toFixed(2));
				$('#id_MOTIONS_display').html(my_sync_fee.toFixed(2));
				$('#id_TOTALMEDIA_display').html(my_base_fee.toFixed(2));
				*/
		} else {
			// single payment...
				SplitFee = 0;
				SplitCount = 1;
				$('#hidden_form_splitcount').val(1);
				/*
				my_base_fee = parseFloat($('#hidden_base_charge').val());
				$('#id_TOTALMEDIA_display').html(my_base_fee.toFixed(2));
				*/
				$('#splitfee_amount').html(SplitFee.toFixed(2));
		}

		// redisplay totals using new info.
			display_total()
		return false;
	}

	// show / hide a div based on a click.
	// used to show hide credit card info with:
	// show_hide('tableCreditCard','textCreditCard','Credit Cards')
	function show_hide(div_id,text_id,text) {
		$('#' + div_id).toggle();
		if ($('#' + div_id).is(":hidden")) {
			$('#' + text_id).html('Show ' + text);
		} else {
			//$('#' + text_id).html('Hide ' + text);
			$('#' + text_id).hide();
		}
		return false;
	}

	function isInteger(s){
	    var i;
	    for (i = 0; i < s.length; i++) {
		  // Check that current character is number.
		  var c = s.charAt(i);
		  if (((c < "0") || (c > "9")))
			return false;
	    }
	    // All characters are numbers.
	    return true;
	}

	function stripCharsInBag(s, bag){
	    var i;
	    var returnString = "";
	    // Search through string's characters one by one.
	    // If character is not in bag, append to returnString.
	    for (i = 0; i < s.length; i++) {
		  // Check that current character isn't whitespace.
		  var c = s.charAt(i);
		  if (bag.indexOf(c) == -1)
			returnString += c;
	    }
	    return returnString;
	}

	function checkValidChar(strChar){
	    s = stripCharsInBag(strChar, phoneNumberDelimiters);
	    return (s.length >= minUserNameNumber);
	}

	function checkPhone(strPhone){
	    s = stripCharsInBag(strPhone, phoneNumberDelimiters);
	    return (isInteger(s) && s.length >= minDigitsInPhoneNumber);
	}


	function checkZip(strZip){
	    s = stripCharsInBag(strZip, phoneNumberDelimiters);
	    return (isInteger(s) && s.length >= minDigitsInZipNumber);
	}

	function field_check($Field,$fail_text) {
	   //alert($fail_text);
	   var errorFound = 0
	   if (!$Field.value) {
		 $Field.style.background = "#ff5544";
		 aErrors[++nErrors] = $fail_text;
		 errorFound = 1
	   } else {
		 $Field.style.background = "";
	   }
	   return errorFound;
	}

	function change_password() {
		var RC;
		var msg;

		oldPass = $('#reg_user_pass').val();
		var user_id
		user_id = $('#id_user_id').val();

		if(oldPass.length < 1) {
			alert("Old password not specified");
		}
		else
		if (oldPass == 'Old Password') {
			alert("Old password not specified");
		}
		else
		{
			RC = verify_password('reg_user_pass_ch1','reg_user_pass_ch2');
			if(RC) {
				newPass = $('#reg_user_pass_ch1').val();
				msg = "Need to change password from " + oldPass + " to " + newPass;
				//form_div;

				var reqUrl = "/aj/";
				var szdata = "dummy=dummy/function:change_password/user_id:" + user_id + "/oldPass:" + oldPass + "/newPass:" + newPass;
				$.ajax({
				   type: "GET",
				   async: false,
				   url: reqUrl,
				   data: szdata,
				   success: function(data){
						$aData = eval('(' + data + ')');
						if($aData['RC'] == 0)
							alert("Invalid Password");
						else {
							alert("Password Changed");
						}
					}
				});

			}
		}
		return false;
	}

/*
	$('#change_password').submit(function() {
			change_password();
			return false;
		}
	);
*/

