// This script is intended for use with a minimum of Netscape 4 or IE 4.

// This fucntion wil redirect to the page whose URL is set in the hidden variable.
// This is for dynamically changing the URL.
function OpenPharmacyAdvisor()
{
	location.href = document.getElementById("hidPharmaAdvisorURL").value;	
}


if(window.document.layers)
{
	var ns4 = true;
}

if(window.document.all) 
{
	var ie4 = true;
}

function mSetFocus(aControl)
{
	try
	{
		aControl.focus();
	}
	catch (e)
	{
	}
}

// Function Name	:	SelectedAgent
// Purpose			:	This function is used to store the value of the selected agent.
/*function SelectedAgent(aObj)
{				
	var idval;
	document.all[aObj.name];
	idval=aObj.children[0].id;
	document.getElementById('Login1_hidSelectedAgentID').value=document.getElementById(idval).value;	
}*/

function SelectedAgent(aObj)
{
	document.getElementById('Login1_hidSelectedAgentID').value = aObj.childNodes[1].innerHTML;
}

// Function Name	:	GetSelectedValues
// Purpose			:	This function is check whether at least one agent selected or not.
function GetSelectedValues()
{				
	var idval;
	idval=document.getElementById('Login1_hidSelectedAgentID').value;
	
	if (Trim(idval)=='')
	{
		alert('Please select agent id'); 
		return false;
	}
	return true;
}

// Function Name	:	OpenScreen
// Purpose			:	This function is used to navigate from page to page.
function OpenScreen(aUrl)
{				
	if (aUrl!='')
	{
		window.document.location.href = aUrl;
		return false;
	}
}

// Function Name	:	Trim
// Purpose			:	This function is helpful for trimming the value.
function Trim(aStrValue)
{
	// this loop will get rid of leading spaces
	while (aStrValue.substring(0,1) == ' ')
		aStrValue = aStrValue.substring(1, aStrValue.length);
	// this loop will get rid of trailing spaces
	while (aStrValue.substring(aStrValue.length-1,aStrValue.length) == ' ')
		aStrValue = aStrValue.substring(0, aStrValue.length-1);
		return aStrValue;

}
//  Trim ends
//===============================================================================

//Checking whether field value is empty or not
function isEmpty(s)
{
	return ((s == null) || (s.length == 0));
}
//End Of Checking whether field is empty or not


//Checking whether field value is Integer or not
function isInteger (s)
{
	var pIntIndex;
	if (isEmpty(s))
	if (isInteger.arguments.length == 1)
		return false;
	else
		return (isInteger.arguments[1] == true);

	for (pIntIndex = 0; pIntIndex < s.length; pIntIndex++)
	{
		// Check that current character is number.
		var c = s.charAt(pIntIndex);

		if (!((c >= "0") && (c <= "9")))
			return false;
	}

	// All characters are numbers.
	return true;
}
//End Of Checking whether field value is Integer or not

//Checking whether field value is positive Integer or not
function isPositiveInteger (s)
{
	if (isInteger(s) == true)
	{
		if ( s == 0 )
			return false;
		else
			return true;
	}
	else 
		return false;
}
//End Of Checking whether field value is Integer or not

function reformat (s)
{   
	var arg;
    var sPos = 0;
    var resultString = "";
    
    for (var pIntIndex = 1; pIntIndex < reformat.arguments.length; pIntIndex++) {
       arg = reformat.arguments[pIntIndex];
       if (pIntIndex % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}

function reformatUSPhone (USPhone)
{   return (reformat (USPhone, "(", 3, ") ", 3, "-", 4))
}


function isUSPhoneNumber (s)
{   var digitsInUSPhoneNumber = 10;
	if (isEmpty(s)) 
       if (isUSPhoneNumber.arguments.length == 1) return false;
       else return (isUSPhoneNumber.arguments[1] == true);       
    return (isInteger(s) && s.length == digitsInUSPhoneNumber)
}

function CheckUSPhone (theField)
{   
	pStrValue= Trim(theField.value);
    if (pStrValue!='')
    {  
		var phoneNumberDelimiters = "()-. ";
		var normalizedPhone = '';
		var i;
		for (i = 0; i < pStrValue.length; i++)
		{   
			// Check that current character isn't whitespace.
			var c = pStrValue.charAt(i);
      		if (phoneNumberDelimiters.indexOf(c) == -1) normalizedPhone += c;
		}						
		if (isUSPhoneNumber(normalizedPhone)) 
		{
			// if you don't want to reformat as (123) 456-789, comment next line out
			theField.value = reformatUSPhone(normalizedPhone);
			return true;
		}
		else 
		{  
			  theField.focus();
			  theField.select();
			  alert('Please enter valid Phone/Fax');
			  return false;					
		}
	}
}


// Funcation Name	:	CheckDate
// Purpose			:	Checking For Date

function CheckDate(theField,MsgFlag)
{

	FormatDate(theField);
	//*****************//
	if (theField.value!='')
	{	
		var strVal,strDt;
		var dateDelimiter = "";
		
		strVal= Trim(theField.value);
		
		if (CheckDate.arguments.length == 1)
			MsgFlag = true;
		
		strDt=new Date();
		
		if (strVal.indexOf('-') != -1)
			dateDelimiter = '-';
		else
			dateDelimiter = '/';
			
		var fI=strVal.indexOf(dateDelimiter);
		var fL=strVal.lastIndexOf(dateDelimiter);
		var yr=(strVal.substring(fL+1));
		var mont=(strVal.substring(0,strVal.indexOf(dateDelimiter)));
		var dt=(strVal.substring(strVal.indexOf(dateDelimiter)+1,strVal.lastIndexOf(dateDelimiter)));

		if(isNaN(dt)||isNaN(mont)||isNaN(yr))
		{
			if (MsgFlag==true)
			{
				alert("Please enter a valid Date.");
				theField.focus();
			}
			theField.value = '';
			return false;
		}
		if (strVal.indexOf(".")!= -1)
		{
			if (MsgFlag==true)
			{
				alert("Please enter a valid Date.");
				theField.focus();
			}
			theField.value = '';
			return false;
		}

		if ((strVal.length>=6))
		{
			if ((mont>0) && (mont<=12))
			{
				if ((dt>0) && (dt<=31))
				{
					if(yr.length==4)
					{
						var now = new Date();
						var thisyear;
						var thisdate;
						var thismonth;
						thisyear = now.getYear();
						thisdate=now.getDate();
						thismonth=now.getMonth();
						var entereddate = new Date(parseInt(yr), parseInt(mont)-1, parseInt(dt), 0, 0, 0);
						var entereddate2 = new Date(parseInt(thisyear), parseInt(thismonth), parseInt(thisdate));
						if(mont==1 || mont==3 || mont==5 || mont==7 || mont==8 || mont==10 || mont==12)
						{
							if(dt<0 || dt>31)
								{
									if (MsgFlag==true)
									{
										alert("Please enter a valid Date.");
										theField.focus();
									}
									theField.value = '';
									return false;
								}
						}
						if(mont==4 || mont==6 || mont==9 || mont==11)
						{
							if(dt<0 || dt>30)
								{
									if (MsgFlag==true)
									{
										alert("Please enter a valid Date.");
										theField.focus();
									}
									theField.value = '';
									return false;
								}

						}
						//leap year
						if (CheckLeapYear(yr)==true)
						{
							if(mont==2)
							{
									if(dt<0 || dt>29)
									{
										if (MsgFlag==true)
										{
											alert("Please enter a valid Date.");
											theField.focus();
										}
										theField.value = '';
										return false;
									}
							}
						}
						if (CheckLeapYear(yr)==false)
						{
							if(mont==2)
							{
									if(dt<0 || dt>28)
									{
										if (MsgFlag==true)
										{
											alert("Please enter a valid Date.");
											theField.focus();
										}
										theField.value = '';
										return false;
									}
							}

						}
					}
					else
					{
						if (MsgFlag==true)
						{
							alert("Please enter a valid Date.");
							theField.focus();
						}
						theField.value = '';
						return false;
					}
				}
				else
				{
					if (MsgFlag==true)
					{
						alert("Please enter a valid Date.");
						theField.focus();
					}
					theField.value = '';
					return false;
				}
			}
			else
			{
				if (MsgFlag==true)
				{
					alert("Please enter a valid Date.");
					theField.focus();
				}
				theField.value = '';
				return false;
			}
		}
		else
		{
			if (MsgFlag==true)
			{
				alert("Please enter a valid Date.");
				theField.focus();
			}
			theField.value = '';
			return false;
		}
	}
	if ( eval(yr) <= '1752')
	{
		if (MsgFlag==true)
		{
			alert("Please enter a valid Date.");
			theField.focus();
		}
		theField.value = '';
		return false;
	}

	if (Trim(theField.value) != '')
	{
		//For auto formating the day and month i.e day 1--01, Month 1--01 in the date
		var pMonthtemp,pDaytemp
		pMonthtemp=Trim(mont);
		pDaytemp=Trim(dt);
		if (pMonthtemp.length==1)
		{
			mont="0"+Trim(mont);
		}
		if (pDaytemp.length==1)
		{
			dt="0"+Trim(dt);
		}

		theField.value = Trim(mont) + "/" + Trim(dt) + "/" + Trim(yr);
	}
	return true;

}
//	CheckDate Ends
//	=======================================================================================


function FormatDate(aField)
{
	// author : HYD1003049
	// date   : 18/03/2004
	// desc    : pass the field to auto format the value to mm/dd/yyyy
	//           if the field value is not correct it won't format...
	var pStrDate = Trim(aField.value);
	var pNewDate;
	
	if ((Trim(pStrDate)!='') && (pStrDate.length == 8))
	{
		if (pStrDate.indexOf('/')==(-1))
		{
			if (isNaN(pStrDate)==false)
			{
				pNewDate=pStrDate;
				if (pStrDate.length==8)
				{
					pNewDate = pStrDate.substring(0,2) + '/' + pStrDate.substring(2,4) + '/' + pStrDate.substring(4,8)
				}
				else if (pStrDate.length==6)
				{
					pNewDate = '0' + pStrDate.substring(0,1) + '/' + '0' + pStrDate.substring(1,2) + '/' + pStrDate.substring(2,6)
				}
				else if (pStrDate.length==7)
				{
					if (parseInt(pStrDate.substring(0,2))>12)
					{
						pNewDate = '0' + pStrDate.substring(0,1) + '/' + pStrDate.substring(1,3) + '/' + pStrDate.substring(3,7)
					}
					else
					{
						if (parseInt(pStrDate.substring(0,2))>9)
						{
							pNewDate = pStrDate.substring(0,2) + '/' + '0' + pStrDate.substring(2,3) + '/' + pStrDate.substring(3,7)
						}
						else
						{
							pNewDate = '0' + pStrDate.substring(0,1) + '/' + '0' + pStrDate.substring(1,3) + '/' + pStrDate.substring(3,7)
						}
					}
				}
				aField.value = pNewDate;
			}
		}
	}
}

//	 Function Name		:	CheckLeapYear
//   Purpose			:	To check the entered year is leap year or not.
function CheckLeapYear(aLngYear)
{
	var pLngYear=aLngYear; //stores the year value
	if ( (pLngYear%4==0) && ( (pLngYear%100!==0) || (pLngYear%400==0)) )
	{
		return true
	}
	else
	{
		return false;
	}
}
//	CheckLeapYear ends
//================================================================================

function HandleEnterKey(aControlName)
{
	//Pass the control name to this function to which the enter key has to bindede
	//Also call this funciton on onkeydown event of Body tag
	
	var pShow;
	
	if (ie4)
	{
		if (event.keyCode == 13)
		{
			pShow=true;
		}
	}
	
	if (ns4)
	{
		if (e.which == 13)
		{
			pShow=false;
		}
	}
	if (pShow==true )
	{	
		var ControlObj = window.document.getElementById(aControlName);
		
		if (ControlObj == '[object]')
		{
			window.document.getElementById(aControlName).click();
			return false;
		}
		else
		{
			return false;
		}
	}
}
//end of handling the enter key

// Function Name	:	DefaultFocus
// Purpose			:	This function is used to focus the control on the first text box control

function DefaultFocus(aParField)
{
	var txtFieldObj;
	
	txtFieldObj=document.getElementById(aParField);
		
	if((txtFieldObj!=null) && (txtFieldObj.disabled == false))
	{	
		txtFieldObj.select();	
		txtFieldObj.focus();
	}
}

// Function Name	:	SetDateDigit
// Purpose			:	Set month and year to 2 digit on lost focus if user specified 1 digit

function SetDateDigit(obj)
{
	obj.value = Trim(obj.value);
	if (obj.value.length == 1)
		obj.value = "0" + obj.value;
}

// Function Name	:	ConvertToDate
// Purpose			:	Converts string to date value

function ConvertToDate(astrDate)
{
	if (Trim(astrDate)!='')
	{
			var strDateValue,strDt;
			strDateValue=new String(astrDate);		
			var fI=strDateValue.indexOf('/');
			var fL=strDateValue.lastIndexOf('/');
			var yr=strDateValue.substring(fL+1);
			//Converting into 4 digit year if 2 digits entered.Ramana on 07/10/2001
			if (yr.length==2)
			{		
				yr = 20 + yr;
			}
			var mont=strDateValue.substring(0,strDateValue.indexOf('/'));
			var dt=strDateValue.substring(strDateValue.indexOf('/')+1,strDateValue.lastIndexOf('/'));
			date=new Date(yr,mont-1,dt);	
			return date         
	}		
}	

//Author : HYD0600012
//Date : 08/10/2005
//Function to check only ALPHA chars.
function CheckALPHAOnly(aObj, aFor)
{
	var txtStateObj=aObj;
	
	if (txtStateObj != null)
	{
		var pIndex;
		var alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		var stateVal = txtStateObj.value;
	
		stateVal = stateVal.toUpperCase();
			
		for (pIndex=0; pIndex<stateVal.length; pIndex++)
		{

			if (alphabets.indexOf(stateVal.charAt(pIndex)) == -1)
			{
				alert("Please enter a valid " + aFor + ".");
				txtStateObj.select();
				txtStateObj.focus();
				return false;
			}
		
		}
	
	}

	return true;
}

//Author : HYD0600012
//Date : 08/10/2005
//Function to check only ALPHA and Numeric chars.
function CheckALPHANum(aObj, aFor)
{
	var txtStateObj=aObj;
	
	if (txtStateObj != null)
	{
		var pIndex;
		var alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ";
		var stateVal = txtStateObj.value;
	
		stateVal = stateVal.toUpperCase();
			
		for (pIndex=0; pIndex<stateVal.length; pIndex++)
		{

			if (alphabets.indexOf(stateVal.charAt(pIndex)) == -1)
			{
				alert("Please enter valid " + aFor + ".");
				txtStateObj.select();
				txtStateObj.focus();
				return false;
			}
		
		}
	
	}

	return true;
}
//Opening the model dislogue starts
var dialogWin = new Object();
var vbNULL = "CSSI_DEFINED_NULL" ;//This is used to track if cancel is pressed in modal dialog or modal dialog is closed by user.

function mfpOpenPopupWindow(pStrSiteName, pStrTitle, pIntHeight, pIntWidth, pBlnShowScrollbar, pStrDestination) 
{
    /*Parameters
     pStrTitle          = Title of the modal screen
     pIntHeight         = Height
     pIntWidth          = Width
     pBlnShowScrollbar  = If you wish to show the scrollbar in the modal form.
     pStrDestination    = Destination with complete querystring etc..
    */
    var pOutput;
    var strOptions = '';
    
    //Get the unique dialog name; so as to avoid overwriting existing dialog box.
    var strDlgName = (new Date()).getSeconds().toString();

    dialogWin.width = pIntWidth;
    dialogWin.height = pIntHeight;
	if(navigator.appName == 'Netscape')
    {
			pBlnShowScrollbar = 1;
	}
    if (pBlnShowScrollbar == 1)
        pStrShowScrollbar ="Yes";
    else
        pStrShowScrollbar ="No";

    //Decode the destination string
    pStrDestination = pStrDestination.replace(/&/g , '~');
    //Build the complete URL
    pStrURL = pStrSiteName + 'popup_main.aspx?Title=' + pStrTitle + '&Scrollbar=' + pStrShowScrollbar + '&Destination=' + pStrDestination 
    
    //Check for the browser type.
    if(navigator.appName == 'Netscape')
    {
        //Center form in Netscape
        dialogWin.left = window.screenX + ((window.outerWidth - dialogWin.width)/2);
        dialogWin.top =  window.screenY + ((window.outerHeight - dialogWin.height)/2);

        strOptions = strOptions + ' height='  + pIntHeight;
        strOptions = strOptions + ',width='   + pIntWidth;
        strOptions = strOptions + ',modal='   + 'yes';
        strOptions = strOptions + ',screenX=' + dialogWin.left;
        strOptions = strOptions + ',screenY=' + dialogWin.top;
        strOptions = strOptions + ',';

        dialogWin.returnedValue = '';
        dialogWin.win=window.open(pStrURL, strDlgName, strOptions);
        pOutput = dialogWin.returnedValue;
    }
    else
    {
        //Center form in IE
        //pIntHeight =  pIntHeight + pIntHeight * 0.1;
        //pIntWidth =  pIntWidth + pIntWidth * 0.005;
        
        dialogWin.left = (screen.width - dialogWin.width)/2;
        dialogWin.top = (screen.height - dialogWin.height)/2;

        strOptions = strOptions + 'dialogHeight: ' + pIntHeight + 'px;';
        strOptions = strOptions + 'dialogWidth: ' + pIntWidth +'px';
        strOptions = strOptions + 'center: no; ';
        strOptions = strOptions + 'help: No; ';
        strOptions = strOptions + 'resizable: No; ';
        strOptions = strOptions + 'status: No; ';
        strOptions = strOptions + 'left:' + dialogWin.left + '; ';
        strOptions = strOptions + 'top:' + dialogWin.top + '; ';
        //alert(pStrURL)
        //alert(strDlgName)
        //alert(strOptions)
        pOutput = window.showModalDialog(pStrURL, strDlgName, strOptions);
    }    
    if (!pOutput) 
    {
        pOutput = vbNULL;
    }
    return pOutput;
}

//Set the return value for child window.
function mfpReturnToparent(pStrValue)
{
    
    if(navigator.appName == 'Netscape')
    {
        top.opener.dialogWin.returnedValue = pStrValue;        
    }
    else
    {
        window.returnValue = pStrValue;
    }
}
//opening model dailogue ends

function FormatGivenDate(aobjControl)
{
	var val = eval(aobjControl);
	var CurrentControl;
	CurrentControl = aobjControl; //document.getElementById(val.controltovalidate) ;
	var op=CurrentControl.value;
	//trim the value automatically
	CurrentControl.value = Trim(op);
	//incase data type is DATE - change the format automatically

	//if ((val.operator == "DataTypeCheck") && (val.type == "Date"))
	//{
		//incase 6 - format yeat first
		if (op.length==6)
		{
				var strVal = CurrentControl.value;
				var fL=strVal.lastIndexOf('/');
				if (fL == -1)
				{
					op = strVal.substring(0,2) + '/' + strVal.substring(2,4) + '/' + strVal.substring(4,6);
				}
				else
				{

					var yr=(strVal.substring(fL+1));
					yr = (parseInt(yr) + parseInt(2000)) - ((parseInt(yr) < 29) ? 0 : 100);
					op = strVal.substring(0,fL+1) + yr ;

				}
		}
		
		//Do autoformat only incase length is 8 chars
		if (op.length == 8)
		{
			if (isNaN(op)==false)
			{
				CurrentControl.value = op.substring(0,2) + '/' + op.substring(2,4) + '/' + op.substring(4,8);
			}
			else
			{	
				var strVal = op;
				var fL=strVal.lastIndexOf('/');
				var yr=(strVal.substring(fL+1));
				if (yr.length==2)
				{
					yr = (parseInt(yr) + parseInt(2000)) - ((parseInt(yr) < 29) ? 0 : 100);
				}

				var month=(strVal.substring(0,strVal.indexOf('/')));
				var dt=(strVal.substring(strVal.indexOf('/')+1,strVal.lastIndexOf('/')));

				if (month.length==1)
				{
					month="0"+month;
				}
				if (dt.length==1)
				{
					dt="0"+dt;
				}

				CurrentControl.value = month + "/" + dt + "/" + yr;
			}
		}
	//}
}

// Check for Valid Email
function echeck(str) 
{

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	var emailIdMessage = "Please enter valid email id.";
	
	if (str.indexOf(at)==-1){
		alert(emailIdMessage)
		return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		alert(emailIdMessage)
		return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert(emailIdMessage)
		return false
	}

		if (str.indexOf(at,(lat+1))!=-1){
		alert(emailIdMessage)
		return false
		}

		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert(emailIdMessage)
		return false
		}

		if (str.indexOf(dot,(lat+2))==-1){
		alert(emailIdMessage)
		return false
		}
	
		if (str.indexOf(" ")!=-1){
		alert(emailIdMessage)
		return false
		}

 		return true					
}
// Email related functions

//This will open email screen
function openEmail(siteName, theURL,value) 
{ 
	var windowTitle;
	windowTitle = 'Email Article';
	mfpOpenPopupWindow(siteName, windowTitle,260,350,0,theURL)
}
//
function ValidateEmail()
{
	var objEmail = document.getElementById("txtTo");
	if (echeck(objEmail.value)== false)
	{
		objEmail.select();
		objEmail.focus();				
		return false;
	}
}
//
function ShowSendMessage()
{
	alert("Article sent to specified address successfully.")
	parent.close();
	return false;
}
//
//Survey related functions
function ShowChooseAPlanOnly()
{

	window.document.getElementById("DivResults").style.display = 'none';
}
//
function HideChooseAPlan()
{
	window.document.getElementById("DivChoosePlan").style.display = 'none';
	window.document.getElementById("DivResults").style.display = 'none';
	return false;
}
//
function ShowResultsPlan()
{
	window.document.getElementById("DivChoosePlan").style.display = 'none';
	window.document.getElementById("DivResults").style.display = 'inline';
}

//
//Login related
function SubmitLogin()
{		
	if (event.keyCode == 13)
	{
		var pControlName = 'Login1_LoginButton';		
		HandleEnterKey(pControlName);
	}
}
//
function ClearMessage()
{	
	var tabObj = document.getElementById("Login1_lblMessage");
	if (tabObj == '[object]')
	{
		tabObj.value = '';		
	}
	return true;
}

function popUp(link,x,y)
 	{
		var sizestr = "height=" + y + ",width=" + x + ",scrollbars=1";
  		var subWin = window.open(link,"",sizestr);
  	}