function checkDateCount(year, month)
{
	var day; 
	var date = new Date(year,month,0,0,0,0,0);
	day = date.getDate();
	return day;
}

function checkDateFormat(date)
{
	if(date.length < 10 || date.length > 10)
	{
		return false;
	}
	else if(date.charAt(2) != "/" || date.charAt(5) != "/")
	{			
		return false;						
	}
	else if(isNaN(date.substring(0,2)) || isNaN(date.substring(3,5)) || isNaN(date.substring(6,10)))
	{
		return false;
	}
	else
	{
		var day, month, year

		year = parseInt(date.substring(6,10));

		if(date.substring(0,1) == "0")
		{
			day = date.substring(1,2);
		}
		else
		{
			day = date.substring(0,2);
		}

		if(date.substring(3,4) == "0")
		{
			month = date.substring(4,5);
		}
		else
		{
			month = date.substring(3,5);
		}

		if(year < 1900 || year > 2050)
		{
			return false;
		}
		else if(month < 1 || month > 12)
		{
			return false;
		}
		else 
		{
			var dayOfMonth = checkDateCount(year,month);
			if(day < 1 || day > dayOfMonth)
			{
				return false;
			}
		}
	}
	return true;
}

function checkTimeFormat(time)
{
	if(time.length < 7 || time.length > 7)
	{
		return false;
	}
	else if(time.substring(2,3) != ":")
	{				
		return false;							
	}
	else if(isNaN(time.substring(0,2)) || isNaN(time.substring(3,5)))
	{ 
		return false;
	}
	else
	{
		var hour, min, ap;
		if(time.substring(0,1) != "0")
		{
			hour = time.substring(0,2);
		}	
		else
		{
			hour = time.substring(1,2);
		}

		if(time.substring(3,4) != "0")
		{
			min = time.substring(3,5);
		}
		else
		{
			min = time.substring(4,5);
		}
		ap = time.substring(5,7).toUpperCase();

		if(hour < 1 || hour > 12)
		{
			return false;
		}
		else if(min > 60)
		{
			return false;
		}
		else if(ap != "AM" && ap != "PM")
		{
			return false;
		}
	}
	return true;
}


function checkValue(name, nameArray)
{
	var j;
	var index = -1;
	for(j=0; j<nameArray.length; j++)
	{
		if(nameArray[j][0] == name)
		{
			index = j;
			break;	
		}
	}
	return index;
}



function popup(url,name,scroll,resize,width,height)
{
	remote = window.open(url,name,'scrollbars='+scroll+',resizable=no,width='+width+',height='+height+',left=0,top=15');
}


function popup_detail(url,name,tool,scroll,resize,width,height,left,top)
{
	remote = window.open(url,name,'toolbar='+tool+',scrollbars='+scroll+',resizable=no,width='+width+',height='+height+',left='+left+',top='+top);
}


function popup_modeless(url,name,tool,scroll,resize,width,height,left,top)
{
	remote = window.showModelessDialog(url,name, 'width='+width+',height='+height+',left='+left+',top='+top);
}


function popup_modal(url,name,tool,scroll,resize,width,height,left,top)
{

	remote = window.showModalDialog(url,name, 'width='+width+',height='+height+',left='+left+',top='+top);

}



function confirm_del(value)
{ 
	return confirm('Are you sure you want to delete this '+value+' ?');
}





function MM_reloadPage(init) 
{  //reloads the window if Nav4 resized
	if (init==true) with (navigator) 
	{
		if ((appName=="Netscape")&&(parseInt(appVersion)==4)) 
		{
    			document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; 
		}
	}
  	else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}



MM_reloadPage(true);

function MM_findObj(n, d)
{ 
	var p,i,x;
	if(!d)
		d=document;

	if((p=n.indexOf("?"))>0&&parent.frames.length)
	{
    	d=parent.frames[n.substring(p+1)].document;
		n=n.substring(0,p);
	}

	if(!(x=d[n])&&d.all)
		x=d.all[n];

	for (i=0;!x&&i<d.forms.length;i++)
		x=d.forms[i][n];

	for(i=0;!x&&d.layers&&i<d.layers.length;i++)
		x=MM_findObj(n,d.layers[i].document);

  	if(!x && document.getElementById)
		x=document.getElementById(n);

	return x;
}	

	

function MM_showHideLayers() 
{
  	var i,p,v,obj,args=MM_showHideLayers.arguments;
  	for (i=0; i<(args.length-2); i+=3) 
	if ((obj=MM_findObj(args[i]))!=null) 
	{ 
		v=args[i+2];
    	if (obj.style) 
		{ 
			obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; 
		}

    	obj.visibility=v; 
	}
}


function checkTextAreaSize(curForm, fieldName, fieldLength)
{
	var objElement =  eval("curForm."+fieldName);
	var fieldValue = trim(objElement.value);

	if(fieldValue.length > 0)
	{
		dblQuote(fieldName);
		if(fieldValue.length > fieldLength)
		{
			alert("The max. characters is " + fieldLength + ", extra character(s) have been discarded.");
			objElement.value = fieldValue.substr(0,fieldLength);
			eval("curForm."+fieldName+".focus()");		
		}
	}
}



function ValidateStartEndDate(curForm, startDate, endDate, focusField, Msg, flag)
{
//flag 1 = alert

//flag 2 = confirm

	var startDt = eval("curForm."+startDate+".value");
	var endDt = eval("curForm."+endDate+".value");
	var blnValidateStartEndDate = true;

	if (startDt != "" && endDt != "")
	{
		var startTime = new Date(changeDateMonth(startDt)).getTime();	
		var endTime = new Date(changeDateMonth(endDt)).getTime();	
		var dateRange = startTime - endTime;

		if(dateRange > 0)
		{
			if(flag == 1)
			{		
				alert(Msg);
				document.all(focusField).focus();
				blnValidateStartEndDate = false;
			}
			else if(flag == 2)
			{
				if(confirm(Msg))
				{
					blnValidateStartEndDate = true;
				}
				else
				{
					blnValidateStartEndDate = false;
				}
			}
		}
	}	
	return blnValidateStartEndDate;
}



function ValidateStartEndDateTime(curForm, startDate, startTime, endDate, endTime, backToField, Msg)
{
	var startDt = "";
	var startTm = "";
	var endDt = "";
	var endTm = "";

	startDt = eval("curForm."+startDate+".value");

	if (startTime != "")
	{
		startTm = eval("curForm."+startTime+".value");
	}

	endDt = eval("curForm."+endDate+".value");

	if (endTm != "")
	{
		endTm = eval("curForm."+endTime+".value");
	}

	var blnValidateStartEndDate = true;

	if (startDt != "" && endDt != "")
	{
		var startTime = new Date(changeDateMonth(startDt) + " " + startTm).getTime();	
		var endTime = new Date(changeDateMonth(endDt) + " " + endTm).getTime();	
		var dateRange = startTime - endTime;

		if(dateRange > 0)
		{
			alert("Invalid input [" + eval("curForm."+backToField+".value") + "] : " + Msg);
			if (backToField != "")
			{
				document.all(backToField).value = "";
				if (backToField == startTime && startDt != endDt)
				{
					document.all(startDate).value = "";
				}
				else if (backToField == endTime && startDt != endDt)
				{
					document.all(endDate).value = "";
				}

				document.all(backToField).select();
				document.all(backToField).focus();
			}
			else
			{
				document.all(startDate).select();
				document.all(startDate).focus();
			}

			blnValidateStartEndDate = false;
		}
	}	

	return blnValidateStartEndDate;
}



function trim(strText)
{
	while(strText.charAt(0) == ' ' && strText.length > 0)
	{
		strText = strText.substring(1, strText.length);
	}

	while(strText.length>0 && strText.charAt(strText.length-1)==' ')
	{
		strText = strText.substring(0, strText.length-1);
	}
	return strText;
}



function changeDateMonth(dateValue)
{
	var strMonth="";
	var strDay = "";
	var strYear = "";
	var changedDate = "";

	if(dateValue.length > 0)	
	{
		strDay = dateValue.substring(0, 2);
		strMonth = dateValue.substring(3, 5);
		strYear = dateValue.substring(6, 10); 		
		changedDate = strMonth + "/" + strDay + "/" + strYear;
	}
	return changedDate;
}


function dblQuote(fieldName)
{
	var strIn = trim(document.all(fieldName).value);
	if(strIn.length > 0)
	{
		if(!checkDblQuote(strIn))
		{
			alert('Invalid character ^\\');
			document.all(fieldName).focus();
		}
	}
}	



function checkDblQuote(strIn)
{
	var strOut = trim(strIn);
	var re = /\^\\"/gi;
	var blnMatch = "";	
	if(strOut.length > 0)
	{
		blnMatch = strOut.match(re);
		if(blnMatch == null)
		{
			return true;
		}
		else if(blnMatch != null)
		{
			return false;	
		}
	}	
}



function changeDblQuote(strIn, flag)
{
	var strOut = trim(strIn);
	if(strOut.length > 0)
	{
		if(flag ==1)
		{
//			strOut = strOut.replace(/"/gi, '^\\');
			strOut = strOut.replace(/"/gi, '&quot;');
		}
		else if(flag == 2)
		{
			strOut = strOut.replace(/\^\\/gi, '"');
		}
	}
	return strOut;
}



function fmtInputField(aryField, flag)
{
	var i=0;
	if(flag == 1)
	{
		for(i=0; i<aryField.length; i++)
		{
			document.all(aryField[i]).value = changeDblQuote(document.all(aryField[i]).value, flag);
		}	
	}
	else if(flag == 2)
	{
		for(i=0; i<aryField.length; i++)
		{
			document.all(aryField[i]).value = changeDblQuote(document.all(aryField[i]).value, flag);
		}		
	}
}

function CheckEmpty()
{
	if(window.document.fClientList.sCompanyName.value == "")
	{
		alert("Please input the Company Name!");
		window.document.fClientList.sCompanyName.focus();
	}	
	else if(document.fClientList.sCompanyNature.value == "")
	{
		alert("Please input the Company Nature!");
		window.document.fClientList.sCompanyNature.focus();
	}
	else if(!trim(window.document.fClientList.dFirstContact.value)=='' && !checkDateFormat(window.document.fClientList.dFirstContact.value))
	{
		alert("Please input the valid format of first contact date!");
		window.document.fClientList.dFirstContact.focus();
	}
	else if(!trim(window.document.fClientList.dFirstPresent.value)=='' && !checkDateFormat(window.document.fClientList.dFirstPresent.value))
	{
		alert("Please input the valid format of first presentation date!");
		window.document.fClientList.dFirstPresent.focus();
	}
	else
	{
		window.document.fClientList.submit();
	}	
}

