/* ##################### */
/*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 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;
	  }

}

function checkAllperm(checked)
{
	  for(var i=0;i<document.showPermFrm.elements.length;i++)
	  {  
		 	var e = document.showPermFrm.elements[i];
			if(e.type == "checkbox") e.checked = checked;
	  }

}

function checkAllperm1(checked)
{
	  for(var i=0;i<document.permFrm.elements.length;i++)
	  {  
		 	var e = document.permFrm.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(isValidEmail(email_1) == 0)
		  {
			   
			   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
  		//s = ss.substr(0, 6);

     	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)
{
	 //alert("You do not have the permission to remove !");
	 //return false;
	 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 bookmark()
{
	window.external.AddFavorite(url,title);
}



// JavaScript Document

function changecolor(row)
{
	row.className='blue';
}

function restorecolor(row)
{
	row.className='white';
}

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=50,top=50')");
   if (!pop.opener) popUpWin.opener = self;
}

var pop='';
function openwin1(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 checkPassword(chk)
{
	if(chk.userName.value.length==0)
	{
		alert("Please Select a User");
		chk.userName.focus();
		return false;
	}
	
	if(chk.npass.value.length==0)
	{
		alert("Please type new password");
		chk.npass.focus();
		return false;
	}
	
	if(chk.rpass.value.length==0)
	{
		alert("Please Re-Type the new password");
		chk.rpass.focus();
		return false;
	}
	
	if(chk.npass.value!=chk.rpass.value)
	{
		alert("New password and Re-Type password does not match");
		chk.npass.value="";
		chk.rpass.value="";
		chk.npass.focus();
		return false;
	}
}

function addUser(chk)
{
	if(chk.userName.value.length==0)
	{
		alert("Please type a user name");
		chk.userName.focus();
		return false;
	}
	
	if(chk.npass.value.length==0)
	{
		alert("Password field can not be left blank");
		chk.npass.focus();
		return false;
	}
	
	if(chk.rpass.value.length==0)
	{
		alert("Re-Type Password field can not be left blank");
		chk.rpass.focus();
		return false;
	}
	
	if(chk.npass.value!=chk.rpass.value)
	{
		alert("Password and Re-Type password does not match");
		chk.npass.value="";
		chk.rpass.value="";
		chk.npass.focus();
		return false;
	}
}

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;
	}
}	

function checkAll1(val,val1)
 {
   dml=document.memberform;
   len=dml.elements.length;
   var i=0;
   	for (i=0; i<len; i++) {

	   var s = "The quick brown fox jumped over the lazy yellow dog.";
  		ss = dml.elements[i].name
  		s = ss.substr(0, 5);

     	if (dml.elements[i].type == "checkbox" && s==val1) {
        	if (val == 1) { 
           		dml.elements[i].checked=true;
        	} 
			else {
           		dml.elements[i].checked=false;
        	}
     	}   
   	}
}

function chk4() {
     chk2=document.form1;
     len=chk2.elements.length;
	 var i=0;
 	 var pp=1;
   	 for (i=0; i<len; i++) {
        if ((chk2.elements[i].type == "checkbox") && (chk2.elements[i].checked==true)) {
           pp=2;
	    }
	 }
     if (pp==1) {
	    return ("1");
     }  
}

function chktrim(inputString) {
    if (typeof inputString != "string") { return inputString; }
    var retValue = inputString;
    var ch = retValue.substring(0, 1);
    while (ch == " ") { 
       retValue = retValue.substring(1, retValue.length);
       ch = retValue.substring(0, 1);
    } 
    ch = retValue.substring(retValue.length-1, retValue.length);
    while (ch == " ") { 
       retValue = retValue.substring(0, retValue.length-1);
       ch = retValue.substring(retValue.length-1, retValue.length);
    }
    while (retValue.indexOf("  ") != -1) { 
       retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
    }
    return retValue; 
}


		/*For New Member Validations*/
		

function userValid(chk) 
   {
   			if (chk.email.value.length == 0) 
   			{
      alert("E-mail address can't be left blank");
      chk.email.focus();
      return false;
   			}
   			
   			if (chk.email.value.indexOf('@') == -1) 
   			{
      alert("Error in e-mail address");
      chk.email.focus();
      return false;
   			}
   			
   		if (chk.email.value.indexOf('.') == -1) 
   	{
      alert("Error in e-mail address");
      chk.email.focus();
      return false;
   }
   
   if (chk.email.value.indexOf('@') != chk.email.value.lastIndexOf('@')) 
   {
      alert("Please Specify One E-mail address only");
      chk.email.focus();
      return false;
   }
     
   if (chk.pass.value == 0) 
   {
      alert("Unable to submit !! Do not leave the Password field blank");
      chk.pass.focus();
      return false; 
   }
   
   if (chk.yourName.value == 0) 
   {
      alert("Name can't be left blank");
      chk.yourName.focus();
      return false;
   }
   
   if (chk.bAddress.value == 0) 
   {
      alert("Billing Address can't be left blank");
      chk.bAddress.focus();
      return false;
   }
   
   if (chk.bCity.value == 0) 
   {
      alert("City can't be left blank");
      chk.bCity.focus();
      return false;
   }
   if (chk.bState.value == 0) 
   {
      alert("State can't be left blank");
      chk.bState.focus();
      return false;
   }
   if (chk.bZip.value == 0) 
   {
      alert("Zip/Post Code  can't be left blank");
      chk.bZip.focus();
      return false;
   }
   if (chk.bPhone.value == 0) 
   {
      alert("Phone can't be left blank");
      chk.bPhone.focus();
      return false;
   }
   
   var val1= chk.bPhone.value;
   
   if (isNaN(val1)) 
   {
      alert("Enter Valid Phone Number");
      chk.bPhone.focus();
      return false;
   }
   
   if (!chk.bsStatus.checked) 
   {
	   
      if (chk.sAddress.value == 0) 
      {
         alert("shipping Address can't be left blank");
         chk.sAddress.focus();
         return false;
      }   
      if (chk.sCity.value == 0) 
      {
         alert("City can't be left blank");
         chk.sCity.focus();
         return false;
      }
      if (chk.sState.value == 0) 
      {
         alert("State can't be left blank");
         chk.sState.focus();
         return false;
      }
      if (chk.sZip.value == 0) 
      {
         alert("Zip/Post Code  can't be left blank");
         chk.sZip.focus();
         return false;
      }
      if (chk.sPhone.value == 0) 
      {
         alert("Phone can't be left blank");
         chk.phone2.focus();
         return false;
      }
      
      var val1= chk.sPhone.value;
     
       if (isNaN(val1)) 
       {
         alert("Enter Valid Phone Number");
         chk.sPhone.focus();
         return false;
      }
   }
   }
   
   
   /*For Old Member Validations*/
		

function userValid(chk) 
{
   			
   if (chk.yourName.value == 0) 
   {
      alert("Name can't be left blank");
      chk.yourName.focus();
      return false;
   }
   
   if (chk.bAddress.value == 0) 
   {
      alert("Billing Address can't be left blank");
      chk.bAddress.focus();
      return false;
   }
   
   if (chk.bCity.value == 0) 
   {
      alert("City can't be left blank");
      chk.bCity.focus();
      return false;
   }
   if (chk.bState.value == 0) 
   {
      alert("State can't be left blank");
      chk.bState.focus();
      return false;
   }
   if (chk.bZip.value == 0) 
   {
      alert("Zip/Post Code  can't be left blank");
      chk.bZip.focus();
      return false;
   }
   if (chk.bPhone.value == 0) 
   {
      alert("Phone can't be left blank");
      chk.bPhone.focus();
      return false;
   }
   
   var val1= chk.bPhone.value;
   
   if (isNaN(val1)) 
   {
      alert("Enter Valid Phone Number");
      chk.bPhone.focus();
      return false;
   }
   
   if (!chk.bsStatus.checked) 
   {
	   
      if (chk.sAddress.value == 0) 
      {
         alert("shipping Address can't be left blank");
         chk.sAddress.focus();
         return false;
      }   
      if (chk.sCity.value == 0) 
      {
         alert("City can't be left blank");
         chk.sCity.focus();
         return false;
      }
      if (chk.sState.value == 0) 
      {
         alert("State can't be left blank");
         chk.sState.focus();
         return false;
      }
      if (chk.sZip.value == 0) 
      {
         alert("Zip/Post Code  can't be left blank");
         chk.sZip.focus();
         return false;
      }
      if (chk.sPhone.value == 0) 
      {
         alert("Phone can't be left blank");
         chk.phone2.focus();
         return false;
      }
      
      var val1= chk.sPhone.value;
     
       if (isNaN(val1)) 
       {
         alert("Enter Valid Phone Number");
         chk.sPhone.focus();
         return false;
      }
   }
   }
   
function MM_openBrWindow(theURL,winName,features)
{
	window.open(theURL,winName,features);
}
function deleteAlert_new(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,5);
               if (e.type=='checkbox' && name == "check")
               {
                    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 deleteAlert2(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 validEmail(str)
{
	mailRE = new RegExp( );
	mailRE.compile( '^[\._a-z0-9-]+@[\.a-z0-9-]+[\.]{1}[a-z]{2,4}$', 'gi' );
	return (mailRE.test(str));
}
function auctionSubmit()
{
   var num = getCheckedNum();
    if(num == 0) {
    	alert("No auction items selected! Please select first.");
		return false;
    }
    document.form.action ="auction_cart.php#1";
    document.form.submit();
}
function tenderSubmit()
{
   var num = getCheckedNum();
    if(num == 0) {
    	alert("No items selected! Please select first.");
		return false;
    }
    document.form.action ="tender_cart.php#1";
    document.form.submit();
}


function getCheckedNum()
{
	var num = 0;
	for(var i=0;i<document.form.elements.length;i++)
	{  
		var e = document.form.elements[i];
		if(e.type == "checkbox")
		{
			if(e.checked) num++;
		}
	}
    return num;
}
function auctionOrder()
{
	var current_bid,bid_price;
	var num = 0;  
	var isSubmit = 0;//false

	var records = form.records.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)+500;
		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 500!");
}
function tenderOrder()
{
	var tender_price;
	var num = 0;  
	var isSubmit = 0;//false

	var records = form.records.value;
	var tot=0;

	for(var j=0;j<records;j++)
	{
		tender_price=document.form['price['+j+']'].value;
		if(tender_price.length > 0) tot++;
	}

	if(tot > 0) isSubmit=1;
	if(isSubmit == 1)
	{
		document.form.action="tender_order_insert.php"; 
		document.form.target="";
		document.form.submit();
	}
	else alert("Please enter your Tender Price for the vehicle(s) you want!");
}




function auctionQuotation()
{
	var num = 0;  
	var isSubmit = 0;//false

    for(var i=0;i<document.form.elements.length;i++)
    {
		var e = document.form.elements[i];

		if (e.type == "text")
		{
           	var price = e.value;
			if(price >0) isSubmit = 1; //true
			
            num++;
        }
    }	    
	if(isSubmit == 1)
	{
		document.form.action="auction_quotation.php";     
		document.form.target="";
		document.form.submit();
	}
	else alert("Please enter your Bid Price for the vehicle(s) you want to place QUOTATION !");
}

function cartToBid()
{
	var cartItemMax = parseInt(document.form.cartItemMax.value);
    var cartItemNum = parseInt(document.form.cartItemNum.value);
	if(cartItemMax <= cartItemNum) {
        alert("Your Auction Cart is full. It can store a maximum of "+cartItemMax+" auction items.");
        return;
    }
	document.form.action="auction_cart.php";     
	document.form.target="";
	document.form.submit();
}

function addToCart()
{
    var cartItemMax = parseInt(document.form.cartItemMax.value);
    var cartItemNum = parseInt(document.form.cartItemNum.value);
    var num = getCheckedNum();
	
	if(cartItemMax <= cartItemNum) {
        alert("Your Auction Cart is full. It can store a maximum of "+cartItemMax+" auction items.");
        return;
    }	
	if(cartItemMax < (cartItemNum+num)) {
        alert("It will exceed the maximum limit of auction items ("+cartItemMax+"). At present you have "+cartItemNum+" auction items.");
        return;
    }
	if(num == 0) {
        alert("Please select at least one item to add.");
        return;
    }	
	
    document.form.action=window.location.href;
    document.form.target="";
    document.form.submit();
}

function checkPrice(theForm,startPrice)
{
	 var bidPrice = theForm.value;
	 if(bidPrice >0 && bidPrice<= startPrice)
	 {
		  alert("Bid price must be greater than Current Price ("+ startPrice + ")!");
		  theForm.focus();
	 }
}
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;
}
function textLimitCheck(theForm,theControl,maxLength)
{
	if (theForm.value.length > maxLength)
	{
		alert(maxLength + ' characters limit. \r Excessive data will be truncated.');
		theForm.value = theForm.value.substring(0, maxLength-1);
		theForm.focus();
	}
	theControl.value = theForm.value.length;
}
