var rCount = 0;

function multiplyRow() {
    rCount++;
	if (!checkErrors()) {
		return;
	}
	jQuery("#calculate").append("<tr id='trCalculate_"+rCount+"'>"+jQuery("#baseLine").html()+"<td><input type='button' value='Sterge vehicul' onclick='jQuery(\"#trCalculate_"+rCount+"\").remove();jQuery(\"#error\").html(\"\");'></td></tr>");
	jQuery("#error").html("");
	jQuery("#calcRes").html("");
}

function isFloat(str){ 
    str = jQuery.trim(str);
    return /^[-+]?[0-9]+(\.[0-9]+)?$/.test(str);
}

function leadingZeros(s){
	if (s.charAt(0) == "0") return true;
	else return false;
}

function leadingZeros2(s){
	if (s == "0") return false;
	if (s.charAt(0) == "0") return true;
	else return false;
}

function validEmail(fld) {
    var tfld = jQuery.trim(fld);
    var emailFilter=/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*%40([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
       
    if (fld == "") {
        return false;
    } 
	else if (!emailFilter.test(tfld)) {              
        return false;
    } 
	else {
        return true;
    }
}

function checkEmail() {
	var str3 = jQuery("#calculateForm").serialize();
	var arrayStr = str3.split("&");
	for(var i = 0; i < arrayStr.length; i++){		
		temp3 = arrayStr[i].split("=");
		if((temp3[0] == 'Email') && !validEmail(temp3[1])){
			jQuery("#error").html("<big>"+em1er+"</big>");
			return false;
		}
	}
	return true;
}

function checkErrors() {
	
	var str1 = jQuery("#calculateForm").serialize();
	var arrInit1 = new Array();
	var arrRes1 = new Array();
	var temp1 = new Array();
	var arrInit1 = str1.split("&");
	var k = 0;
	jQuery("#calcRes").html("");
	for(var i = 0; i < arrInit1.length; i++){		
		temp1 = arrInit1[i].split("=");
		if((!isFloat(temp1[1])) && (temp1[0] != 'Email')){
			jQuery("#error").html("<big>"+errr1+"</big>");			
			return false;
		}
		if((temp1[1] == "0") && (temp1[0] != 'cheltuieliAdditionale') && (temp1[0] != 'Email')){
			jQuery("#error").html("<big>"+errr3+"</big>");			
			return false;
		}
		if((leadingZeros(temp1[1])) && (temp1[0] != 'cheltuieliAdditionale') && (temp1[0] != 'Email')){
			jQuery("#error").html("<big>"+errr2+"</big>");			
			return false;
		}
		if((leadingZeros2(temp1[1])) && (temp1[0] == 'cheltuieliAdditionale')){
			jQuery("#error").html("<big>"+errr2+"</big>");			
			return false;
		}
	}
	return true;
}

function goSubmit() {

		if (!checkErrors()) {
			return;
		}
		
		if (!checkEmail()) {
			return;
		}	
		jQuery("#dialog2").dialog('open');
		jQuery("#error").html("");
		jQuery("#calcRes").html("");
		var str2 = jQuery("#calculateForm").serialize();	
		var arrInit2 = new Array();	
		var arrRes2 = new Array();	
		var temp2 = new Array();
		
		var arrInit2 = str2.split("&");		
		var k = 0;
		for(var i = 0; i < arrInit2.length; i++){		
			temp2 = arrInit2[i].split("=");
			if (temp2[0] == 'Email'){
				arrRes2[i] = temp2[0]+"="+temp2[1];
				continue;
			}
			arrRes2[i] = temp2[0]+"_"+k+"="+temp2[1];			
			if (temp2[0] == 'cheltuieliAdditionale'){
				k++;
			}								
		}	
		str2 = arrRes2.join("&");
		
		jQuery.post(ajaxUrl+"&type=345", str2, function(data){
			jQuery("#calcRes").html("<big>"+done1+" : "+"<b class='red'>"+data+" RON</b></big></br>");		
			jQuery("#addInfo").show();
			jQuery("#dialog2").dialog('close');
		});
	
}