// JavaScript Document

var invalid=" ";
var maxLen = 50;

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
	
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;

	for (i = 0; i < strString.length && blnResult == true; i++)
    {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
        {
			blnResult = false;
        }
	}

	 return blnResult;
	}
	
function validemailadd()
	{
		var str=document.addUsr.email.value
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if (filter.test(str))
			testresults=true;
		else
		{
			alert("Please input a valid email address!");
			testresults=false;
		}
		return testresults;
	}
	
function isValidIPAddress(ipaddr)
{
	var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
	if (re.test(ipaddr))
	{
		var parts = ipaddr.split(".");
		if (parseInt(parseFloat(parts[0])) == 0)
		{
			return false;
		}
		for (var i=0; i<parts.length; i++)
		{
			if (parseInt(parseFloat(parts[i])) > 255)
			{
				return false;
			}
		}
		return true;
	}
	else
	{
		return false;
	}
}

function trim1(str)
{
  var i,j;
  i = 0;
  j=str.length-1;
  str = str.split("");
  while(i < str.length)
  {
    if(str[i]==" ")
    {
      str[i] = ""
    }
    else
    {
      break;
    }
    i++;
  }
  while(j > 0)
  {
    if(str[j]== " ")
    {
      str[j]=""
    }
    else
    {
      break;
    }
    j--;
  }
  return str = str.join("")
}

function setFocus()
{
	if(document.forms.login.uid.value!=""||document.forms.login.uid.value!=" ")
	{
		document.forms.login.uid.focus();
	}
	else
	{
		document.forms.login.password.focus();
	}
}

function validatelogin()
	{
		login.Submit.disabled=true;
		if(login.uid.value == ""||login.uid.value == " ")
		{
			alert("Enter a valid user Id");
			login.uid.focus();
			login.Submit.disabled=false;
			return false;
		}
		
		if(login.pwd.value == ""||login.pwd.value == " ")
		{
			alert("password field cannot be blank");
			login.pwd.focus();
			login.Submit.disabled=false;
			return false;
		}
	return true;

	}
	
function validatechpwd()
			{	
				chpwd.Submit.disabled=true;
				if(chpwd.oldpwd.value == ""||chpwd.oldpwd.value == " ")
				{
					alert('You must enter your old password');
					chpwd.oldpwd.focus();
					chpwd.Submit.disabled=false;
					return false;
				}
				if(chpwd.newpwd.value == ""||chpwd.newpwd.value == " ")
				{
					alert('You must enter a new password you want');
					chpwd.newpwd.focus();
					chpwd.Submit.disabled=false;
					return false;
				}
				if (chpwd.newpwd.value.indexOf(invalid) > -1)
				{
					alert('You entered spaces in new password field.\nSpaces are not allowed');
					chpwd.newpwd.focus();
					chpwd.Submit.disabled=false;
					return false;
				}
				if(chpwd.newpwdcopy.value == ""||chpwd.newpwdcopy.value == " ")
				{
					alert('Please re-enter new password for confirmation');
					chpwd.newpwdcopy.focus();
					chpwd.Submit.disabled=false;
					return false;
				}
				if(chpwd.newpwd.value != chpwd.newpwdcopy.value)
				{
					alert('new password and re type new password fields are not matching');
					chpwd.newpwdcopy.focus();
					chpwd.Submit.disabled=false;
					return false;
				}
				return true;
			}

function add()
	{
		if (IsNumeric(addClient.amt_dom.value) == false) 
		{
			alert("Please provide only numeric characters for amount domain!");
			addClient.amt_dom.focus();
			return false;
		}

		if (IsNumeric(addClient.amt_space.value) == false) 
		{
			alert("Please provide only numeric characters for amount space!");
			addClient.amt_space.focus();
			return false;
		}

		if (IsNumeric(addClient.amt_db.value) == false) 
		{
			alert("Please provide only numeric characters for amount database!");
			addClient.amt_db.focus();
			return false;
		}
		
		var amtDomain = addClient.amt_dom.value;
		//var amt = amt + parseInt(amtDomain);
		var amtSpace = addClient.amt_space.value;
		//amt = amt + parseInt(amtSpace);
		var amtDb = addClient.amt_db.value;
		
		/*var dom = parseInt(amtDomain);
		var space = parseInt(amtSpace);
		var db = parseInt(amtDb);*/

		var amt = parseInt(amtDomain) + parseInt(amtSpace) + parseInt(amtDb);

		//var amt = dom + space + db;
		//amt = amt + parseInt(amtDb);
		addClient.amt.value = amt;
		//alert(amtDomain);


		
		return true;
	}
	
function validateClient()
	{
		addClient.Submit.disabled=true;
		if(addClient.cl_name.value==""||addClient.cl_name.value==" ")
		{
			alert('Please Provide the name of the client');
			addClient.cl_name.focus();
			addClient.Submit.disabled=false;
			return false;
		}
		/*if(addClient.domain.value==""||addClient.domain.value==" ")
		{
			alert('Please provide doamin address booked to the client after "http://"');
			addClient.domain.focus();
			return false;
		}
		if ((addClient.domain.value.indexOf(".") == -1) || (addClient.domain.value.indexOf(":") == -1) || (addClient.domain.value.indexOf("/") == -1))
		{
			msg="http://www.mysite.com";
			alert('Please provide a valid domain name, eg: '+msg);
			addClient.domain.focus();
			return false;
		}
		if(addClient.space.selectedIndex==0)
		{
			alert('Please select space booked on server')
			addClient.space.focus();
			return false;
		}*/
		
		if (addClient.amt_dom.value=="")
		{
			alert('Please Provide the amount for the domain booked');
			addClient.amt_dom.focus();
			return false;
		}
		if (addClient.amt_space.value=="")
		{
			alert('Please Provide the amount for the space booked');
			addClient.amt_space.focus();
			return false;
		}
		if (addClient.amt_db.value=="")
		{
			alert('Please Provide the amount for the database');
			addClient.amt_db.focus();
			return false;
		}
		if(addClient.maintenance.selectedIndex==0)
		{
			alert('Please select maintenance criteria')
			addClient.maintenance.focus();
			return false;
		}
		if(addClient.month.selectedIndex==0)
		{
			alert('Please select month of expiry date')
			addClient.month.focus();
			return false;
		}
		if(addClient.dd.selectedIndex==0)
		{
			alert('Please select day of expiry date')
			addClient.dd.focus();
			return false;
		}
		if (addClient.dd.options[addClient.dd.selectedIndex].value>30 && (addClient.month.options[addClient.month.selectedIndex].value=="4" || addClient.month.options[addClient.month.selectedIndex].value=="6" || addClient.month.options[addClient.month.selectedIndex].value=="9" || addClient.month.options[addClient.month.selectedIndex].value=="11"))
		{
			alert ( "Please select a valid date for the month." );
			addClient.dd.focus();
			return false;
		}
		if(addClient.yy.selectedIndex==0)
		{
			alert('Please select year of expiry date')
			addClient.yy.focus();
			return false;
		}
		if (addClient.dd.options[addClient.dd.selectedIndex].value>28 && addClient.month.options[addClient.month.selectedIndex].value=="2" && addClient.yy.options[addClient.yy.selectedIndex].value % 4 != 0)
		{
			alert ( "Please select a valid date for the month of the year." );
			addClient.dd.focus();
			return false;	
		}
		if (addClient.dd.options[addClient.dd.selectedIndex].value>29 && addClient.month.options[addClient.month.selectedIndex].value=="2")
		{
			alert('Month of february never can have more than 29 days');
			addClient.dd.focus();
			return false;
		}
		/*if(addClient.ip.value==""||addClient.ip.value==" ")
		{
			alert('Please Provide the IP address of the domain booked');
			addClient.ip.focus();
			return false;
		}
		if (isValidIPAddress(addClient.ip.value) == false) 
		{
			alert('Please provide a valid IP address ')
			addClient.ip.focus();
			return false;
		}
		if(a ddClient.du_name.value==""||addClient.du_name.value==" ")
		{
			alert('Please Provide the domain user name of the domain booked');
			addClient.du_name.focus();
			return false;
		}
		if(addClient.d_passwd.value==""||addClient.d_passwd.value==" ")
		{
			alert('Please Provide the domain user password of the domain booked');
			addClient.d_passwd.focus();
			return false;
		}
		if(addClient.d_passwd_copy.value==""||addClient.d_passwd_copy.value==" ")
		{
			alert('Please retype the domain user password of the domain booked');
			addClient.d_passwd_copy.focus();
			return false;
		}
		if(addClient.d_passwd.value!=addClient.d_passwd_copy.value)
		{
			alert('Domain user password and password copy fields are not matching');
			addClient.d_passwd_copy.focus();
			return false;
		}
		if(addClient.srvr1_ip.value==""||addClient.srvr1_ip.value==" ")
		{
			alert('Please Provide the IP address of server1');
			addClient.srvr1_ip.focus();
			return false;
		}
		if (isValidIPAddress(addClient.srvr1_ip.value) == false) 
		{
			alert('Please provide a valid IP address ')
			addClient.srvr1_ip.focus();
			return false;
		}
		if(addClient.srvr2_ip.value==""||addClient.srvr2_ip.value==" ")
		{
			alert('Please Provide the IP address of server2');
			addClient.srvr2_ip.focus();
			return false;
		}
		if (isValidIPAddress(addClient.srvr2_ip.value) == false) 
		{
			alert('Please provide a valid IP address ')
			addClient.srvr2_ip.focus();
			return false;
		}*/
		return true;
	}
	
	function validateDomain()
	{
		domain.Submit.disabled=true;
		if(trim1(domain.dname.value)=='')
		{
			alert('Please provide a domain name');
			domain.dname.focus();
			domain.Submit.disabled=false;
			return false;	
		}
		
		if(trim1(domain.client.value)=='')
		{
			alert('Please select a client');
			domain.client.focus();
			domain.Submit.disabled=false;
			return false;	
		}
		return true;	
	}
	
	function validateSpace()
	{
		if(trim1(hosting.client.value)=='')
		{
			alert('Please select a client');
			hosting.client.focus();
			return false;	
		}
		return true;
	}
	
	function validateDb()
	{
		if(trim1(db.dbname.value)=='')
		{
			alert('Please provide database name');
			db.dbname.focus();
			return false;	
		}
		if(trim1(db.spacetype.value)=='0')
		{
			alert('Please select space');
			db.spacetype.focus();
			return false;	
		}
		if(trim1(db.client.value)=='')
		{
			alert('Please select client');
			db.client.focus();
			return false;	
		}
	}
	
	function validateAmc()
	{
		if(trim1(amc.amcfor.value)=='')
		{
			alert('Please provide details for amc');
			amc.amcfor.focus();
			return false;	
		}
		if(trim1(amc.client.value)=='')
		{
			alert('Please select client');
			amc.client.focus();
			return false;	
		}
	}
	
	function validateUser()
			{	
				if(addUsr.uid.value==""||addUsr.uid.value==" ")
				{
					alert('Please provide a user id');
					addUsr.uid.focus();
					return false;
				}
				if(addUsr.passwd.value==""||addUsr.passwd.value==" ")
				{
					alert('Please provide a password');
					addUsr.passwd.focus();
					return false;
				}
				if(addUsr.passwd_copy.value==""||addUsr.passwd_copy.value==" ")
				{
					alert('Please retype the password');
					addUsr.passwd_copy.focus();
					return false;
				}
				if(addUsr.passwd.value != addUsr.passwd_copy.value)
				{
					alert('password field are not matching');
					addUsr.passwd_copy.focus();
					return false;
				}
				if(addUsr.empcode.value==""||addUsr.empcode.value==" ")
				{
					alert('Please provide employee code of the user');
					addUsr.empcode.focus();
					return false;
				}
				if(addUsr.fname.value==""||addUsr.fname.value==" ")
				{
					alert('Please provide first name of the employee');
					addUsr.fname.focus();
					return false;
				}
				if(addUsr.lname.value==""||addUsr.lname.value==" ")
				{
					alert('Please provide last name of the employee');
					addUsr.lname.focus();
					return false;
				}
				if(addUsr.designation.value==""||addUsr.designation.value==" ")
				{
					alert('Please provide designation of the employee');
					addUsr.designation.focus();
					return false;
				}
				if(addUsr.email.value == "")
				{
					alert('Please enter a valid email address');
					addUsr.email.focus();
					return false;
				}
				if(validemailadd(addUsr.email.value)==false)
				{ 
					addUsr.email.focus();
					return false;
				}
				return true;
			}
			
	function validatePack(strSp)
	{
		if(trim1(pack.pname.value)=='')
		{
			alert('Please provide package name');
			pack.pname.focus();
			return false;
		}
		if(strSp=='1')
		{
			if(trim1(pack.space.value)=='')
			{
				alert('Please provide space');
				pack.space.focus();
				return false;
			}
		}
		if(trim1(pack.amt.value)=='')
		{
			alert('Please provide package amount');
			pack.amt.focus();
			return false;
		}
		if(IsNumeric(pack.amt.value)==false)
		{
			alert('Please provide only numeric values for amount field');
			pack.amt.focus();
			return false;	
		}
		return true;
	}
	
	function validateBill()
	{
		if(bill.reportType[4].checked==true && trim1(bill.txtBillNoFrom.value)=="")
		{
			alert('Please provide billNo');
			bill.txtBillNoFrom.focus();
			return false;
		}
		if(IsNumeric(bill.txtBillNoFrom.value)==false)
		{
			alert('Please provide only numeric values for billNo');
			bill.txtBillNoFrom.focus();
			return false;
		}
		return true;	
	}
	
	function validateTruckM()
	{
		if(trim1(truckmaintenance.txtDateFrom.value)=="")
		{
			alert('Please provide start date');
			truckmaintenance.txtDateFrom.focus();
			return false;
		}
		
		if(trim1(truckmaintenance.txtDateTo.value)=="")
		{
			alert('Please provide end date');
			truckmaintenance.txtDateTo.focus();
			return false;
		}
		return true;	
	}
	
	function validateParty()
	{
		if(trim1(partylist.ddlGroup.value)=="")
		{
			alert('Please select a group');
			partylist.ddlGroup.focus();
			return false;
		}
		return true;
	}
	
	function validatePartyLedger()
	{
		if(trim1(partyledger.ddlParty.value)=="")
		{
			alert('Please select a ledger');
			partyledger.ddlParty.focus();
			return false;
		}
		return true;	
	}
	
	function validateGroupSummary()
	{
		if(trim1(groupsummary.ddlGroup.value)=="")
		{
			alert('Please select a group');
			groupsummary.ddlGroup.focus();
			return false;
		}
		return true;
	}
