/************************************************
DESCRIPTION: For use with MerckSource links
*************************************************/
function openMerckExternalWin(product, url) {
	if(! confirm("Thank you for visiting " + product + ".com.\n\nThe site you are going to is an unbranded, health education site brought to you by Merck, and is not related to " + product + ".com.\n\nDo you wish to continue?"))
    	return;

    var externalWin = window.open(url,null,"toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=600,height=500");
    if (externalWin.opener == null)
    	externalWin.opener=window;
        externalWin.opener.name="opener";
}

/************************************************
DESCRIPTION: For use with HCP Disclaimer popup
*************************************************/
function openDisclaimerWin() {
  	if (document.cookie) {
  	  if (document.cookie.toLowerCase().indexOf("us_healthcare=yes") >= 0) {
  	    window.location.href="/rizatriptan_benzoate/maxalt/hcp/index.jsp";
  	    return;
  	  }
  	}
	
	var myWin = window.open("/rizatriptan_benzoate/maxalt/hcp_disclaimer_popup.jsp","Healthcare","status=no,width=300,height=190, scrollbars=no")
  	if (myWin.opener == null) 
  		myWin.opener=window;
  	myWin.opener.name="opener";
	myWin.focus();
}

/************************************************
DESCRIPTION: Validates required fields for RMI

PARAMETERS:
   	requestInfo - form to validate

RETURNS:
   	True if valid, alert window with errors if false.
*************************************************/
function validateRMI(requestInfo) {
	// Set variables
	var isErr = false; // for any type of error
	var errMessage = "";
	var errField = ""; // if there is an error, focus on the first required form field error
	
	// Get form values
	var firstname = requestInfo.firstname.value;
	var lastname = requestInfo.lastname.value;
	var email = requestInfo.email.value;
	var address1 = requestInfo.address1.value;
	var city = requestInfo.city.value;
	var state = requestInfo.state.selectedIndex;
	var postalcode = requestInfo.postalcode.value;
	//var birth_month = requestInfo.birth_month.selectedIndex;
	//var birth_day = requestInfo.birth_day.selectedIndex;
	//var birth_year = requestInfo.birth_year.selectedIndex;
	
	if(validateIsEmpty(firstname)) {  // firstname field is empty
		isErr = true;
		errMessage = "Please enter your First Name\r\n";
		if (errField.length <= 0) errField = "firstname";
	}
	if(validateIsEmpty(lastname)) {  // lastname field is empty
		isErr = true;
		errMessage = errMessage + "Please enter your Last Name\r\n";
		if (errField.length <= 0) errField = "lastname";
	}
	if (!validateIsEmpty(email)) {  // email field contains a value
        email = email.trim();
        var x = email.search(/ /);
        var y = email.search(/,/);
        var z = email.search(/;/);
        
        if ((x != -1) || (y != -1) || (z != -1)) { // Check for more than one email address
            isErr = true;
            errMessage = errMessage + "You are only allowed to enter one e-mail address at a time.\r\n";
            if (errField.length <= 0) errField = "email";
        }
        if (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.([A-Za-z0-9]{2,3})$/) == -1) { // Check for valid email address
            isErr = true;
            errMessage = errMessage + "Please enter your valid e-mail address\r\n";
            if (errField.length <= 0) errField = "email";
        }
    } else {  // email field is empty
        isErr = true;
        errMessage = errMessage + "Please enter your E-mail Address\r\n";
        if (errField.length <= 0) errField = "email";
    }
	if(validateIsEmpty(address1)) {  // address1 field is empty
		isErr = true;
		errMessage = errMessage + "Please enter your Address\r\n";
		if (errField.length <= 0) errField = "address1";
	}
	if(validateIsEmpty(city)) {  /// city field is empty
		isErr = true;
		errMessage = errMessage + "Please enter your City\r\n";
		if (errField.length <= 0) errField = "city";
	}
	if(state == 0) {
		isErr = true;
		errMessage = errMessage + "Please enter your State\r\n";
		if (errField.length <= 0) errField = "state";
	}
	if(validateIsEmpty(postalcode)) {  // postalcode field is empty
		isErr = true;
		errMessage = errMessage + "Please enter your ZIP Code\r\n";
		if (errField.length <= 0) errField = "postalcode";
	} else {
		postalcode = postalcode.trim(); // remove whitespace around value
		if (!validateUSZip(postalcode)) { // Check zip code is in valid format
			isErr = true;
			errMessage = errMessage + "Please enter a valid ZIP Code in 5 digit or zip+4 format.\r\n";
			if (errField.length <= 0) errField = "postalcode";
		}
	}
/*	if(birth_month == 0) {
		isErr = true;
		errMessage = errMessage + "Please enter your Date of Birth: Month\r\n";
		if (errField.length <= 0) errField = "birth_month";
	}
	if(birth_day == 0) {
		isErr = true;
		errMessage = errMessage + "Please enter your Date of Birth: Day\r\n";
		if (errField.length <= 0) errField = "birth_day";
	}
	if(birth_year == 0) {
		isErr = true;
		errMessage = errMessage + "Please enter your Date of Birth: Year\r\n";
		if (errField.length <= 0) errField = "birth_year";
	}*/
		
	if (isErr) {
		alert(errMessage);
		
		eval("requestInfo." + errField + ".focus()");
		
		return false;
	}
	
	return true;
}

/************************************************
DESCRIPTION: Validates that a string is a United
  	States zip code in 5 digit format or zip+4
  	format. The following are acceptable:
  		12345
		12345-1234
		12345+1234
		12345 1234
		123451234

PARAMETERS:
	strValue - String to be tested for validity

RETURNS:
   	True if valid, otherwise false.
*************************************************/
function validateUSZip(strValue) {
	if (strValue.search(/(^\d{5}$)|(^\d{5}-\d{4}$)|(^\d{5}\+\d{4}$)|(^\d{5}\W+\d{4}$)|(^\d{9}$)/) == -1) return false;
	return true;
}

/************************************************
DESCRIPTION: Checks if a string is empty

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid (empty), otherwise false (not empty).
*************************************************/
function validateIsEmpty(strValue) {
	strValue = strValue.trim();
	if(strValue.length > 0) return false;
	return true;
}

/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  	be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/
function trimString (str) {
	str = this != window? this : str;
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

// Added as a method to the String.prototype
String.prototype.trim = trimString;