// --------------------- Email Verification Function ----------------------

 function emailVerify(emailid)
{
 
  var strEmail = emailid ;
  if(!validcharOnly(strEmail,"Email Address") )
  return false ; 
  if(strEmail.charAt(strEmail.length-1) == ".")
	return false ;
  for(i=0;i<strEmail.length-1;i++)
  {	
	if(strEmail.charAt(i) == " ")
		return false ;
  }
  pos = strEmail.search("@");
  if(pos != -1)
  {
	part = strEmail.slice(pos+1) ;
	flag = -1;
	pos = part.search("@") ;
	if(pos != -1)
		return false ;
	for(i=0;i<part.length;i++)
	{
		if(part.charAt(i)=='.')
		{	
			flag = 0 ;
			break;
		}
	}
	if(flag != -1)
		return(true) ;
	else
		return(false) ;
  }
  else
	return(false) ;
}

// --------------------- Number Verification Function ----------------------

function numberVerify(numbervalue)
{
	if(isNaN(numbervalue))
		return(false) ;	
	else
		return(true) ;
}

// --------------------- Phone No. Verification Function ----------------------

function phoneVerify(phonevalue)
{
	valid = "0123456789-()"
	phonevalue = allTrim(phonevalue) ;
	for(i=0;i<phonevalue.length;i++)
	{
		flag = 0 ;
		for(j=0;j<valid.length;j++)
		{
			if(phonevalue.charAt(i) == valid.charAt(j))
			{
				flag = 1 ;
				break ;
			}
		}
		if(flag == 0)
			return(false) ;
	}
	return(true) ; 
}

// --------------------- Date Verification Function ----------------------

function dateVerify(datevalue)
{
	var strDate ;
	var month,day,year ;
	var datearray ;
	strDate = datevalue ;
	if(strDate.search("/") != -1 )
		datearray = strDate.split("/") ;
	else if(strDate.search("-") != -1 )
		datearray = strDate.split("-") ;
	else
		return(false) ;
	if(( datearray.length < 3 ) || ( datearray.length > 3 ) )
		return(false);
	
	m = datearray[0] ;
        d = datearray[1] ;
        y = datearray[2] ;
	if(isNaN(m))
		return(false) ;
	month = parseInt(m,10) ;
	if(isNaN(d))
		return(false) ;
	day = parseInt(d,10) ;
	if(isNaN(y))
		return(false) ;
	year = parseInt(y,10) ;
	if((day >= 1 && day <= 31) && ( month == 1 ) ||
		(day >= 1 && day <= 31) && ( month == 3 ) ||
		(day >= 1 && day <= 31) && ( month == 5 ) ||
		(day >= 1 && day <= 31) && ( month == 7 ) ||
		(day >= 1 && day <= 31) && ( month == 8 ) ||
		(day >= 1 && day <= 31) && ( month == 10 ) ||
		(day >= 1 && day <= 31) && ( month == 12 ))
	{
		if( year >= 1900 && year <= 9999 )
		{
			return(true) ;
		}
		else
			return(false) ;
	}
	else if((day >= 1 && day <= 30) && ( month == 4 ) ||
		(day >= 1 && day <= 30) && ( month == 6 ) ||
		(day >= 1 && day <= 30) && ( month == 9 ) ||
		(day >= 1 && day <= 30) && ( month == 11 ))
	{
		if( year >= 1900 && year <= 9999 )
		{
			return(true) ;
		}
		else
			return(false) ;	
	}
	else if((month == 2) && (year%4 == 0) && (day >= 1 && day <= 29))
	{
		if( year >= 1900 && year <= 9999 )
		{
			return(true) ;
		}
		else
			return(false) ;
	}
	else if((month == 2) && (day >= 1 && day <= 28))
	{
		if( year >= 1900 && year <= 9999 )
		{
			return(true) ;
		}
		else
			return(false) ;
		}
	else
		return(false) ;
}

// --------------------- Save message alert Function ----------------------

function saveRecordMsg()
{
	if(confirm("Do you wish to save this record"))
		return(true) ;
	else
		return(false) ;
}

// --------------------- Edit message alert Function ----------------------

function editRecordMsg()
{
	if(confirm("Do you wish to edit this record"))
		return(true) ;
	else
		return(false) ;
}

// --------------------- Delete message alert Function ----------------------

function deleteRecordMsg()
{
	if(confirm("Do you wish to delete this record"))
		return(true) ;
	else
		return(false) ;
}

// --------------------- Exit message alert Function ----------------------

function exitMsg()
{
	if(confirm("Do you wish to exit from this window"))
		return(true) ;
	else
		return(false) ;
}

// --------------------- Customized message alert Function ----------------------

function customizedMsg(msg)
{
	if(confirm(msg))
		return(true) ;
	else
		return(false) ;
}

// --------------------- Email Only field Function ----------------------

function  emailOnly(emailvalue,fieldname)
{
	if(emailVerify(emailvalue))
		return(true);
	else
	{
		alert("Please enter valid email address in "+fieldname) ;
		return(false) ;
	}
}

// --------------------- Number Only field Function ----------------------

function  numberOnly(numbervalue,fieldname)
{
	if(numberVerify(numbervalue))
		return(true);
	else
	{
		alert("The "+fieldname+" can contain numeric values only") ;
		return(false) ;
	}
}

// --------------------- Phone Only field Function ----------------------

function  phoneOnly(phonevalue,fieldname)
{
	if(phoneVerify(phonevalue))
		return(true);
	else
	{
		alert("The "+fieldname+" can contain phone number values only") ;
		return(false) ;
	}
}


// --------------------- Date Only field Function ----------------------

function  dateOnly(datevalue,fieldname)
{
 
	if(dateVerify(datevalue))
		return(true);
	else
	{
		alert("The "+fieldname+" is Invalalid Date. Please Enter Valid Date mm/dd/yyyy.") ;
		return(false) ;
	}
}

//CPS Date Verify
function  CPSdateOnly(datevalue,fieldname)
{
	if(CPSdateVerify(datevalue))
		return(true);
	else
	{
		alert(fieldname+" is Invalalid Date. Please Enter Valid Date YYYY-MM-DD.") ;
		return(false) ;
	}
}

function CPSdateVerify(datevalue)
{

	var strDate ;
	var month,day,year ;
	var datearray ;
	strDate = datevalue ;
	if(strDate.search("/") != -1 )
		datearray = strDate.split("/") ;
	else if(strDate.search("-") != -1 )
		datearray = strDate.split("-") ;
	else
		return(false) ;
	if(( datearray.length < 3 ) || ( datearray.length > 3 ) )
		return(false);
	
	m = datearray[1] ;
        d = datearray[2] ;
        y = datearray[0] ;
	if(isNaN(m))
		return(false) ;
	month = parseInt(m,10) ;
	if(isNaN(d))
		return(false) ;
	day = parseInt(d,10) ;
	if(isNaN(y))
		return(false) ;
	year = parseInt(y,10) ;
	if((day >= 1 && day <= 31) && ( month == 1 ) ||
		(day >= 1 && day <= 31) && ( month == 3 ) ||
		(day >= 1 && day <= 31) && ( month == 5 ) ||
		(day >= 1 && day <= 31) && ( month == 7 ) ||
		(day >= 1 && day <= 31) && ( month == 8 ) ||
		(day >= 1 && day <= 31) && ( month == 10 ) ||
		(day >= 1 && day <= 31) && ( month == 12 ))
	{
		if( year >= 1900 && year <= 9999 )
		{
			return(true) ;
		}
		else
			return(false) ;
	}
	else if((day >= 1 && day <= 30) && ( month == 4 ) ||
		(day >= 1 && day <= 30) && ( month == 6 ) ||
		(day >= 1 && day <= 30) && ( month == 9 ) ||
		(day >= 1 && day <= 30) && ( month == 11 ))
	{
		if( year >= 1900 && year <= 9999 )
		{
			return(true) ;
		}
		else
			return(false) ;	
	}
	else if((month == 2) && (year%4 == 0) && (day >= 1 && day <= 29))
	{
		if( year >= 1900 && year <= 9999 )
		{
			return(true) ;
		}
		else
			return(false) ;
	}
	else if((month == 2) && (day >= 1 && day <= 28))
	{
		if( year >= 1900 && year <= 9999 )
		{
			return(true) ;
		}
		else
			return(false) ;
		}
	else
		return(false) ;
}
//End CPS Date Verify

// --------------------- Triming string from left sides Function ----------------------

function leftTrim(ltrimvalue)
{
	while(ltrimvalue.charAt(0)==' ')
		ltrimvalue = ltrimvalue.substr(1,ltrimvalue.length-1);
		
	return(ltrimvalue) ;
}

// --------------------- Triming string from right sides Function ----------------------

function rightTrim(rtrimvalue)
{
	while(rtrimvalue.charAt(rtrimvalue.length-1)==' ')
		rtrimvalue = rtrimvalue.substr(0,rtrimvalue.length-1);
	
	return(rtrimvalue) ;
}


 // --------------------- Triming string from both sides Function ----------------------

function allTrim(atrimvalue)
{
	return(rightTrim(leftTrim(atrimvalue))) ;
}

//-----------------------ZipCode validation Function---------------------------
function ZipCodevalidcharOnly(charvalue,fieldname)
{
	var iChars = "-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
       for (var i = 0; i < charvalue.length; i++) {
           if (iChars.indexOf(charvalue.charAt(i)) == -1) 
		   {
			alert("Only valid characters are allowed in "+fieldname) ;
			return false;
           }
       }
	return(true);
}


 // --------------------- Character validation Function ----------------------

function validcharOnly(charvalue,fieldname)
{
	invalid = "~/:<>`|^!';"
	charvalue = allTrim(charvalue) ;
	for(i=0;i<invalid.length;i++)
	{
		for(j=0;j<invalid.length;j++)
		{
			if(charvalue.charAt(j) ==  invalid.charAt(i))
			{
				alert("Only valid characters are allowed in "+fieldname) ;
				return(false);
			}
		}
	}
	for(i=0;i<charvalue.length;i++)
	{
		if(charvalue.charAt(i) == 66 || charvalue.charAt(i) == 39)
		{
			alert("Only valid characters are allowed in "+fieldname) ;
			return(false);
		}
	}
	return(true);
}

//_________ Validation for Characters of Password________

function charVerify(charvalue)
{
  var r;
  var s =charvalue
  s=s.toLowerCase();
  i=97;
  flag=true;
  for(j=0;j<s.length;j++)
  {
	for(i=97;i<123;i++)
	{
		r = s.search(String.fromCharCode(i)) ;
		if (r != -1)
		{
			flag=true;
			break;
		}
	else
		{ 
			flag=false;
			
		}
	}
}	for(j=0;j<s.length;j++)
  {
	for(i=0;i<9;i++)
	{
		r = s.search(i) ;
		if (r != -1)
		{
			flag=true;
			break;
		}
	else
		{ 
			flag=false;
			
		}
	}	
}
	
	if (flag==true)
	{
		
		return(true);
	}
	else
		return(false);
	
}
//-------- function for number validation ----------------
function numberVerify1(numbervalue1)
{
	if(isNaN(numbervalue1))
		return(true) ;	
	else
		return(false) ;
}
//-------- function for Alphanumeric values ---------------
 function botValidPassword(charval,fieldname)
{
	if((numberVerify1(charval)==true) && (charVerify(charval)==true))
		{
			return(true);
		}
		else
		{
			alert("Please enter alphanumeric values") ;
			return(false) ;
		}
	
}
 // --------------------- Not Allowed Null Function ----------------------

function notallowedNull(charvalue,fieldname)
{
	if(allTrim(charvalue) == "")
	{
		alert("Value for "+fieldname+" is not allowed to be blank") ;
		return(false);
	}
	return(true) ;
}

 // --------------------- DropDownBox not Null Function ----------------------
function notallowedNullDropDown(charvalue,fieldname,ShipORBill)
{
	if(allTrim(charvalue) == "")
	{
		alert("Please select " + fieldname + " for " +ShipORBill) ;
		return(false);
	}
	return(true) ;
}

 // --------------------- Money Validation Function ----------------------

function moneyOnly(moneyvalue,fieldname)
{  
	valid = "0123456789."
	moneyvalue = allTrim(moneyvalue) ;
	for(i=0;i<moneyvalue.length;i++)
	{
		flag = 0 ;
		for(j=0;j<valid.length;j++)
		{
			if(moneyvalue.charAt(i) == valid.charAt(j))
			{
				flag = 1 ;
				break ;
			}
		}
		if(flag == 0)
		{
			alert("Only Money values are allowed in " + fieldname);
			return(false) ;
		}
	}
	return(true) ; 
}


 // --------------------- Dates Comparison Function ----------------------

function compareDates(datevalue1,datevalue2)
{
	
	var strDate = datevalue1;
	var month1,day1,year1,month2,day2,year2 ;
	pos1 = strDate.search("/") ;
	if(pos1 == 2 || pos1 == 1)
	{
		m = strDate.slice(0,pos1)
		mlater = strDate.slice(pos1+1)
		
		if(isNaN(m))
			return(false) ;
		month1 = parseInt(m,10) ;
		
		pos2 = mlater.search("/") ;
		
		if(pos2 == 2 || pos1 == 1)
		{		
			d = mlater.slice(0,pos2)
			dlater = mlater.slice(pos2+1)
			
			if(isNaN(d))
				return(false) ;
			day1 = parseInt(d,10) ;
			
			y = dlater.slice(0)
			if(isNaN(y))
				return(false) ;
			year1 = parseInt(y,10) ;
		}
	}
	var strDate = datevalue2;
	pos1 = strDate.search("/") ;
	if(pos1 == 2 || pos1 == 1)
	{
		m = strDate.slice(0,pos1)
		mlater = strDate.slice(pos1+1)
		
		if(isNaN(m))
			return(false) ;
		month2 = parseInt(m,10) ;
		
		pos2 = mlater.search("/") ;
		
		if(pos2 == 2 || pos1 == 1)
		{		
			d = mlater.slice(0,pos2)
			dlater = mlater.slice(pos2+1)
			
			if(isNaN(d))
				return(false) ;
			day2 = parseInt(d,10) ;
			
			y = dlater.slice(0)
			if(isNaN(y))
				return(false) ;
			year2 = parseInt(y,10) ;
		}
	}
	if(year1 > year2)
	{
		return(datevalue1) ;
	}
	else if(year2 > year1)
	{
		return(datevalue2) ;
	}
	else if(month1 > month2)
	{
		return(datevalue1) ;
	}
	else if(month2 > month1)
	{
		return(datevalue2) ;
	}
	else if(day1 >day2)
	{
		return(datevalue1) ;
	}
	else if(day2 > day1)
	{
		return(datevalue2) ;
	}
	else
	{
		return(datevalue1) ;
	}
}
 // --------------------- Date Range restriction Function ----------------------
function daterangeVerify(date1,date2,datediff)
{
	sStartDate = date1;
	sEndDate = date2;
        DateRange = datediff; 
	bStartDateSpecified = false;
	bEndDateSpecified = false;
	startDateObj = null;
	endDateObj = null;
	if ( sStartDate.length > 0 )
	{
		bStartDateSpecified = true;
		nStartYear = parseInt(sStartDate.substring(6, 10));
		sStartMonth = sStartDate.substring(0, 2);
		if ( sStartMonth.charAt(0) == '0' )
		{
			nStartMonth = parseInt(sStartMonth.substring(1, 2));
		}
		else
		{
			nStartMonth = parseInt(sStartMonth.substring(0, 2));
		}
		sStartDay = sStartDate.substring(3, 5);
		if ( sStartDay.charAt(0) == '0' )
		{
			nStartDay = parseInt(sStartDay.substring(1, 2));
		}
		else
		{
			nStartDay = parseInt(sStartDay.substring(0, 2));
		}
		startDateObj = new Date(nStartYear, nStartMonth-1, nStartDay);
	}
	if ( sEndDate.length > 0 )
	{
		bEndDateSpecified = true;
		nEndYear = parseInt(sEndDate.substring(6, 10));
		sEndMonth = sEndDate.substring(0, 2);
		if ( sEndMonth.charAt(0) == '0' )
		{
			nEndMonth = parseInt(sEndMonth.substring(1, 2));
		}
		else
		{
			nEndMonth = parseInt(sEndMonth.substring(0, 2));
		}
		sEndDay = sEndDate.substring(3, 5);
		if ( sEndDay.charAt(0) == '0' )
		{
			nEndDay = parseInt(sEndDay.substring(1, 2));
		}
		else
		{
			nEndDay = parseInt(sEndDay.substring(0, 2));
		}
		endDateObj = new Date(nEndYear, nEndMonth-1, nEndDay);
	}
	// Only need to check the date range if both the start and end date are specified
	if ( bStartDateSpecified && bEndDateSpecified )
	{
		nDayDiff = Math.round((endDateObj.getTime() - startDateObj.getTime())/86400000);
		if ( (nDayDiff >= 0) && (nDayDiff <= DateRange) )
		{
			return true;
		}
		else if ( nDayDiff > DateRange )
		{
			alert("An invalid date range was entered.  To Date cannot be more than " + DateRange + " days ahead of From Date.");
			return false;
		}
		
	}
	return true;  
}

//--------------------------- remove all spaces in string --------------
function spacenotallowed(str)
{
	for(i=0;i<str.length;i++)
	{
		if(str.charAt(i)==' ')
			str = str.substr(1,str.length-1);
	}	
	return(str) ;
}

//----------------sdd To Cart  Start-------------------------
var CartPath ="https://secure.mynetpharma.com/shop/";  //LivePath
//var CartPath = "http://localweb/secure.mynetpharma/shop/";  //LivePath

function AddToCart2(ItemCode)
{
	//alert(ItemCode);
	document.location= CartPath + "processcart2.asp?ItemCode="+ ItemCode + "&action=Add"
} 
//----------------Add To Cart  End-------------------------


//----------------Add State Based on Country Start----------------
function StateSearch(counryName,stateName,selectedvalue)
{
var CountryCa = new Array();

CountryCa[0] = "Alberta,AB";CountryCa[1] = "British Columbia,BC";CountryCa[2] = "Manitoba,MB";CountryCa[3] = "New Brunswick,NB";CountryCa[4] = "Newfoundland,NL";
CountryCa[5] = "Northwest Territories,NT";CountryCa[6] = "Nova Scotia,NS";CountryCa[7] = "Nunavut,NU";CountryCa[8] = "Ontario,ON";
CountryCa[9] = "Prince Edward Island,PE";CountryCa[10] = "Quebec,QC";CountryCa[11] = "Saskatchewan,SK";CountryCa[12] = "Yukon Territory,YT";

var CountryUs = new Array();

CountryUs[0] = "alabama,AL";CountryUs[1] = "Alska,AK";CountryUs[2] = "American Samoa,AS";CountryUs[3] = "Arizona,AZ";CountryUs[4] = "Arkansas,AR";
CountryUs[5] = "California,CA";CountryUs[6] = "Colorado,CO";CountryUs[7] = "Connecticut,CT";CountryUs[8] = "Delaware,DE";CountryUs[9] = "District Of Columbia,DC";
CountryUs[10] = "Florida,FL";CountryUs[11] = "Georgia,GA";CountryUs[12] = "Guam,GU";CountryUs[13] = "Hawaii,HI";CountryUs[14] = "Idaho,ID";
CountryUs[15] = "Illinois,IL";CountryUs[16] = "Indiana,IN";CountryUs[17] = "Iowa,IA";CountryUs[18] = "Kansas,KS";CountryUs[19] = "Kentucky,KY";
CountryUs[20] = "Louisiana,LA";CountryUs[21] = "Maine,ME";CountryUs[22] = "Maryland,MD";CountryUs[23] = "Massachusetts,MA";CountryUs[24] = "Michigan,MI";
CountryUs[25] = "Minnesota,MN";CountryUs[26] = "Mississippi,MS";CountryUs[27] = "Missouri,MO";CountryUs[28] = "Montana,MT";CountryUs[29] = "Nebraska,NE";
CountryUs[30] = "Nevada,NV";CountryUs[31] = "New Hampshire,NH";CountryUs[32] = "New Jersey,NJ";CountryUs[33] = "New Mexico,NM";CountryUs[34] = "New York,NY";
CountryUs[35] = "North Carolina,NC";CountryUs[36] = "North Dakota,ND";CountryUs[37] = "Northern Mariana Islands,MP";CountryUs[38] = "Ohio,OH";
CountryUs[39] = "Oklahoma,OK";CountryUs[40] = "Oregon,OR";CountryUs[41] = "Pennsylvania,PA";CountryUs[42] = "Puerto Rico,PR";CountryUs[43] = "Rhode Island,RI";
CountryUs[44] = "South Carolina,SC";CountryUs[45] = "South Dakota,SD";CountryUs[46] = "Tennessee,TN";CountryUs[47] = "Texas,TX";CountryUs[48] = "Utah,UT";
CountryUs[49] = "Vermont,VT";CountryUs[50] = "Virgin Islands,VI";CountryUs[51] = "Virginia,VA";CountryUs[52] = "Washington,WA";CountryUs[53] = "West Virginia,WV";
CountryUs[54] = "Wisconsin,WI";CountryUs[55] = "Wyoming,WY";

var CountryUsm = new Array();

CountryUsm[0] ="Federated States Of Micronesia,FM";CountryUsm[1] = "Marshall Islands,MH";CountryUsm[2] = "Palau,PW";CountryUsm[3] = "Armed Forces - Canal Zone,CZ";
CountryUsm[4] = "Americas Armed Force,AA";CountryUsm[5] = "Pacific Armed Force,AP";CountryUsm[6] = "Armed Forces - Philippine Islands,PI";CountryUsm[7] = "Trust Territory of the Pacific Islands,TT";

var CountryName=document.getElementById(counryName).value;

		if(CountryName==332)
			{
				AddStateToDropD(CountryUs,stateName,selectedvalue);
			}
		if(CountryName==455)
			{
				AddStateToDropD(CountryCa,stateName,selectedvalue);
			}
		if(CountryName==543)
			{
				AddStateToDropD(CountryUsm,stateName,selectedvalue);
			}

}

function AddStateToDropD(StateArray,StateElement,selectedvalue)
{
document.getElementById(StateElement).length=0;

		var selectText=document.createElement('option');
			selectText.text="Select State"
			selectText.value="";
			
			try
				{
					document.getElementById(StateElement).add(selectText,null); // standards compliant
				}
			catch(ex)
			  {
				document.getElementById(StateElement).add(selectText); // IE only
			  }
			
			for (i=0;i<StateArray.length;i++)
			{   
			 
				var y=document.createElement('option');
				var mProdsplit = StateArray[i].split(',');
				y.value= mProdsplit[1];
				y.text=mProdsplit[0];
				try
					{
						document.getElementById(StateElement).add(y,null); // standards compliant
					}
				catch(ex)
				  {
					document.getElementById(StateElement).add(y); // IE only
				  }
			}
			
			if(selectedvalue !=undefined) document.getElementById(StateElement).value= selectedvalue
			   
			
}
//----------------Add State Based on Country End----------------
function whatIs(addr, w, h)
{
var w=w || 500,h=h || 500,l=(screen.availWidth - w) / 2,t=(screen.availHeight - h) / 2;
window.open(addr,"CVV2","width="+w+",height="+h+",left="+l+",top="+t);
}
