if ( !window.console || !window.console.log ) { window.console = { log:function(msg){ /*alert(msg)*/ } }; }

var pop;
function openWin( url, width, height, scrollbars ) {
	var props = "";
	scrollbars = (scrollbars)?("yes"):("no");
	if ( width && height ) {
		props = "width="+width+",height="+height+",scrollbars="+scrollbars+",menu=no";
	}
	pop = window.open( url, "holypop", props );
}

/**
 * 
 */
function getSwf( name ) {
	if ( $.browser["msie"] ) { 
		return window[name];
	} else {
		return document[name];
	}
}

function submitConsultation(form)
{
	if ( $("#certify-check").get(0).checked || $("#certify-check").get(0).checked == "true" ) {
		return true;
	}
	alert("You must agree to the TERMS OF USE");
	return false;
}

/**
 * 
 */
function validateForm(form) 
{
	var inputs = $(form).find(":input[type=text]");
	for ( i=0;i<inputs.length;i++ ) {
		if ( $(inputs[i]).val() == "" ) {
			alert("All fields are required");
			return false;
		}
	}
	return true;
}

function validateRegisterForm(form) 
{
	var inputs = $(form).find(":input[type=text]");
	for ( i=0;i<inputs.length;i++ ) {
		if ( $(inputs[i]).val() == "" ) {
			alert("All fields are required");
			return false;
		}
	}

	inputs = $(form).find(":input[type=password]");
	for ( i=0;i<inputs.length;i++ ) {
		if ( $(inputs[i]).val() == "" ) {
			alert("All fields are required");
			return false;
		}
	}

	if ( !$("#readTerms").get(0).checked ) {
		alert("You must agree to the terms of use");
		return false;
	}

	if ( !$("#over18").get(0).checked ) {
		alert("You must be 18 years or older");
		return false;
	}

	
	if ( form.password.value != form.password2.value ) {
		alert("The passwords do not match. Please retype");
		return false;
	}

	return true;
}

/**
 * 
 */
function validatePersonalInfoForm(form) 
{
	var inputs;
	$(form).find(".error").removeClass("error");
	$("input:not(.not-required)").each( function() { 
		/*$(form).find(":input[type=text]").each( function() {*/
			if ( $(this).val() == "" ) {
				$(this).addClass("error");
			}
		});

	$(form).find("select").each( function() {
			if ( $(this).val() == "" ) {
				$(this).addClass("error");
			}
		});

	// date, zip and phone must all be numerals
	$(form).find(".number-only").each( function() {
			var m = $(this).val().match( /[^0-9]/ );
			if ( m && m.length > 0 ) {
				$(this).addClass("error");
			}
		});
	
	if ( $(form).find(".error").length > 0 ) {
		alert("There are some errors with your input. Please verify");
		return false;		
	}

	return true;
}


function login( form ) 
{
	var inputs = $(form).find(":input[type=text]");
	for ( i=0;i<inputs.length;i++ ) {
		if ( $(inputs[i]).val() == "" ) {
			alert("All fields are required");
			return false;
		}
	}

	inputs = $(form).find(":input[type=password]");
	for ( i=0;i<inputs.length;i++ ) {
		if ( $(inputs[i]).val() == "" ) {
			alert("All fields are required");
			return false;
		}
	}
	return true;
}


function rejectPhotos()
{
	// get reason
	var reason = $(":textarea [name=reason]").val();
	$(":input[name=action]").val("reject");
	$("form").submit();
}

function assignConsultation()
{
	// get doctor
	var doctor = $(":select [name=doctor]").val();
        if ( !doctor || doctor.length <= 0 ) {
	    alert("Please select a doctor from the list");
	    return;
	}
	$(":input[name=action]").val("approve");
	$("form").submit();
}

function validateMedicalInformation() 
{
	var missing = false;
	$(".question-row:not(.not-required)").each( function() {
		// if has a radio, get length of the selector with one checked
		$(this).css("background-color","transparent");
		if ( $(this).find(":input[type=radio]").length > 0 ) {
			if ( $(this).find(":input[checked=true]").length == 0 ) {
				$(this).css("background-color","#ffdede");
				missing = true;
			}
		}
	});
	return !missing;
}


function initDiagnosisForm() {
	$("#diagnosis-form select").change( function() {
		handleDiagnosisChange();
	});
}

function handleDiagnosisChange() 
{
	var text = "";
	var form = $("#diagnosis-form");

	var classification = form.find(":select[name=classification]").val();
	if ( classification != "null" ) {
		// get diagnosis opening
		text += openings[classification];
		text += "\n\n";
	}


	// get oral medicine,get oral dosage
	if ( form.find(":select[name=oral-anti] :selected").val() != "null" && 
		 form.find(":select[name=oral-anti-indication] :selected").val() != "null" ) {
		
		var tmp = oralTmpl;
		tmp = tmp.split("#DRUG#")[0] + form.find(":select[name=oral-anti] :selected").text() + tmp.split("#DRUG#")[1];
		tmp = tmp.split("#DOSAGE#")[0] + form.find(":select[name=oral-anti-indication] :selected").text() + tmp.split("#DOSAGE#")[1];

		text+= tmp + "\n\n";
	}

	// get topical medicine,get topical dosage
	if ( form.find(":select[name=topical] :selected").val() != "null" && 
		 form.find(":select[name=topical-indication] :selected").val() != "null" ) {
		
		var tmp = topicalTmpl;
		tmp = tmp.split("#DRUG#")[0] + form.find(":select[name=topical] :selected").text() + tmp.split("#DRUG#")[1];
		tmp = tmp.split("#DOSAGE#")[0] + form.find(":select[name=topical-indication] :selected").text() + tmp.split("#DOSAGE#")[1];

		text+= tmp + "\n\n";
	}

	// get topical medicine2 ,get topical dosage2
	if ( form.find(":select[name=topical2] :selected").val() != "null" && 
		 form.find(":select[name=topical-indication2] :selected").val() != "null" ) {

		var tmp = topicalTmpl;
		tmp = tmp.split("#DRUG#")[0] + form.find(":select[name=topical2] :selected").text() + tmp.split("#DRUG#")[1];
		tmp = tmp.split("#DOSAGE#")[0] + form.find(":select[name=topical-indication2] :selected").text() + tmp.split("#DOSAGE#")[1];

		text+= tmp + "\n\n";
	}

	// get topical medicine2 ,get topical dosage2
	if ( form.find(":select[name=cleaner] :selected").val() != "null" && 
		 form.find(":select[name=cleaner-indication] :selected").val() != "null" ) {

		var tmp = cleanserTmpl;
		tmp = tmp.split("#DRUG#")[0] + form.find(":select[name=cleaner] :selected").text() + tmp.split("#DRUG#")[1];
		tmp = tmp.split("#DOSAGE#")[0] + form.find(":select[name=cleaner-indication] :selected").text() + tmp.split("#DOSAGE#")[1];

		text+= tmp + "\n\n";
	}

	if ( classification != "null" ) {
		// get diagnosis closing
		text += closings[classification];
		text += "\n\n";

		// get diagnosis reminders
		text += reminders[classification];
		text += "\n\n";
	}

	form.find("textarea[name=diagnosis-comments]").val( text );
}

