/* ##################### */
/*general functions to check the users email, alphabets, name, password, remove spaces form the Name through trim() etc..*/
function clearValues(theForm)			//empty all field in a form
{
	for(var i=0;i<theForm.elements.length;i++)
	{  
		var e = theForm.elements[i];
		e.value = "";
	}
	return false;
}

function trim (strVar) { 
	if(strVar.length >0)
	{
		while(strVar.charAt(0)==" ") 
			strVar=strVar.substring(1,strVar.length); 
		while(strVar.charAt(strVar.length-1)==" ") 
			strVar=strVar.substring(0,strVar.length-1); 			
	 }
	 return strVar; 
}
function isNotAlphabets(str){
	for (var i = 0; i < str.length; i++)
	{
		var ch = str.substring(i, i + 1);
		if((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) 
		{
			return true;
		}
	}
	return false;
}
function isNotNumeric(str){
	for (var i = 0; i < str.length; i++)
	{
		var ch = str.substring(i, i + 1);
		if((ch < '0' || '9' < ch)) 
		{
			if(ch == "-" || ch == ".") continue;
			return true;
		}
	}
	return false;
}

/*To check the Login ID of the User*/

function isNotID(str){
	for (var i = 0; i < str.length; i++)
	{
		var ch = str.substring(i, i + 1);
		if((ch < '0' || '9' < ch) && ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch))) 
		{
			if(ch == "_") continue;
			return true;
		}
	}
	return false;
}
function isValidEmail(emailid){		
	var l=emailid.length;
	if(l==0)
	{
		return false;	
	}
	if(l!=0)
	{
		var a=emailid.indexOf('@');
		var d=emailid.lastIndexOf('.');
		var str1=emailid.substr(0,a);
		var str2=emailid.substr(a+1,d-a-1);
		var str3=emailid.substr(d+1,l);
		var len1=str1.length;
		var len2=str2.length;
		var len3=str3.length;
		if(a<0 || d<2)
		{
			alert ("Check for missing '@' or '.' ");
			return false;
		}
		else if (a>d)
		{
			alert ("Invalid email. Please enter correct email address");
			return false;
		}				
		if (len1<=1 || len2<=1 || len3 <=1)
		{
			alert ("Invalid email. Please enter correct email address");
			return false;
		}				
	}
	return true;
}
function validEmail(str)
{
	mailRE = new RegExp( );
	mailRE.compile( '^[\._a-z0-9-]+@[\.a-z0-9-]+[\.]{1}[a-z]{2,4}$', 'gi' );
	return (mailRE.test(str));
}



/*Used in All Cp Modules*/
function checkAll(checked)
{
	for(var i=0;i<document.removeForm.elements.length;i++)
	{  
		var e = document.removeForm.elements[i];
		if(e.type == "checkbox") e.checked = checked;
	}
}
/*Used in Freight Settings and Freight manager Module. Both these functions have same name accept form name. */
function checkAllFreight(checked)
{
	for(var i=0;i<document.frmdelStatus.elements.length;i++)
	{  
		var e = document.frmdelStatus.elements[i];
		if(e.type == "checkbox") e.checked = checked;
	}
}

function checkCreateUser(membersForm)
{
//Members Id validation
		  var member = trim(document.membersForm.loginId.value);
		  if(member.length == 0)
		  {
		       alert("Please enter Login ID");
			   document.membersForm.loginId.focus();
			   return false;
		  }
		  var member = document.membersForm.loginId.value;
		  if(member.length < 5)
		  {
		       alert("Login ID should be atleast Five(5) Characters!");
			   document.membersForm.loginId.focus();
			   return false;
		  }
		  if(isNotID(member))
		  {
		       alert("Invalid Characters in Login ID");
			   document.membersForm.loginId.focus();
			   return false;		  
		  }	
		  
		  //Password Validation
		  var password = trim(document.membersForm.password.value);
		  if(password.length == 0)
		  {
		       alert("Please enter Password");
			   document.membersForm.password.focus();
			   return false;
		  }
		  
		  var password = document.membersForm.password.value;
		  if(password.length < 6)
		  {
		       alert("Password should be atleast Six(6) Characters!");
			   document.membersForm.password.focus();
			   return false;
		  }		  
		  if(isNotID(password))
		  {
		       alert("Invalid Characters in Password");
			   document.membersForm.password.focus();
			   return false;		  
		  }
		  
		  //Confirm Password
		  var confirm_password = document.membersForm.confirm_password.value;
		  if(password != confirm_password)		  		  
		  {
		       alert("Password & Confirm Password mismatch!");
			   document.membersForm.confirm_password.focus();
			   return false;		  
		  }
		  
		  //First Name
		  var fname = trim(document.membersForm.name.value);
		  if(fname.length == 0)
		  {
		       alert("Please enter Name");
			   document.membersForm.name.focus();
			   return false;
		  }
		  
		  //Address
		  var address_1 = trim(document.membersForm.address_1.value);
		  if(address_1.length == 0)
		  {
		       alert("Please enter Street Address - 1");
			   document.membersForm.address_1.focus();
			   return false;
		  }
		  
		  //Country		  		  
		  var country = document.membersForm.country.value;
		  if(country.length == 0)
		  {
		       alert("Please Select Users Country");
			   document.membersForm.country.focus();
			   return false;
		  }
		  
		  //Email		  
		  var email_1 = trim(document.membersForm.email_1.value);
		  if(email_1.length == 0)
		  {
		       alert("Please enter Email - 1");
			   document.membersForm.email_1.focus();
			   return false;
		  }
		  
		  if(!validEmail(email_1))
		  {				   
			   alert("Invalid Email ID !");	
			   document.membersForm.email_1.focus();
			   return false;		  
		  }

		  return true;
	 }

/* #############Search keyword######################## */

function checkfrmedit(chk)
{
	if(chk.keyword.value == "")
	{
		alert("Please enter keyword to search");
		chk.keyword.focus();
		return false;
	}	
}

/*############### Delete member */

function deleteAlert1()
{
	var i=0;
	var total = 0;
	var msg = "";
    
   dml=frmdelStatus;
   len=dml.elements.length;
   
	if(len)
	{
		for (i=0; i<len; i++) 
	   	{
  			ss = dml.elements[i].name
    	 	if (dml.elements[i].type == "checkbox") 
  			{
				if(dml.elements[i].checked== true)
				{
					total=total+1;
				}
			}
		}
		if(total >0)
		{
			msg = msg + 'Do You Like to remove ' + total +' records ?';
		}
		else
		{
			alert("Please check the items to remove !");
			return false;
		}
		if(msg.length >0)
		{
			var flag;
			flag = confirm(msg + " \n If Yes Press \"OK\" else Press \"Cancel\".");			
			if(flag == true)
				return true;
		}
	}      
	return false;
}

/*#########  Delete in the CP folder files*/

function deleteAlert(theForm)
{
	 var records = theForm.records.value;
     var total = 0;
     var msg = "";
     if(records>0)
     {
          for(i=0; i<theForm.length; i++)
          {
               e=theForm.elements[i];
               name = e.name;
               name = name.substr(0,4);
               if (e.type=='checkbox' && name == "item")
               {
                    if(eval('e.checked') == true)
                    {
                         total= total+1;
                    }
               }
          }
          if(total >0)
          {
               msg = msg + 'Do You Like to remove ' + total +' records ?';
          }
          else
          {
               alert("Please check the items to remove !");
               return false;
          }
          if(msg.length >0)
          {
               var flag;
               flag = confirm(msg + " If Yes Press \"OK\" else Press \"Cancel\".");
               if(flag == true)
               return true;
          }
     }
	 //alert("You can not delete items");
     return false;
}

function deleteAlertOrder(theForm)
{
	 var records = theForm.records.value;
     var total = 0;
     var msg = "";
     if(records>0)
     {
          for(i=0; i<theForm.length; i++)
          {
               e=theForm.elements[i];
               name = e.name;
               name = name.substr(0,4);
               if (e.type=='checkbox' && name == "item")
               {
                    if(eval('e.checked') == true)
                    {
                         total= total+1;
                    }
               }
          }
          if(total >0)
          {
               msg = msg + "If you will delete these orders, it will delete from below modules \nOrder Manager \nCustomer Invoice \nCustom Invoice	\nShipping Instruction \n\nDo You want to delete " + total +" orders ???";
          }
          else
          {
               alert("Please check the items to remove !");
               return false;
          }
          if(msg.length >0)
          {
               var flag;
               flag = confirm(msg + " If Yes Press \"OK\" else Press \"Cancel\".");
               if(flag == true)
               return true;
          }
     }
	 //alert("You can not delete items");
     return false;
}


function deleteAdmin(theForm)
{
	var records = theForm.records.value;
	var total = 0;
	var msg = "";
	if(records>0)
	{
		for(i=0; i<theForm.length; i++)
		{
			e=theForm.elements[i];
			if (e.type=='checkbox')
			{
				if(eval('e.checked') == true)
				{
					total= total+1;
				}
			}
		}
		if(total >0)
		{
			msg = msg + 'Do You Like to remove ' + total +' records ?';
		}
		else
		{
			alert("Please check the items to remove !");
			return false;
		}
		if(msg.length >0)
		{
			var flag;
			flag = confirm(msg + " If Yes Press \"OK\" else Press \"Cancel\".");
			if(flag == true)
			return true;
		}
	}
	//alert("You can not delete items");
	return false;
}

function createFreight(chk)
{
	if((chk.fctryId.value == "") && (chk.fromCountry.value == ""))
	{
		alert("Please Select From Country Or Add New Country to create Freight...");
		chk.fctryId.focus();
		return false;
	}	
	if((chk.tctryId.value == "") && (chk.toCountry.value == ""))
	{
		alert("Please Select To Country Or Add New Country to create Freight...");
		chk.tctryId.focus();
		return false;
	}
}

var pop='';

function openwin(nm,width,height) {
   var name=nm;
   if (pop && !pop.closed) {
      pop.close();
   }
   pop=eval("window.open('"+name+"','NewWIN','chrome[4],toolbar=no,left=5,top=5,width="+width+",height="+height+",directories=no,menubar=no,SCROLLBARS=yes,left=2,right=2')");
   if (!pop.opener) popUpWin.opener = self;
}
function closewin()
{		
	window.close();
}
function checkLogin(chk)
{
	if(chk.userName.value.length==0)
	{
		alert("User Name can not be left blank");
		chk.userName.focus();
		return false;
	}

	if(chk.password.value.length==0)
	{
		alert("Password can not be left blank");
		chk.password.focus();
		return false;
	}
}
/*For Old Member Validations*/  
function MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}


function checkDelete(theForm)				//delete confirmation for order vehicle
{
	var records = theForm.records.value;

	var total = 0;
	var msg = "";
	if(records>0)
	{
		for(i=0; i<theForm.length; i++)
		{
			e=theForm.elements[i];
			if (e.type=='checkbox')
			{
				if(eval('e.checked') == true)
				{
					total= total+1;
				}
			}
		}
		if(total >0)
		{
			msg = msg + 'Do You Like to remove ' + total +' records ?';
		}
		else
		{
			alert("Please check the items to remove !");
			return false;
		}
		if(total==records)
		{
			alert("Atleast one vehicle compulsory in your order");
			return false;
		}
		if(msg.length >0)
		{
			var flag;
			flag = confirm(msg + " If Yes Press \"OK\" else Press \"Cancel\".");
			if(flag == true)
				return true;
		}
	}
	//alert("You can not delete items");
	return false;
}
function auctionOrder()
{
	var current_bid,bid_price;
	var num = 0;  
	var isSubmit = 0;//false

	var records = form.records.value;
	var bid_interval = document.form.bid_interval.value;

	for(var j=0;j<records;j++)
	{
		current_bid=document.form['current_bid['+j+']'].value;
		bid_price=document.form['price['+j+']'].value;
		current_bid=eval(current_bid)+eval(bid_interval);
		if(current_bid>bid_price)
		{
			break;
		}
	}
	if(j==records) isSubmit=1;
	if(isSubmit == 1)
	{
		document.form.action="auction_order_bid.php"; 
		document.form.target="";
		document.form.submit();
	}
	else alert("Please enter your Bid Price more than "+current_bid+"\nMinimum bid margin is JPY "+bid_interval+"!");
}

function check(checked,theForm,id)
{
	var bidPrice = theForm.value;
	if(bidPrice.length == 0)
	{
		checked = false;
	}
	if(isNotInt(bidPrice))
	{
		alert("Invalid Bid Price, Enter Only  Numeric value!");
		theForm.focus();		
	}
	else
	{		
		for(var i=0;i<document.form.elements.length;i++)
		{  
			var e = document.form.elements[i];
			if(e.type == "checkbox") 
			{
				if(e.value == id)
				e.checked = checked;
			}
		}
	}
}
function isNotInt(str){
	for (var i = 0; i < str.length; i++)
	{
		var ch = str.substring(i, i + 1);
		if(ch < '0' || '9' < ch) 
		{					
			return true;
		}
	}
	return false;
}