function trim(s)
{
	while(''+s.charAt(s.length-1)==' ')
		{s=s.substring(0,s.length-1);}
	while(''+s.charAt(0)==' ')
		{s=s.substring(1,s.length)}
return s;
}

function isValidDate(optDay, optMonth, optYear){ 
    if ( optMonth > 12 || optDay > 31){         
        return false; 
		}
    if (( optMonth == 4 || optMonth == 6 || optMonth == 9 || optMonth == 11) && optDay > 30 ){         
        return false; 
		}
     else if ( optMonth == 2 && optDay > 29 ) {        
        return false; 
		} 
    else if (optMonth == 2 && optYear % 4 > 0 && optDay > 28){         
        return false; 
		}
    else {
		return true;
    	}  		
}

function isValidDatePeriod(optDay1, optMonth1, optYear1, optDay2, optMonth2, optYear2){ 
	var sDate
	var eDate
	sDate = new Date(optYear1, optMonth1-1, optDay1)
	eDate = new Date(optYear2, optMonth2-1, optDay2)
	if ((Math.abs(sDate.getTime()/(1000*60*60*24))) - (Math.abs(eDate.getTime()/(1000*60*60*24))) > 0){
			return false
	}
	return true
}

function isPreviousDay(optDay1, optMonth1, optYear1, optDay2, optMonth2, optYear2){ 
	optDay2 = parseInt(optDay2) + 1
	if (!isValidDate(optDay2, optMonth2, optYear2)){
		optDay2 = "1"
		optMonth2 = parseInt(optMonth2) + 1
		if (!isValidDate(optDay2, optMonth2, optYear2)){
			optMonth2 = "1"
			optYear2 = parseInt(optYear2) + 1
		}
	}
//alert("*" + optDay1 + "*" + optDay2 + "*" + optMonth1 + "*" + optMonth2 + "*" + optYear1 + "*" + optYear2)	
	if (optDay1 == optDay2 && optMonth1== optMonth2 && optYear1 == optYear2){
		return true
	}
	return false
}

function isValidEmailid(stremail){
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(stremail) && r2.test(stremail));
}





function ValidateEnq()
{
	var strTemp;

	if (document.frmIPLC.txtCountry.selectedIndex == 0)
	{
		alert("Please select country.");
		document.frmIPLC.txtCountry.focus();
		return false;
	}
	strTemp = trim(document.frmIPLC.txtName.value);
	if (strTemp.length == 0)
	{
		alert("Please enter patient's name.");
		document.frmIPLC.txtName.focus();
		return false;
	}
	if (document.frmIPLC.txtAge.selectedIndex == 0)
	{
		alert("Please enter your age.");
		document.frmIPLC.txtAge.focus();
		return false;
	}
	/*
	strTemp = trim(document.frmIPLC.txtPPNo.value);
	if (strTemp.length == 0)
	{
		alert("Please enter your passport number.");
		document.frmIPLC.txtPPNo.focus();
		return false;
	}
	*/
	strTemp = trim(document.frmIPLC.txtTel.value);
	if (strTemp.length == 0)
	{
		alert("Please enter your telephone number.");
		document.frmIPLC.txtTel.focus();
		return false;
	}
	strTemp = trim(document.frmIPLC.txtEmail.value);
	if (strTemp.length == 0)
	{
		//alert("Please enter your email address.");
		//document.frmIPLC.txtEmail.focus();
		//return false;
	}
	else
	if (isValidEmailid(strTemp) == false)
	{
		alert("Invalid email address.");
		document.frmIPLC.txtEmail.focus();
		return false;
	}

	//alert("OK");
	document.frmIPLC.action = "iplcenquirySendEmail.asp";
	return true;
}



function ValidateApt()
{
	var strTemp;

	if (document.frmIPLC.txtCountry.selectedIndex == 0)
	{
		alert("Please select country.");
		document.frmIPLC.txtCountry.focus();
		return false;
	}
	strTemp = trim(document.frmIPLC.txtName.value);
	if (strTemp.length == 0)
	{
		alert("Please enter patient's name.");
		document.frmIPLC.txtName.focus();
		return false;
	}
	if (document.frmIPLC.txtAge.selectedIndex == 0)
	{
		alert("Please enter patient's age.");
		document.frmIPLC.txtAge.focus();
		return false;
	}
	strTemp = trim(document.frmIPLC.txtPPNo.value);
	if (strTemp.length == 0)
	{
		alert("Please enter patient's passport number.");
		document.frmIPLC.txtPPNo.focus();
		return false;
	}
	strTemp = trim(document.frmIPLC.txtTel.value);
	if (strTemp.length == 0)
	{
		alert("Please enter your telephone number.");
		document.frmIPLC.txtTel.focus();
		return false;
	}
	strTemp = trim(document.frmIPLC.txtEmail.value);
	if (strTemp.length != 0 && isValidEmailid(strTemp) == false)
	{
		alert("Invalid email address.");
		document.frmIPLC.txtEmail.focus();
		return false;
	}
	if (document.frmIPLC.chkNpDt.checked == false)
	{
		if (document.frmIPLC.aptFdd.value == "" || document.frmIPLC.aptFmm.value == "" || document.frmIPLC.aptFyy.value == "")
		{
			alert("Please enter preferred appointment date(From).");
			document.frmIPLC.aptFdd.focus();
			return false;
		}
		if (!isValidDate(document.frmIPLC.aptFdd.value, document.frmIPLC.aptFmm.value, document.frmIPLC.aptFyy.value))
		{
			alert("Invalid preferred appointment date(From).");
			document.frmIPLC.aptFdd.focus();
			return false;
		}
		if (document.frmIPLC.aptTdd.value == "" || document.frmIPLC.aptTmm.value == "" || document.frmIPLC.aptTyy.value == "")
		{
			alert("Please enter preferred appointment date(To).");
			document.frmIPLC.aptTdd.focus();
			return false;
		}
		if (!isValidDate(document.frmIPLC.aptTdd.value, document.frmIPLC.aptTmm.value, document.frmIPLC.aptTyy.value))
		{
			alert("Invalid preferred appointment date(To).");
			document.frmIPLC.aptTdd.focus();
			return false;
		}
		if (!isValidDatePeriod(document.frmIPLC.aptFdd.value, document.frmIPLC.aptFmm.value, document.frmIPLC.aptFyy.value,document.frmIPLC.aptTdd.value, document.frmIPLC.aptTmm.value, document.frmIPLC.aptTyy.value))
		{
			alert("Invalid preferred appointment date range.\n Date(To) can not be less than date(From).");
			document.frmIPLC.aptFdd.focus();
			return false;
		}
		if (document.frmIPLC.optTime(0).checked == false && document.frmIPLC.optTime(1).checked == false && document.frmIPLC.optTime(2).checked == false)
		{
			alert("Please select appointment time.");
			document.frmIPLC.optTime(0).focus();
			return false;
		}
	}

	//alert("OK");
	document.frmIPLC.action = "iplcapptSendEmail.asp";
	return true;
}



function ValidateAptCancel()
{
	var strTemp;

	strTemp = trim(document.frmIPLC.txtName.value);
	if (strTemp.length == 0)
	{
		alert("Please enter patient's name.");
		document.frmIPLC.txtName.focus();
		return false;
	}
	strTemp = trim(document.frmIPLC.txtPPNo.value);
	if (strTemp.length == 0)
	{
		alert("Please enter patient's passport number.");
		document.frmIPLC.txtPPNo.focus();
		return false;
	}
	if (document.frmIPLC.optHsp(0).checked == false && document.frmIPLC.optHsp(1).checked == false)
	{
		alert("Please select original appointment hospital.");
		document.frmIPLC.optHsp(0).focus();
		return false;
	}
	if (trim(document.frmIPLC.txtClinic.value).length == 0 && trim(document.frmIPLC.txtDr.value).length == 0)
	{
		alert("Please enter either clinic or doctor name.");
		document.frmIPLC.txtClinic.focus();
		return false;
	}
	if (document.frmIPLC.aptdd.value == "" || document.frmIPLC.aptmm.value == "" || document.frmIPLC.aptyy.value == "")
	{
		alert("Please enter original appointment date.");
		document.frmIPLC.aptdd.focus();
		return false;
	}
	if (!isValidDate(document.frmIPLC.aptdd.value, document.frmIPLC.aptmm.value, document.frmIPLC.aptyy.value))
	{
		alert("Invalid original appointment date.");
		document.frmIPLC.aptdd.focus();
		return false;
	}
	/*
	if (document.frmIPLC.apthr.value == "")
	{
		alert("Please enter original appointment time.");
		document.frmIPLC.apthr.focus();
		return false;
	}
	if (document.frmIPLC.aptmin.value == "")
	{
		alert("Please enter original appointment time.");
		document.frmIPLC.aptmin.focus();
		return false;
	}
	if (document.frmIPLC.aptap.value == "")
	{
		alert("Please enter original appointment time.");
		document.frmIPLC.aptap.focus();
		return false;
	}
	*/
	/*
	if (document.frmIPLC.aptType.value == "")
	{
		alert("Please select cancel or change of appointment.");
		document.frmIPLC.aptType.focus();
		return false;
	}
	*/
	//if (document.frmIPLC.aptType.value == "CHANGE" && document.frmIPLC.chkNpDt.checked == false)
	if (document.frmIPLC.optAptType(1).checked == true && document.frmIPLC.chkNpDt.checked == false)
	{
		if (document.frmIPLC.aptFdd.value == "" || document.frmIPLC.aptFmm.value == "" || document.frmIPLC.aptFyy.value == "")
		{
			alert("Please enter preferred new appointment date(From).");
			document.frmIPLC.aptFdd.focus();
			return false;
		}
		if (!isValidDate(document.frmIPLC.aptFdd.value, document.frmIPLC.aptFmm.value, document.frmIPLC.aptFyy.value))
		{
			alert("Invalid preferred new appointment date(From).");
			document.frmIPLC.aptFdd.focus();
			return false;
		}
		if (document.frmIPLC.aptTdd.value == "" || document.frmIPLC.aptTmm.value == "" || document.frmIPLC.aptTyy.value == "")
		{
			alert("Please enter preferred new appointment date(To).");
			document.frmIPLC.aptTdd.focus();
			return false;
		}
		if (!isValidDate(document.frmIPLC.aptTdd.value, document.frmIPLC.aptTmm.value, document.frmIPLC.aptTyy.value))
		{
			alert("Invalid preferred new appointment date(To).");
			document.frmIPLC.aptTdd.focus();
			return false;
		}
		if (!isValidDatePeriod(document.frmIPLC.aptFdd.value, document.frmIPLC.aptFmm.value, document.frmIPLC.aptFyy.value,document.frmIPLC.aptTdd.value, document.frmIPLC.aptTmm.value, document.frmIPLC.aptTyy.value))
		{
			alert("Invalid preferred new appointment date range.\n Date(To) can not be less than date(From).");
			document.frmIPLC.aptFdd.focus();
			return false;
		}
		if (document.frmIPLC.optNewTime(0).checked == false && document.frmIPLC.optNewTime(1).checked == false && document.frmIPLC.optNewTime(2).checked == false)
		{
			alert("Please select new appointment time.");
			document.frmIPLC.optNewTime(0).focus();
			return false;
		}
	}

	//alert("OK");
	//return false;
	document.frmIPLC.action = "iplcapptcancelSendEmail.asp";
	return true;
}

function ValidateSendEmail()
{
	var strTemp;

	strTemp = trim(document.frmIPLC.txtEmail.value);
	if (strTemp.length == 0)
	{
		alert("Please enter email address.");
		document.frmIPLC.txtEmail.focus();
		return false;
	}
/*
	if (isValidEmailid(strTemp) == false)
	{
		alert("Invalid email address.");
		document.frmIPLC.txtEmail.focus();
		return false;
	}
*/
	var idx;
	var aryTemp;
	aryTemp = strTemp.split(",");
	for (idx=0; idx < aryTemp.length; idx++)
	{
		if (isValidEmailid(trim(aryTemp[idx])) == false)
		{
			alert("Invalid email address.");
			document.frmIPLC.txtEmail.focus();
			return false;
		}
	}

	//alert("OK");
	//return false;
	document.frmIPLC.action = "SendEmail2Friend.asp";
	return true;
}

