Months = new Array(12);
Months[0] = "January";
Months[1] = "February";
Months[2] = "March";
Months[3] = "April";
Months[4] = "May";
Months[5] = "June";
Months[6] = "July";
Months[7] = "August";
Months[8] = "September";
Months[9] = "October";
Months[10] = "November";
Months[11] = "December";

DayNames = new Array(7);
DayNames[1] = "Monday";
DayNames[2] = "Tuesday";
DayNames[3] = "Wednesday";
DayNames[4] = "Thursday";
DayNames[5] = "Friday";
DayNames[6] = "Saturday";
DayNames[7] = "Sunday";

dojo.require("dojo.parser");
dojo.require("dijit.form.Button");
dojo.require("dijit.Dialog");
dojo.require("dijit.form.TextBox");

function displayHCPVerificationDialog( destination ) {
	document.getElementById("destination").value = destination;
	if ( document.getElementById("displayDialog").value == "yes" ) {
		dijit.byId('hcpVerificationDialog').show();
	} else {
		location.href = destination;
	}
}

function submit( action ) {

	// if user clicks no to HCP question, rewrite destination to be 
	// sorry page

	if ( action == "nonhcp" ) {
		document.getElementById("destination").value = "../hcp/hcp_sorry.jsp";
	}
				
	var destination = document.getElementById("destination").value;

	var oForm = document.forms["frmHCPVerification"];
	var oAction = oForm.elements["action"];
	
	// override action if wanting to go somewhere other than
	// search form or sorry page
	if ( destination.indexOf( "hcp_sitefeedback.jsp" ) > -1 ) {
		action = "hcpsitefeedback";
	} else if ( destination.indexOf( "hcp_medicalquestion.jsp" ) > -1 ) {
		action = "mir";
	}
	
	oAction.value = action;
	oForm.submit();
}

//
// Author:
// Description:	Format date into date string in MON-DD-YYYY format
//
// Param:		date object
// Return:		formatted date string
//
function FormatDate(DateIn) {
	return Months[DateIn.getMonth()] + " " + DateIn.getDate() + ", " + DateIn.getFullYear();
}

//
// Author:
// Description:	Creates current date string in MON-DD-YYYY format
//
// Return:		formatted current date
//
function FormatDateNow() {
	var TempDate = new Date();
    return FormatDate(TempDate);
}

//
// Author:		Andrzej Adamczyk
// Description:	Removes empty spaces from the beginning of the string
//
// Param:		string
// Return:		left trimmed string or null
//
function LTrim(str) {
	if (str == null) return null;

	var length = str.length;
	var pos = 0;

	for (var i = 0; i < length; i++) {
		pos = i;

		if (str.charAt(i) != ' ' && str.charAt(i) != '\t') {
			break;
		}
	}

	return str.substring(pos, length);
}

//
// Author:		Andrzej Adamczyk
// Description:	Removes empty spaces from the end of the strings
//
// Param:		string
// Return:		right trimmed string or null
//
function RTrim(str) {
	if (str == null) return null;

	var pos = str.length;

	for (var i = str.length - 1; i >= 0; i--) {
		if (str.charAt(i) != ' ' && str.charAt(i) != '\t') {
			break;
		}

		pos = i;
	}

	return str.substring(0, pos);
}

//
// Author:		Andrzej Adamczyk
// Description:	Removes empty spaces from the beginning and end of the string
//
// Param:		string
// Return:		trimmed string or null
//
function Trim(str) {
	if (str == null) return null;

	return LTrim(RTrim(str));
}

//
// Author:		Andrzej Adamczyk
// Description:	Displays leave message when user leaves Medinfo Portal
//
function leave() {
	alert("You are now leaving Medical Information Portal page.");
}

//
// Author:		Andrzej Adamczyk
// Description:	Check date taking into account leap years
//
// Param:		day
//				month
//				year
// Return:		error message
//
function checkDate(day, month, year) {
	var errMsg = "";

	if ((year % 4 == 0) && !((year % 100 == 0) && !(year % 400 == 0))) {
		if (month == 2 && day > 29) {
			errMsg = Months[month - 1] + " has only 29 days in " + year + ".\n";
		}
	} else {
		if (month == 2 && day > 28) {
			errMsg = Months[month - 1] + " has only 28 days in " + year + ".\n";
		}
	}

	if (((month == 4) || (month == 6) || (month == 9) || (month == 11)) && (day > 30)) {
		errMsg += Months[month - 1] + " has only 30 days.\n";
	} else if (day > 31) {
		errMsg += Months[month - 1] + " has only 31 days.\n";
	}

	return errMsg;
}

//
// Author:		Andrzej Adamczyk
// Description: Validate if the date string has appropriate format. Accepted formats
//				are: mm/dd/yyyy, mm-dd-yyyy, mm.dd.yyyy. The date range is 1900 to 2099.
//
// Param:		date string
// Return:		true id date is valid format, false otherwise
//
function validateDateFormat(date) {
	var re = new RegExp("(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d\\d");
	
	var match = re.exec(date);
	var valid = true;

	if (match == null) {
		valid = false;
	}
    else {
		if (match.index != 0 || match[0].length != date.length) {
			valid = false;
		}
	}

	return valid;
}

//
// Author:		Andrzej Adamczyk
// Description:	Validate date string and return error message if date is not valid.
//
// Param:		date string
// Return:		error message
//
function validateDate(date) {
	var errMsg = "";

	if (!validateDateFormat(date)) {
		errMsg += "Invalid date. Please enter date in mm/dd/yyyy format.\n";
	}
	else {
		//validate date
		var month = date.substring(0, 2);
		var day = date.substring(3, 5);
		var year = date.substring(6, date.length);

		errMsg += checkDate(day, month, year);
	}

	return errMsg;
}

//
// Author:      Andrzej Adamczyk
// Description: Validate phone number. Acceptable phone number
//              formats are: (XXX)-XXX-XXXX; (XXX).XXX.XXXX; (XXX)/XXX-XXXX
//              XXX.XXX.XXXX; XXX-XXX-XXXX; XXX/XXX/XXXX
//
// Param:		phone number string
// Return:		true if valid, false otherwise
//
function validatePhone(str) {
    var re = new RegExp("\\(?\\d{3}\\)?([.\\/\\-])\\d{3}\\1\\d{4}");
	var match = re.exec(str);
	var valid = true;

	if (match == null) {
		valid = false;
	}
    else {
		if (match.index != 0 || match[0].length != str.length) {
			valid = false;
		}
	}

	return valid;
}

//
// Author:      Andrzej Adamczyk
// Description: Validate email address
//
// Param:		email string
// Return:		true if valid, false otherwise
//
function validateEmail(str) {
	var re = new RegExp("^[a-zA-Z][\\'\\w\\.\\-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$");
	var match = re.exec(str);
	var valid = true;

	if (match == null) {
		valid = false;
	}
    else {
		if (match.index != 0 || match[0].length != str.length) {
			valid = false;
		}
	}

	return valid;
}

//
// Author:		Andrzej Adamczyk
// Description:	Displays confirmation message. If message passed as an
//				argument is empty string, successful save message is
//				displayed, otherwise error message is displayed.
//
// Param:		error message
//
function showConfirmation(msg) {
	var prefix1 = "Changes have not been saved due to the following errors:\n";
	var prefix2 = "Changes have been saved.";

	if (msg.length > 0) {
		alert(prefix1 + msg);
	}
	else {
		alert(prefix2);
	}
}

//
// Author:		Andrzej Adamczyk
// Desription:	Validates user 511 identifier.
//
// Param:		511 string
// Return:		true if identifier is valid, false otherwise
//
function validateUserName(str) {
	var re = new RegExp("[a-zA-Z0-9]{1,10}");
	var match = re.exec(str);
	var valid = true;

	if (match == null) {
		valid = false;
	} else {
		if (match.index != 0 || match[0].length != str.length) {
			valid = false;
		}
	}

	return valid;
}

//
// Author:		Raj Chintalapalli
// Desription:	Validates zip.
//
// Param:		zip
// Return:		true if zip is valid, else false
//
function validateZIP(field) {
	var valid = "0123456789-";
	var hyphencount = 0;

	if (field.length != 5 && field.length != 10) {
		alert("Please enter your 5 digit or 5 digit+4 zip code.");
		return false;
	}

	for (var i = 0; i < field.length; i++) {
		var temp = field.substring(i, i + 1);
		
		if (temp == "-") hyphencount++;
		
		if (valid.indexOf(temp) == -1) {
			alert("Invalid characters in your zip code. Please try again.");
			return false;
		}

		if ((hyphencount > 1) 
			|| ((field.length == 10) && (field.charAt(5) != "-"))) {
			alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'. Please try again.");
			return false;
		}
	}
	
	return true;
}
