

var states = new Array("Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Antarctica", 
"Antigua and Barbuda", "Argentina", "Armenia", "Australia", "Austria", "Azerbaijan", "Bahamas", 
"Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", 
"Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei", "Bulgaria", "Burkina Faso", "Burma", "Burundi", 
"Cambodia", "Cameroon", "Canada", "Cape Verde", "Central African Republic", "Chad", "Chile", "China", 
"Colombia", "Comoros", "Congo, Democratic Republic", "Congo, Republic of the", "Costa Rica", "Cote d'Ivoire", 
"Croatia", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "East Timor", 
"Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Fiji", "Finland", "France",
 "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Greenland", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel","Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, North", "Korea, South", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Mauritania", "Mauritius", "Mexico", "Micronesia", "Moldova", "Mongolia", "Morocco", "Monaco", "Mozambique", "Namibia", "Nauru", "Nepal", "Netherlands", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Norway", "Oman", "Pakistan", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Qatar", "Romania", "Russia", "Rwanda", "Samoa", "San Marino", " Sao Tome", "Saudi Arabia", "Senegal", "Serbia and Montenegro", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "Spain", "Sri Lanka", "Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Togo","Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", 
"United States", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", "Vietnam", "Yemen", "Zambia", "Zimbabwe");


var dates = new Array("All Dates", "Custom", "Today", "This week", "This week-to-date", "This Month", "This Month-to-date","This quarter", "This quarter-to-date", "This year", "This year-to-date", "Yesterday", "Recent", "Last week", "Last week-to-date", "Last month", "Last month-to-date", "Last quarter", "Last quarter-to-date", "Last year", "Last year-to-date", "Since one month ago", "Since two months ago", "Since three months ago", "Since one year ago", "Next week", "Next 4 weeks", "Next Month", "Next quarter", "Next year");

var dates1 = new Array("All Dates", "Custom", "Today", "This week", "This Month", "This quarter", "This year", "Yesterday", "Recent - last 4 days", "Last week", "Last month", "Last quarter", "Last year", "Next week", "Next Month", "Next quarter", "Next year");

var trtype = new Array("Select Type", "Credit Purchase", "Cash Purchase", "Check", "Vendor Prepayment", "Vendor Refunds", "Deposit", "Journal","Invoice", "Sales Receipt", "Transfer Funds","customer prepayment","Credit Card Charges","Credit Card Credit");


function checkMailId(mailids)
{
	var arr = new Array('.com','.net','.org','.biz','.coop','.info','.museum','.name','.pro'
	,'.edu','.gov','.int','.mil','.ac','.ad','.ae','.af','.ag','.ai','.al',
	'.am','.an','.ao','.aq','.ar','.as','.at','.au','.aw','.az','.ba','.bb',
	'.bd','.be','.bf','.bg','.bh','.bi','.bj','.bm','.bn','.bo','.br','.bs',
	'.bt','.bv','.bw','.by','.bz','.ca','.cc','.cd','.cf','.cg','.ch','.ci',
	'.ck','.cl','.cm','.cn','.co','.cr','.cu','.cv','.cx','.cy','.cz','.de',
	'.dj','.dk','.dm','.do','.dz','.ec','.ee','.eg','.eh','.er','.es','.et',
	'.fi','.fj','.fk','.fm','.fo','.fr','.ga','.gd','.ge','.gf','.gg','.gh',
	'.gi','.gl','.gm','.gn','.gp','.gq','.gr','.gs','.gt','.gu','.gv','.gy',
	'.hk','.hm','.hn','.hr','.ht','.hu','.id','.ie','.il','.im','.in','.io',
	'.iq','.ir','.is','.it','.je','.jm','.jo','.jp','.ke','.kg','.kh','.ki',
	'.km','.kn','.kp','.kr','.kw','.ky','.kz','.la','.lb','.lc','.li','.lk',
	'.lr','.ls','.lt','.lu','.lv','.ly','.ma','.mc','.md','.mg','.mh','.mk',
	'.ml','.mm','.mn','.mo','.mp','.mq','.mr','.ms','.mt','.mu','.mv','.mw',
	'.mx','.my','.mz','.na','.nc','.ne','.nf','.ng','.ni','.nl','.no','.np',
	'.nr','.nu','.nz','.om','.pa','.pe','.pf','.pg','.ph','.pk','.pl','.pm',
	'.pn','.pr','.ps','.pt','.pw','.py','.qa','.re','.ro','.rw','.ru','.sa',
	'.sb','.sc','.sd','.se','.sg','.sh','.si','.sj','.sk','.sl','.sm','.sn',
	'.so','.sr','.st','.sv','.sy','.sz','.tc','.td','.tf','.tg','.th','.tj',
	'.tk','.tm','.tn','.to','.tp','.tr','.tt','.tv','.tw','.tz','.ua','.ug',
	'.uk','.um','.us','.uy','.uz','.va','.vc','.ve','.vg','.vi','.vn','.vu',
	'.ws','.wf','.ye','.yt','.yu','.za','.zm','.zw');
	var mai = mailids;
	var val = true;

	var dot = mai.lastIndexOf(".");
	var ext = mai.substring(dot,mai.length);
	var at = mai.indexOf("@");


	if(dot > 5 && at >1)
	{
		for(var i=0; i<arr.length; i++)
		{
			if(ext == arr[i])
			{
				val = true;
				break;
			}
			else
			{
				val = false;
			}
		}
		if(val == false)
		{
		    inlinemsg('email','Enter a valid Email id','text'); 
			return false;
		}
	}
	else
	{
	    inlinemsg('email','Enter a valid Email id','text'); 
		return false;
	}
return true;
}

/* Function to check the due days field in the addcust page*/

function termcheck()
{
var newterm=document.add.newterm.value;
var s = newterm.toString();
if(s==0)
  {
   inlinemsg('dcol1','Enter valid duedays','text'); 
   document.add.newterm.value = "";		
  }
  else{
    if(document.getElementById('msgcontentdcol1')!=null)
      hidemsg('dcol1');
      }
         for (i = 0; i < s.length; i++)
         {
         var c = s.charAt(i);
         if (isNaN(c)) 
	   {
	    inlinemsg('dcol1','Enter valid duedays','text'); 
		document.add.newterm.value = "";	
	   }
	  else{
        if(document.getElementById('msgcontentdcol1')!=null)
           hidemsg('dcol1');
         }
      }

}


function check(c)
{
	
	var eem    = document.add.vemail.value;
	var name   = document.add.custname.value;
	var cname  = document.add.chkname.value;
	var ph     = document.add.phone.value;
	var add    = document.add.address.value;
	var sadd   = document.add.shipadd.value;
      var st     = document.add.state.value;
	var ct     = document.add.city.value;
	var zp     = document.add.zip.value;
	var cr     = document.add.creditlimit.value;
	var fx     = document.add.fax.value;
	//	if(document.getElementById('msgcontentss')!=null)
	  hidemsg('ss');
	if(name == "" ||cname == "" || add == "")
	{
	    inlinemsg('ss','Name,Check Name,Address should not be empty!','');
		return false;
	}
	if(!isNaN(name))
	{
	  inlinemsg('custname','Enter a valid name','text');
	 return false;
	}
	if (!isNaN(cname))	
	{
	  inlinemsg('chkname','Check Name should not be a number!','text');
	  document.add.chkname.focus();
	  return false;
	}
      if(add.length > 100)
	{
	  inlinemsg('address','Address is too long(above 100 chars)!','text');
	  document.add.address.focus();
	  return false;  
	}
      if(sadd.length > 100)
	{
	  inlinemsg('shipadd','Address is too long(above 100 chars)!','text');
	  document.add.address.focus();
	  return false;  
	}
	if(st!='' && !isNaN(st))
	{
	  inlinemsg('state','State should not be a number','text');
	 return false;
	}
	if(ct!='' && !isNaN(ct))
	{
	  inlinemsg('city','City should not be a number','text');
	 return false;
	}

	if(eem!='' && !checkMailId(eem))
	{
		return false;
	}
	for(var k=0; k<ph.length; k++)
	{
	  var alphaa = ph.charAt(k);
	  var hh = alphaa.charCodeAt(0);
	  if((hh > 42 && hh<46) || (hh > 47 && hh<58))
	  {
	  }
	  else
	  {
	    inlinemsg('phone','Enter a valid Phone no (e.g: 1234567 (or) +91-011-4444444)','text');
		return false;
	  }
	}
	
	for(var k=0; k<fx.length; k++)
	{
	  var alphaa = fx.charAt(k);
	  var hh = alphaa.charCodeAt(0);
	  if((hh > 42 && hh<46) || (hh > 47 && hh<58))
	  {
	  }
	  else
	  {
	    inlinemsg('fax','Enter a valid Fax no (e.g: 1234567 (or) +91-011-4444444)','text');
		return false;
	  }
	}
	
	if(cr!='')
	{
	var xx = isNaN(cr);
	    if((xx) || (cr<0))
	      {
	        inlinemsg('crdlimit','Enter valid credit limit.','text');
		return false;
	      }
	}
	if(c)
	{
	  var terms  = document.add.terms.value;
	  var cusbal = document.add.cusbalance.value;
	  var dat    = document.add.balancedate.value;
        var crdt   = document.add.creditlimit.value;
	  for(var k=0; k<terms.length; k++)
	  {
	    var alphaa = terms.charAt(k);
	    var hh = alphaa.charCodeAt(0);
	    if((hh > 42 && hh<46) || (hh > 47 && hh<58))
	      {
	      }
	    else
	      {
		    inlinemsg('dcol1','Enter valid terms.','text');	
		return false;
	      }	
	  }
	  if(cusbal!='')
	  {
	    var yy = isNaN(cusbal);
	    if((yy) || (cusbal==0))
	     {
	       inlinemsg('cusbal','Enter valid customer balance.','text');
		return false;
	     }
	
	    if(cusbal!=0 && dat =="")
		{
		    inlinemsg('fdate','Date cannot be empty for an entered balance','text');
		 	return false; 	 
		}
          if(crdt!='' && crdt!=0 && cusbal > crdt) 
		 {
		   inlinemsg('cusbal','Customer balance should not exceed the credit limit.','text');
		return false;
	     }
			
	   }
	   var ss=document.add.newterm.value;
	   if(ss>150)
	    {
	      inlinemsg('dcol1','Due days could not exceed 150 days','text');
	    return false;
	    }
	}

//	if(document.getElementById('msgcontentfdate')!=null)
	  hidemsg('fdate');  				
return true;
}


function specialcharcheck(s,id){
   var ss=document.getElementById(id);
   SpecialChar='No';
   if(id=='address' || id=='shipadd'){
    spchar="`\\~!^+\"|%:=<>;?";
	var spchars =" `\\ ~ ! ^ + \" | : =   < > ; ?"; 
	}
   else if(id=='custname' || id=='chkname'){
    spchar="`\\~!^+\"|%:=<>;?";
	var spchars =" `\\ ~ ! ^ + \" | : =   < > ; ?"; 
	}	
  else if(id=='fax'){
    spchar="`()(\\~!@^&*\"|%:=,<>$;?{}[]#/";
	  var spchars =" ` ( )  \\ ~ ! @ ^ & * \" | : = , < > $ ; ? {} [] # /"; 

	}	
   else{
	  spchar="`()(\\~!@^&*+\"|%:=,<>$;?{}[]-#/";
	  var spchars =" ` ( )  \\ ~ ! @ ^ & * + \" | : =  , < > $ ; ? {} [] - # /"; 
	}
	for(var i=0; i< s.length; i++)
	{
		for(var j=0; j < spchars.length; j++)
		{			
			if(s.charAt(i)== spchar.charAt(j))
			{			
				SpecialChar='Yes';
				break;
			}
		
		}		
	}
	if (SpecialChar == 'Yes')
	{
	    inlinemsg(id,'Special characters are not accepted','text');
		ss.value="";
		return false;
	}
	// if(document.getElementById('msgcontent'+id)!=null)
	     hidemsg(id);
	return true;
}

function amtcheck(val,id){
  
   var vid=document.getElementById(id);
   var ss=val.indexOf(".");
	   if(ss==-1){
	     if(val.length>11){
	       inlinemsg('amount','Amount exceeds maximum length','text');
	     vid.value="";
	     return false;
	     }
		}
	   else if(ss!=-1){
	     var st=val.split(".");
	     if(st[0].length>11){
	       inlinemsg('amount','Amount exceeds maximum length','text');
		   vid.value="";
	       return false;
		}
	   if(st[1].length>2){
	       inlinemsg('amount','Amount should have 2 decimal values','text');
		   vid.value="";
	       return false;
		}
		
     }
     
	 //  if(document.getElementById('msgcontent'+id)!=null)
	     hidemsg(id);

	return true;	
}


function isSpl(obj) {

	var name = obj.value;

	for(var k=0; k<name.length; k++) {
	    var hh = name.charCodeAt(k);
	    if(!((hh >= 64 && hh<=90 ) || (hh >= 97 && hh<=122) || (hh > 37 && hh<58) ||(hh==32)||(hh==35)||(hh==36)||(hh==95)||(hh==123)||(hh==125)||(hh==91)||(hh==93))) {
	      	return true;
	    }			
	}
	return false;
}

//@.#,$,&,*,(),{},[],.(dot),_(underscore),-(hyphen),(comma)

/*address can have alphanumerics and , - and / # & */
function isSplAdd(obj) {
	var name = obj.value;

	for(var k=0; k<name.length; k++) {
	    var hh = name.charCodeAt(k);
	    if(!((hh >= 64 && hh<=90 ) || (hh >= 97 && hh<=122) || (hh > 37 && hh<58) || (hh==10) || (hh==32)||(hh==35)||(hh==36)||(hh==95)||(hh==123)||(hh==125)||(hh==91)||(hh==93))) {
	      	return true;
	    }			
	}
	return false;
}

function checkvend(v)
{
	var eem   = document.add.vemail.value;
	var name  = document.add.vendname.value;
	var cname = document.add.chkname.value;
	var ph    = document.add.phone.value;
	var addr   = document.add.address.value;
	var ct    = document.add.city.value;
	var st	  = document.add.state.value;
	var zp    = document.add.zip.value;
	var fx    = document.add.fax.value;

	if(name == "" ) {
		inlinemsg('vendname','Name should not be empty!','text');
		return false;
	}
	if(cname == "" ) {
		inlinemsg('chkname','Check Name should not be empty!','text');
		return false;
	}
	if(addr == "" ) {
		inlinemsg('address','Address should not be empty!','text');
		return false;
	}

	var tobj = document.add.vendname;
	if(isSpl(tobj)) {
		document.add.vendname.select();
		inlinemsg('vendname','Not a valid name!','text');
		return false;
	} 

	tobj = document.add.chkname;
	if(isSpl(tobj)) {
		document.add.chkname.select();
		inlinemsg('chkname','Not a valid check name!','text');
		return false;
	} 

	tobj = document.add.address;
	if(isSplAdd(tobj)) {
		document.add.address.select();
		inlinemsg('address','Address should not have special chars!','text');
		return false;
	} 

	tobj = document.add.city;
	if(isSpl(tobj)) {
		document.add.city.select();
		inlinemsg('city','Not a valid City!','text');
		return false;
	} 
	
	tobj = document.add.zip;
	if(isSpl(tobj)) {
		document.add.zip.select();
		inlinemsg('zip','Not a valid Zip code!','text');
		return false;
	} 	

	tobj = document.add.state;
	if(isSpl(tobj)) {
		document.add.state.select();
		inlinemsg('state','Not a valid state!','text');
		return false;
	} 	

	tobj = document.add.tax;
	if(isSpl(tobj)) {
		document.add.tax.select();
		inlinemsg('tax','Not a valid taxID!','text');
		return false;
	} 

	if (!isNaN(name))	
	{
	  document.add.vendname.select();
	  inlinemsg('vendname','Name should not be a number!','text');
	  document.add.vendname.select();
	  return false;
	}
	if (!isNaN(cname))	
	{
	  document.add.chkname.select();
	  inlinemsg('chkname','Check Name should not be a number!','text');	 
	  return false;
	}
	if (ct!='' && (!isNaN(ct)))	
	{
	  document.add.city.select();
	  inlinemsg('city','City should not be a number!','text');
	  return false;
	}
	if (st!='' && (!isNaN(st)))	
	{
	  document.add.state.select();
	  inlinemsg('state','State should not be a number!','text'); 
	  return false;
	}

	
	if(eem!='' && (!checkMailId(eem)))
	{
		return false;
	}
	if(addr.length > 100)
	{
	  document.add.address.focus(); 
	  inlinemsg('address','Address is too long(above 100 chars)!','text'); 
	  return false;  
	}
	
	for(var k=0; k<ph.length; k++)
	{
	  var hh = ph.charCodeAt(k);
	  if(!((hh > 42 && hh<46) || (hh > 47 && hh<58))) {
		document.add.phone.select();
	  	inlinemsg('phone','Enter a valid Phone no (e.g: 1234567 (or) +91-011-4444444)','text'); 
		return false;
	  }
	}
	
	for(var k=0; k<fx.length; k++)
	{
	  var hh = fx.charCodeAt(k);
	  if(!((hh > 42 && hh<46) || (hh > 47 && hh<58))) {
		document.add.fax.select();
		inlinemsg('fax','Enter a valid Fax no. ','text'); 
		return false;
	  }
	}	
	
	if(v==1)
	{
		var opbal = document.add.opbal.value;
		var dat   = document.add.date.value;
		if(opbal!='')	
		{
			if(isNaN(opbal) || (opbal == 0))
			{
				document.add.opbal.select();
				inlinemsg('opbal','Enter a valid amount','text'); 				 	return false; 	
			}
		}
		if(opbal != 0 && dat =="")
		{
			inlinemsg('ddiv','Date can not be empty for an entered balance','');
		 	return false; 	 
		}			
	}
	return true;
}

function conAutoFill(obj) {
	var con = obj.value;
	document.add.chkname.value=con;
	document.add.chkname.focus();
}


