document.write("<script type='text/javascript' src='validation.js'></"+"script>");

var xmlHttp
var action
var div
var check=0;
var element;

function getNewTotal(gRowId){
	var totalamout=document.getElementById("qty"+gRowId).value*document.getElementById("unitPrise"+gRowId).value;
	document.getElementById("total"+gRowId).value=totalamout;
}
function getTotalAmount(gRowId){
	var i=0;
	
	var wholetotal=0;
	for(i=1; i<=gRowId; i++){
		
		wholetotal=wholetotal+Number(document.getElementById("total"+i).value);
		
	}
	document.getElementById("totalAmount").value= wholetotal;
	
}
function addRow(key)
{
	var gRowId=0;
	
	if(key.keyCode==13){
		var temp=Number(document.getElementById('numRow').value);
		
		if(trim(document.getElementById("description"+temp).value).length>0)
		{
			
			gRowId = Number(document.getElementById('numRow').value)+1;
			
			document.getElementById('numRow').value = gRowId;
			
			var tbody = document.getElementById('tblGrid').getElementsByTagName('tbody')[0]; 
			var row = document.createElement('TR'); 
			
			var cell1 = document.createElement('TD'); 
			cell1.bgcolor="#CFE2EB"
			cell1.align="center" 
			
			var cell2 = document.createElement('TD'); 
			cell2.align="center"
			var cell3 = document.createElement('TD'); 
			var cell4 = document.createElement('TD');
			var cell5 = document.createElement('TD');
			var cell6 = document.createElement('TD');
			cell3.align="center"
			cell4.align="center"
			cell5.align="center"
			cell6.align="center"
			
			cell1.innerHTML = "<input type='text'  size='15' name='grow' id='grow' onKeyPress=nextFocus(event,1) class='input' value='"+gRowId+"' readonly='yes'>"
			cell2.innerHTML = "<input type='text' id='description"+gRowId +"' name='description"+gRowId+"' size='38' onKeyPress=nextFocus(event,1) class='input'>"
			cell3.innerHTML = "<input type='text' id='qty"+gRowId +"' name='qty"+gRowId+"' size='9' onKeyPress='nextFocus(event,2)' class='input'>"
			cell4.innerHTML = "<input type='text' id='unit"+gRowId +"' name='unit"+gRowId+"' size='9' onKeyPress='nextFocus(event,3)' class='input'>" 
			cell5.innerHTML = "<input type='text' id='unitPrise"+gRowId +"' name='unitPrise"+gRowId+"' size='18' onKeyPress='nextFocus(event,4)' class='input' onBlur='getNewTotal("+gRowId+")'>"
			
			cell6.innerHTML = "<input type='text' id='total"+gRowId +"' name='total"+gRowId+"' size='18' onKeyPress='addRow(event)' class='input' onBlur='getTotalAmount("+gRowId+")'>"
			
			
			row.appendChild(cell1); 
			row.appendChild(cell2); 
			row.appendChild(cell3); 
			row.appendChild(cell4); 
			row.appendChild(cell5); 
			row.appendChild(cell6); 
			tbody.appendChild(row); 
		}
		else{
			alert("Please enter description of goods");
		}
		if(trim(document.getElementById("description"+temp).value).length>0)
			document.getElementById("description"+gRowId).focus();
		
				
	return false;
}
}
function nextFocus(key,mode)
{
	if(key.keyCode==13){
		var cellNo = Number(document.getElementById('numRow').value);	
		if(mode==1)
			document.getElementById('qty'+cellNo).focus();	
		if(mode==2)
			document.getElementById('unit'+cellNo).focus();	
		if(mode==3)
			document.getElementById('unitPrise'+cellNo).focus();
		if(mode==4)
			document.getElementById('total'+cellNo).focus();	
	}

}


function validateForm()
{
if(trim(document.getElementById("clientName").value).length==0)
{

alert("Please enter client name");
document.getElementById("clientName").focus();
return false;
}

if(document.getElementById("email").value=="")
{
alert("please enter email id");
document.getElementById("email").focus();
return false;
}
if(trim(document.getElementById("email").value)!="")
{
var checkEmail=isEmail(document.getElementById("email").value);
if(checkEmail==false)
{
	alert("Please Enter a Valid Email");
	document.getElementById("email").focus();
	return false;
}
}
if(document.getElementById("phone").value=="")
{
alert("please enter Phone Number");
document.getElementById("phone").focus();
return false;
}

var checkPhone=ValidatePhone(document.getElementById("phone").value);
if(checkPhone==false)
{
	document.getElementById("phone").focus();
	return false;
}
if(document.getElementById("date").value=="")
{
alert("please enter date");
document.getElementById("date").focus();
return false;
}
if(document.getElementById("date").value.length>0){
		if(!(isDate(document.getElementById("date").value))){
			document.getElementById("date").focus();
			return false;
		}
	}

return true;
}
