
function submitOmniFlexSurvey0() {
	//query database for id and retrieve survey data
	$.post("omniflex_survey.php", {
		action: 'retrieve',
		surveyID: $("#omniFlexSurvey0 [name=surveyId]").val()
	}, function(r) {
		//if row found fill in survey with retrieved data
		if(r.success) {
			surveyID = $("#omniFlexSurvey0 [name=surveyId]").val();
			surveyData = r.data;
			$("#omniFlexSurvey1 :input").each(function() {
				var inputName = $(this).attr("name");
				if(inputName) {
					if(r.data[inputName]) {
						$(this).val(r.data[inputName]);
					}
				}
			});
			$("#omniFlexSurvey2 :input").each(function() {
				var inputName = $(this).attr("name");
				if(inputName) {
					if(r.data[inputName]) {
						$(this).val(r.data[inputName]);
					}
				}
			});
			$("#omniFlexSurvey3 :input").each(function() {
				var inputName = $(this).attr("name");
				if(inputName) {
					if(r.data[inputName]) {
						$(this).val(r.data[inputName]);
					}
				}
			});
			loadPrevMed();
			loadCurrentMed();
			problemChange();
			//goto survey page 1
			$("#omniFlexSurvey0").hide();
			$("#omniFlexSurvey1").show();
			window.scroll(0,200);
		} else {
			$("#omniFlexSurvey0 .survey-error").text(r.error);
			$("#omniFlexSurvey0 .survey-error").slideDown();
			setTimeout(function() {
				$("#omniFlexSurvey0 .survey-error").slideUp();
			}, 3000);
		}
	}, "json");
}

function loadPrevMed() {
	var medArray = $("#previousMedication").val().split(";");
	for(var i in medArray) {
		if(i == 0) {
			var newDiv = $("<div style=\"padding: 1px 0;\"><input type=\"text\" /> <a href=\"#\" onclick=\"addPrevMed(); return false;\"><img src=\"images/plus.gif\" alt=\"Add Another\" /></a></div>");
			$(newDiv).find("input").val(medArray[i]);
			$("#previousMedicationDiv").html(newDiv);
		} else {
			var newDiv = $("<div style=\"padding: 1px 0;\"><input type=\"text\" /> <a href=\"#\" onclick=\"addPrevMed(); return false;\"><img src=\"images/plus.gif\" alt=\"Add Another\" /></a> <a href=\"#\" onclick=\"removePrevMed(this); return false;\"><img src=\"images/delete.gif\" alt=\"Remove\" /></a></div>");
			$(newDiv).find("input").val(medArray[i]);
			$("#previousMedicationDiv").append(newDiv);
		}
	}
}

function savePrevMed() {
	var medArray = [];
	$("#previousMedicationDiv :input").each(function() {
		var med = $(this).val();
		if(med) {
			medArray.push(med);
		}
	});
	$("#previousMedication").val(medArray.join(";"));
}

function addPrevMed() {
	var newDiv = $("<div style=\"padding: 1px 0;\"><input type=\"text\" /> <a href=\"#\" onclick=\"addPrevMed(); return false;\"><img src=\"images/plus.gif\" alt=\"Add Another\" /></a> <a href=\"#\" onclick=\"removePrevMed(this); return false;\"><img src=\"images/delete.gif\" alt=\"Remove\" /></a></div>");
	$("#previousMedicationDiv").append(newDiv);
}

function removePrevMed(ref) {
	$(ref).parent().remove();
}

function loadCurrentMed() {
	var medArray = $("#currentMedication").val().split(";");
	for(var i in medArray) {
		if(i == 0) {
			var newDiv = $("<div style=\"padding: 1px 0;\"><input type=\"text\" /> <a href=\"#\" onclick=\"addCurrentMed(); return false;\"><img src=\"images/plus.gif\" alt=\"Add Another\" /></a></div>");
			$(newDiv).find("input").val(medArray[i]);
			$("#currentMedicationDiv").html(newDiv);
		} else {
			var newDiv = $("<div style=\"padding: 1px 0;\"><input type=\"text\" /> <a href=\"#\" onclick=\"addCurrentMed(); return false;\"><img src=\"images/plus.gif\" alt=\"Add Another\" /></a> <a href=\"#\" onclick=\"removeCurrentMed(this); return false;\"><img src=\"images/delete.gif\" alt=\"Remove\" /></a></div>");
			$(newDiv).find("input").val(medArray[i]);
			$("#currentMedicationDiv").append(newDiv);
		}
	}
}

function saveCurrentMed() {
	var medArray = [];
	$("#currentMedicationDiv :input").each(function() {
		var med = $(this).val();
		if(med) {
			medArray.push(med);
		}
	});
	$("#currentMedication").val(medArray.join(";"));
}

function addCurrentMed() {
	var newDiv = $("<div style=\"padding: 1px 0;\"><input type=\"text\" /> <a href=\"#\" onclick=\"addCurrentMed(); return false;\"><img src=\"images/plus.gif\" alt=\"Add Another\" /></a> <a href=\"#\" onclick=\"removeCurrentMed(this); return false;\"><img src=\"images/delete.gif\" alt=\"Remove\" /></a></div>");
	$("#currentMedicationDiv").append(newDiv);
}

function removeCurrentMed(ref) {
	$(ref).parent().remove();
}

function problemChange() {
	if($("#primaryProblem").val() == "Other") {
		$("#specificProblemDiv").slideDown();
	} else {
		$("#specificProblemDiv").hide();
		$("#specificProblem").val("");
	}
}

function saveOmniFlexSurvey1() {
	savePrevMed();
	saveCurrentMed();
	var inputArray = [];
	$("#omniFlexSurvey1 :input").each(function() {
		var inputName = $(this).attr("name");
		var inputValue = $(this).val();
		if(inputName) {
			inputArray.push(inputName+"%d2%"+inputValue);
		}
	});
	inputString = inputArray.join("%d1%");
	
	$.post("omniflex_survey.php", {
		action: 'save',
		surveyID: surveyID,
		data: inputString
	}, function(r) {
		if(r.success) {
			$("#omniFlexSurvey1 .saveButton").val("Saved!");
			setTimeout(function() {
				$("#omniFlexSurvey1 .saveButton").val("Save and Continue...");
			}, 3000);
		} else {
			$("#omniFlexSurvey1 .survey-error").text(r.error);
			$("#omniFlexSurvey1 .survey-error").slideDown();
			setTimeout(function() {
				$("#omniFlexSurvey1 .survey-error").slideUp();
			}, 3000);
		}
	}, "json");
}

function submitOmniFlexSurvey1() {
	savePrevMed();
	saveCurrentMed();
	var inputArray = [];
	$("#omniFlexSurvey1 :input").each(function() {
		var inputName = $(this).attr("name");
		var inputValue = $(this).val();
		if(inputName) {
			inputArray.push(inputName+"%d2%"+inputValue);
		}
	});
	inputString = inputArray.join("%d1%");
	
	$.post("omniflex_survey.php", {
		action: 'save',
		surveyID: surveyID,
		data: inputString
	}, function(r) {
		if(r.success) {
			$("#omniFlexSurvey1").hide();
			$("#omniFlexSurvey2").show();
			window.scroll(0,200);
		} else {
			$("#omniFlexSurvey1 .survey-error").text(r.error);
			$("#omniFlexSurvey1 .survey-error").slideDown();
			setTimeout(function() {
				$("#omniFlexSurvey1 .survey-error").slideUp();
			}, 3000);
		}
	}, "json");
}

function expand(ref) {
	if($(ref).parent().parent().find(".body").css("display") == "none") {
		$("#omniFlexSurvey2 .body").slideUp();
		$("#omniFlexSurvey2 .title img").attr("src", "images/arrow1.gif");
		var scrollInt = setInterval(function() {
			var currentTop = findTop($(ref).parent().get(0));
			window.scroll(0,currentTop-25);
		}, 10);
		setTimeout(function() {
			clearInterval(scrollInt);
		}, 500);
		$(ref).parent().parent().find(".body").slideDown();
		$(ref).parent().find("img").attr("src", "images/arrow2.gif");
	}
}

function saveOmniFlexSurvey2() {
	var inputArray = [];
	$("#omniFlexSurvey2 :input").each(function() {
		var inputName = $(this).attr("name");
		var inputValue = $(this).val();
		if(inputName) {
			inputArray.push(inputName+"%d2%"+inputValue);
		}
	});
	inputString = inputArray.join("%d1%");
	
	$.post("omniflex_survey.php", {
		action: 'save',
		surveyID: surveyID,
		data: inputString
	}, function(r) {
		if(r.success) {
			$("#omniFlexSurvey2 .saveButton").val("Saved!");
			setTimeout(function() {
				$("#omniFlexSurvey2 .saveButton").val("Save and Continue...");
			}, 3000);
		} else {
			$("#omniFlexSurvey2 .save-error").text(r.error);
			$("#omniFlexSurvey2 .save-error").slideDown();
			setTimeout(function() {
				$("#omniFlexSurvey2 .save-error").slideUp();
			}, 3000);
		}
	}, "json");
}

function previousOmniFlexSurvey2() {
	var inputArray = [];
	$("#omniFlexSurvey2 :input").each(function() {
		var inputName = $(this).attr("name");
		var inputValue = $(this).val();
		if(inputName) {
			inputArray.push(inputName+"%d2%"+inputValue);
		}
	});
	inputString = inputArray.join("%d1%");
	
	$.post("omniflex_survey.php", {
		action: 'save',
		surveyID: surveyID,
		data: inputString
	}, function(r) {
		if(r.success) {
			$("#omniFlexSurvey2").hide();
			$("#omniFlexSurvey1").show();
			window.scroll(0,200);
		} else {
			$("#omniFlexSurvey2 .survey-error").text(r.error);
			$("#omniFlexSurvey2 .survey-error").slideDown();
			setTimeout(function() {
				$("#omniFlexSurvey2 .survey-error").slideUp();
			}, 3000);
		}
	}, "json");
}

function submitOmniFlexSurvey2() {
	var inputArray = [];
	$("#omniFlexSurvey2 :input").each(function() {
		var inputName = $(this).attr("name");
		var inputValue = $(this).val();
		if(inputName) {
			inputArray.push(inputName+"%d2%"+inputValue);
		}
	});
	inputString = inputArray.join("%d1%");
	
	$.post("omniflex_survey.php", {
		action: 'save',
		surveyID: surveyID,
		data: inputString
	}, function(r) {
		if(r.success) {
			$("#omniFlexSurvey2").hide();
			$("#omniFlexSurvey3").show();
			window.scroll(0,200);
		} else {
			$("#omniFlexSurvey2 .survey-error").text(r.error);
			$("#omniFlexSurvey2 .survey-error").slideDown();
			setTimeout(function() {
				$("#omniFlexSurvey2 .survey-error").slideUp();
			}, 3000);
		}
	}, "json");
}

function saveOmniFlexSurvey3() {
	var inputArray = [];
	$("#omniFlexSurvey3 :input").each(function() {
		var inputName = $(this).attr("name");
		var inputValue = $(this).val();
		if(inputName) {
			inputArray.push(inputName+"%d2%"+inputValue);
		}
	});
	inputString = inputArray.join("%d1%");
	
	$.post("omniflex_survey.php", {
		action: 'save',
		surveyID: surveyID,
		data: inputString
	}, function(r) {
		if(r.success) {
			$("#omniFlexSurvey3 .saveButton").val("Saved!");
			setTimeout(function() {
				$("#omniFlexSurvey3 .saveButton").val("Save and Continue...");
			}, 3000);
		} else {
			$("#omniFlexSurvey3 .survey-error").text(r.error);
			$("#omniFlexSurvey3 .survey-error").slideDown();
			setTimeout(function() {
				$("#omniFlexSurvey3 .survey-error").slideUp();
			}, 3000);
		}
	}, "json");
}

function previousOmniFlexSurvey3() {
	var inputArray = [];
	$("#omniFlexSurvey3 :input").each(function() {
		var inputName = $(this).attr("name");
		var inputValue = $(this).val();
		if(inputName) {
			inputArray.push(inputName+"%d2%"+inputValue);
		}
	});
	inputString = inputArray.join("%d1%");
	
	$.post("omniflex_survey.php", {
		action: 'save',
		surveyID: surveyID,
		data: inputString
	}, function(r) {
		if(r.success) {
			$("#omniFlexSurvey3").hide();
			$("#omniFlexSurvey2").show();
			window.scroll(0,200);
		} else {
			$("#omniFlexSurvey3 .survey-error").text(r.error);
			$("#omniFlexSurvey3 .survey-error").slideDown();
			setTimeout(function() {
				$("#omniFlexSurvey3 .survey-error").slideUp();
			}, 3000);
		}
	}, "json");
}

function submitOmniFlexSurvey3() {
	var inputArray = [];
	$("#omniFlexSurvey3 :input").each(function() {
		var inputName = $(this).attr("name");
		var inputValue = $(this).val();
		if(inputName) {
			inputArray.push(inputName+"%d2%"+inputValue);
		}
	});
	inputArray.push("complete%d2%1");
	inputString = inputArray.join("%d1%");
	
	$.post("omniflex_survey.php", {
		action: 'complete',
		surveyID: surveyID,
		data: inputString
	}, function(r) {
		if(r.success) {
			$("#omniFlexSurvey3").hide();
			$("#omniFlexSurvey4 #coupon-code").text(r.coupon);
			$("#omniFlexSurvey4 #coupon-expire").text(r.expire);
			$("#omniFlexSurvey4").show();
			window.scroll(0,200);
		} else {
			$("#omniFlexSurvey3 .survey-error").text(r.error);
			$("#omniFlexSurvey3 .survey-error").slideDown();
			setTimeout(function() {
				$("#omniFlexSurvey3 .survey-error").slideUp();
			}, 3000);
		}
	}, "json");
}

function findTop(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curtop += obj.offsetTop
		}
	}
	return curtop;
}
