// JavaScript Document

var catChecked = false;

function check_ajax(ids)
{
	if (catChecked) return; // ❗ prevent repeated checking
	//alert(ids);
	document.getElementById("lod").innerHTML = '<img src="images/elements/loaders/loader.gif" />'
	//var ids = document.getElementById("cat_name").value;
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			var res = xmlhttp.responseText;
			//alert(res);
			if(res.trim()=="yes")
			{
				 catChecked = true;  // prevent infinite loop
				alert("This Company Name is already Exist");	
				document.getElementById("lod").innerHTML ='';
				document.getElementById("c_name").value ='';
				document.getElementById("c_name").focus();
				return false;
			}
			else
			{
				document.getElementById("lod").innerHTML ='';
			}
		}
	}
	
	var qurl ="?ajax_id=1&vals=" + ids
	//alert(qurl);
	xmlhttp.open("GET","ajax/party_ajax.php"+qurl,true);
	xmlhttp.send();	
}
//p_code,c_name,con_nm1,des1,mobile1,ln_no1,of_add,co_add,e_id1,w_url,t_no,cst_no,tn_date
function redir_pg(party_id,cat_id)
{
	//window.location='party_cost_log_list.php?party_id='+party_id+'&cate_id='+cat_id;
	$("#main_div").load("party_cost_log_list.php?party_id="+party_id+"&cate_id="+cat_id,function(response)
	{});
}
function fom_validate()
{
	var letters=/^[A-Za-z][A-Za-z. ]+$/; 
	if(document.getElementById('c_name').value=='')
	{
		alert('Please Enter the Company Name');  
		document.getElementById('c_name').focus();  
		return false; 
	}
	/*else if(letters.test(document.getElementById('c_name').value)==false)  
	{
		alert('Company Name must have Alphabets only');  
		document.getElementById('c_name').focus();  
		return false;  
	}*/
	
	var letters=/^[A-Za-z][A-Za-z. ]+$/; 
	if(document.getElementById('con_nm1').value=='')
	{
		alert('Please Enter the Contact Person Name 1');  
		document.getElementById('con_nm1').focus();  
		return false; 
	}
	else if(letters.test(document.getElementById('con_nm1').value)==false)  
	{
		alert('Contact Person Name 1 must have Alphabets only');  
		document.getElementById('con_nm1').focus();  
		return false;  
	}
	
	var letters=/^[A-Za-z][A-Za-z. ]+$/; 
	if(document.getElementById('des1').value=='')
	{
		alert('Please Enter the Contact Person 1 Designation');  
		document.getElementById('des1').focus();  
		return false; 
	}
	else if(letters.test(document.getElementById('des1').value)==false)  
	{
		alert('Contact Person Designation 1 must have Alphabets only');  
		document.getElementById('des1').focus();  
		return false;  
	}
	
	var letters = /^[0-9]+$/;  
	if(document.getElementById('mobile1').value=='')
	{
		alert('Please Enter the Con Person 1 Mobile No');  
		document.getElementById('mobile1').focus();
		return false; 
	}
	else if(letters.test(document.getElementById('mobile1').value)==false)  
	{
		alert('Con Person 1 Mobile No must have numeric');  
		document.getElementById('mobile1').focus();  
		return false;  
	}
var m1 = document.getElementById('mobile1').value;
	
	if (!/^[0-9]{10}$/.test(m1)) {
    alert('Mobile number must be exactly 10 digits');
    document.getElementById('mobile1').focus();
    return false;
}

	
	var letters = /^[0-9]\d{2,4}-\d{6,8}$/;  
	if(document.getElementById('ln_no1').value!='')
	{
		/*alert('Please Enter the LandLine No 1');  
		document.getElementById('mobile1').focus();  
		return false; */
		if(letters.test(document.getElementById('ln_no1').value)==false)  
		{
			alert('Landline No 1 is not Valide');  
			document.getElementById('ln_no1').focus();  
			return false;  
		}
	}
	
//	var letters 	= /^[a-zA-Z0-9][a-zA-Z0-9_.-]+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/; 
	var letters 	= /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

	if(document.getElementById('e_id1').value=='')    
	{
		alert('Please Enter the Email ID 1');  
		document.getElementById('e_id1').focus();  
		return false;
	}
	else if(letters.test(document.getElementById('e_id1').value)==false)  
	{
		alert('Email ID 1 is not valid');  
		document.getElementById('e_id1').focus();  
		return false;  
	}

	
	var letters=/^[A-Za-z][A-Za-z. ]+$/; 
	if(document.getElementById('con_nm2').value!='')
	{
		
		/*alert('Please Enter the Contact Person Name 1');  
		document.getElementById('con_nm2').focus();  
		return false; */
		if(letters.test(document.getElementById('con_nm2').value)==false)  
		{
			alert('Contact Person Name 2 must have Alphabets only');  
			document.getElementById('con_nm2').focus();  
			return false;  
		}
		
		var letters=/^[A-Za-z][A-Za-z. ]+$/; 
		if(document.getElementById('des2').value=='')
		{
			alert('Please Enter the Contact Person 2 Designation');  
			document.getElementById('des2').focus();  
			return false; 
		}
		else if(letters.test(document.getElementById('des2').value)==false)  
		{
			alert('Contact Person Designation 2 must have Alphabets only');  
			document.getElementById('des2').focus();  
			return false;  
		}
		
		var letters = /^[0-9]+$/;  
		if(document.getElementById('mobile2').value=='')
		{
			alert('Please Enter the Con Person 2 Mobile No');  
			document.getElementById('mobile2').focus();  
			return false; 
		}
		else if(letters.test(document.getElementById('mobile2').value)==false)  
		{
			alert('Con Person 2 Mobile No must have numeric');  
			document.getElementById('mobile2').focus();  
			return false;  
		}		
	}	
	var letters = /^[0-9]\d{2,4}-\d{6,8}$/;  
	if(document.getElementById('ln_no2').value!='')
	{
		/*alert('Please Enter the LandLine No 1');  
		document.getElementById('mobile1').focus();  
		return false; */
		if(letters.test(document.getElementById('ln_no2').value)==false)  
		{
			alert('Landline No 2 is not Valide');  
			document.getElementById('ln_no2').focus();  
			return false;  
		}
	}
	
/*	var letters 	= /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if(document.getElementById('e_id2').value!='')    
	{
		
		if(letters.test(document.getElementById('e_id2').value)==false)  
		{
			alert('Email ID 2 is not valid');  
			document.getElementById('e_id2').focus();  
			return false;  
		} 
	}*/	
	
	var letters=/^[A-Za-z][A-Za-z. ]+$/; 
	if(document.getElementById('con_nm3').value!='')
	{
		
		/*alert('Please Enter the Contact Person Name 1');  
		document.getElementById('con_nm2').focus();  
		return false; */
		if(letters.test(document.getElementById('con_nm3').value)==false)  
		{
			alert('Contact Person Name 3 must have Alphabets only');  
			document.getElementById('con_nm3').focus();  
			return false;  
		}
		
		var letters=/^[A-Za-z][A-Za-z. ]+$/; 
		if(document.getElementById('des3').value=='')
		{
			alert('Please Enter the Contact Person 3 Designation');  
			document.getElementById('des3').focus();  
			return false; 
		}
		else if(letters.test(document.getElementById('des3').value)==false)  
		{
			alert('Contact Person Designation 3 must have Alphabets only');  
			document.getElementById('des3').focus();  
			return false;  
		}
		
		var letters = /^[0-9]+$/;  
		if(document.getElementById('mobile3').value=='')
		{
			alert('Please Enter the Con Person 3 Mobile No');  
			document.getElementById('mobile3').focus();  
			return false; 
		}
		else if(letters.test(document.getElementById('mobile3').value)==false)  
		{
			alert('Con Person 3 Mobile No must have numeric');  
			document.getElementById('mobile3').focus();  
			return false;  
		}
	}	
	var letters = /^[0-9]\d{2,4}-\d{6,8}$/;  
	if(document.getElementById('ln_no3').value!='')
	{
		/*alert('Please Enter the LandLine No 1');  
		document.getElementById('mobile1').focus();  
		return false; */
		if(letters.test(document.getElementById('ln_no3').value)==false)  
		{
			alert('Landline No 3 is not Valide');  
			document.getElementById('ln_no3').focus();  
			return false;  
		}
	}
	
/*	var letters 	= /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if(document.getElementById('e_id3').value!='')    
	{
		
		if(letters.test(document.getElementById('e_id3').value)==false)  
		{
			alert('Email ID 3 is not valid');  
			document.getElementById('e_id3').focus();  
			return false;  
		} 
	}*/
	
	var letters_add1 = /^\s+$/;  
	if(document.getElementById('of_add').value=='')
	{
		alert('Please Enter the Office Address');  
		document.getElementById('of_add').focus();  
		return false;
	}
	else if(document.getElementById('of_add').value!='')
	{
		var addr = document.getElementById('of_add').value;
		var len = addr.length;
		if(letters_add1.test(document.getElementById('of_add').value)==true)  
		{
			alert("Office Address Can't Start with Spaces");  
			document.getElementById('of_add').focus();  
			return false;  
		}		
		else if(len < 10)
		{
			alert('Office Address Should be Greater than 10 Letters');  
			document.getElementById('of_add').focus();  
			return false;
		}
	}
	var letters_add2 = /^\s+$/; 
	if(document.getElementById('co_add').value=='')
	{
		alert('Please Enter the Courier Address');  
		document.getElementById('co_add').focus();  
		return false;
	}
	else if(document.getElementById('co_add').value!='')
	{
		var addr = document.getElementById('co_add').value;
		var len = addr.length;
		if(letters_add2.test(document.getElementById('co_add').value)==true)  
		{
			alert("Courier Address Can't Start with Spaces");  
			document.getElementById('co_add').focus();  
			return false;  
		}
		else if(len < 10)
		{
			alert('Courier Address Should be Greater than 10 Letters');  
			document.getElementById('co_add').focus();  
			return false;
		}
	}	
	
	if(document.getElementById('of_country').value=='')
	{
		alert('Please Enter Party Country');  
		document.getElementById('of_country').focus();  
		return false;
	}
	

	if(document.getElementById('of_country').value==1)
	{	
		if(document.getElementById('of_state').value=='')
		{
			alert('Please Enter Party State');  
			document.getElementById('of_state').focus();  
			return false;
		}
	
		if(document.getElementById('of_city').value=='')
		{
			alert('Please Enter Party Area');  
			document.getElementById('of_city').focus();  
			return false;
		}	
		
		if(document.getElementById('of_town').value=='')
		{
			alert('Please Enter Party Street');  
			document.getElementById('of_town').focus();  
			return false;
		}
	}
	
//	var letters=/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/; 
//	if(document.getElementById('w_url').value!='')    
//	{
//		/*alert('Please Enter the Email ID 1');  
//		document.getElementById('e_id').focus();  
//		return false;*/
//		if(letters.test(document.getElementById('w_url').value)==false)  
//		{
//			alert('Website URL is not valid');  
//			document.getElementById('w_url').focus();  
//			return false;  
//		} 
//	}
	

	if(document.getElementById("courier_charges1").checked == false && document.getElementById("courier_charges2").checked == false)
	{
			alert('Please Select (Yes / No) for Courier Charges !!');
			document.getElementById("courier_charges1").focus();
			return false;
	}	
	
//	var letters = /^[0-9]{1,10}$/;  
	if(document.getElementById('gst_no').value=='')
	{
		alert('Please Enter the GST No');  
		document.getElementById('gst_no').focus();  
		return false; 
	}
	else if(document.getElementById('gst_no').value!='')
	{
		var gst_no_length = document.getElementById('gst_no').value;
		var len_gst_no	  = gst_no_length.length;

		if(len_gst_no < 15)
		{
			alert('GST No. should be 15 Digit');  
			document.getElementById('gst_no').focus();  
			return false;
		}
	}
	
	
	
/*	else if(letters.test(document.getElementById('gst_no').value)==false)  
	{
		alert('CST No must have numeric and exactly 7 digits');  
		document.getElementById('gst_no').focus();  
		return false;  
	}*/
	
/*	var letters = /^[0-9]{1,20}$/;  
	if(document.getElementById('t_no').value=='')
	{
		alert('Please Enter the Tin No');  
		document.getElementById('t_no').focus();  
		return false; 
	}
	else if(letters.test(document.getElementById('t_no').value)==false)  
	{
		alert('Tin No must have numeric and exactly 11 digits');  
		document.getElementById('t_no').focus();  
		return false;  
	}*/
			
	/*var letters=/^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/;
	if(document.getElementById('tn_date').value=='')
	{
		alert('Please Enter the Date');  
		document.getElementById('tn_date').focus();  
		return false; 
	}
	else if(letters.test(document.getElementById('tn_date').value)==false)  
	{
		alert('Date must be in the given Format');  
		document.getElementById('tn_date').focus();  
		return false;  
	}*/
	
	var totvl = document.getElementById('tot_cn').value;	
	for(i=1;i< totvl; i++)
	{
		var letters=/^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/;
		if(document.getElementById('fmdate'+i).value!='')
		{
			if(letters.test(document.getElementById('fmdate'+i).value)==false)  
			{
				alert('From Date must be in the given Format');  
				document.getElementById('fmdate'+i).focus();  
				return false;  
			}
			/*var letters=/^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/;
			if(document.getElementById('todate'+i).value!='')
			{
				if(letters.test(document.getElementById('todate'+i).value)==false)  
				{
					alert('To Date must be in the given Format');  
					document.getElementById('todate'+i).focus();  
					return false;  
				}
			}*/
			var letters=/^\d+(?:\.\d+)?$/;
			if(document.getElementById('cost_vl'+i).value=='')
			{
				alert('Please Enter the Cost'+i);  
				document.getElementById('cost_vl'+i).focus();  
				return false; 
			}
			else if(letters.test(document.getElementById('cost_vl'+i).value)==false)  
			{
				alert('Cost Should have only Digits of row '+i);  
				document.getElementById('cost_vl'+i).focus();  
				return false;  
			}
			
		}	
		
		var letters=/^\d+(?:\.\d+)?$/;
		if(document.getElementById('cost_vl'+i).value!='')
		{
			/*alert('Please Enter CAM Weight');  
			document.getElementById('cost_vl').focus();  
			return false; */
			if(letters.test(document.getElementById('cost_vl'+i).value)==false)  
			{
				alert('Cost Should have only Digits of row '+i);  
				document.getElementById('cost_vl'+i).focus();  
				return false;  
			}
			else
			{
				var letters=/^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/;
				if(document.getElementById('fmdate'+i).value=='')
				{
					alert('Please Enter the From Date');  
					document.getElementById('fmdate'+i).focus();  
					return false; 
				}
				else if(letters.test(document.getElementById('fmdate'+i).value)==false)  
				{
					alert('From Date must be in the given Format');  
					document.getElementById('fmdate'+i).focus();  
					return false;  
				}
	
				/*var letters=/^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/;
				if(document.getElementById('todate'+i).value!='')
				{
					
				}
				else 
					if(letters.test(document.getElementById('todate'+i).value)==false)  
					{
						alert('To Date must be in the given Format');  
						document.getElementById('todate'+i).focus();  
						return false;  
					}
				}*/
				
			}
		}
	}
			
	
	var where_to= confirm("Do you really want to Submit the Data?");
	if (where_to== true)
	{
		document.fm.submit();
	}
	else
	{
		return false;	
	}
}

function copy_addr()
{
	document.getElementById('co_add').value = '';
	var addr = document.getElementById('of_add').value;  	
	document.getElementById('co_add').value = addr;
}

function form_rest() //this is for reset the forms
{
	var where_to= confirm("Do you really want to Reset the Data?");
	if (where_to== true)
	{
		window.location.reload();
	}
	else
	{
		return false;
	}
}

function foc(ids)
{
	document.getElementById(ids).style.border="2px solid #0099FF";
	
}

function stnon(ids)
{
	document.getElementById(ids).style.border="thin solid #CCCCCC";
}