/* ##################### */
/*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 == "." || 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){	
	return validEmail(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' );
	var email_pattern	=	/^[^0-9_.][A-z0-9_.-]+[@][A-z0-9_-]+([.][A-z0-9_-]+)*[.][A-z]{2,4}$/;
	return email_pattern.test(str);
//	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 address !");	
			   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 chkCompare(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)
          {
               alert("Please select atleast two vehicles to compare !");
               return false;
          }
		  else if (total < 2 || total > 5)
          {
               alert("Please select minimum 2 & maximum 5 vehicles for comparison !");
               return false;
          }
     }
	 //alert("You can not delete items");
     return true;
}

function checkEmpty(formName,fieldArr,fieldNameArr)
{
	var arrLen = fieldArr.length;
	// check the form for restricted words
	for(i=0; i < arrLen; i++)
	{
		var field = eval("document."+formName+"."+fieldArr[i]);
		if(field.value.length == 0)
		{
			alert("Please enter "+fieldNameArr[i]+"");
			field.focus();
			return false;
		}
	}
	return true;
}

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.fportId.value == "") && (chk.fromPort.value == ""))
	{
		alert("Please Select From Port Or Add New Port to create Freight...");
		chk.fportId.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;
	}
	if((chk.tportId.value == "") && (chk.toPort.value == ""))
	{
		alert("Please Select To Port Or Add New Port to create Freight...");
		chk.tportId.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=50,top=50,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;
}

// Back to top function
var scrj = 1;
function pageTop() {
	var x1 = x2 = x3 = 0;
	var y1 = y2 = y3 = 0;

	if (document.documentElement) {
		x1 = document.documentElement.scrollLeft || 0;
		y1 = document.documentElement.scrollTop || 0;
	}

	if (document.body) {
		x2 = document.body.scrollLeft || 0;
		y2 = document.body.scrollTop || 0;
	}

	x3 = window.scrollX || 0;
	y3 = window.scrollY || 0;

	var x = Math.max(x1, Math.max(x2, x3));
	var y = Math.max(y1, Math.max(y2, y3));

	window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));

	if (x > 0 || y > 0) {
		window.setTimeout("pageTop()", 30);
	}
}

function req_check_toggle(checked,na,obj)
{
	 for(var i=0;i<document.form1.elements.length;i++)
	  {  
		var e = document.form1.elements[i];
		var n= e.name;
		var str= n.substring(0,na.length);
	
		if(e.type == "checkbox" && str==na)
		{
			 if(obj.checked == true)
			 {
				 e.checked = false;
				 e.disabled = true;
			 }
			 else
			 {
				 e.disabled = false;
			 }
		}
	  }
}

function req_checkAll(checked,na)
{
	  for(var i=0;i<document.form1.elements.length;i++)
	  {  
		 	var e = document.form1.elements[i];
			var n= e.name;
			var str= n.substring(0,na.length);
		
			if(e.type == "checkbox" && str==na)
				 e.checked = checked;
	  }
}

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

var blink_onoff = 1;
function blinkMe(obj,on_color,off_color,blinkspeed)
{
	if( blink_onoff == 1) {
	   document.getElementById(obj).style.color = on_color;
	   blink_onoff = 0;
	}
	else {
	   document.getElementById(obj).style.color = off_color;
	   blink_onoff = 1;
	}
	setTimeout('blinkMe("'+obj+'","'+on_color+'","'+off_color+'",'+blinkspeed+')',blinkspeed);
}

// check the forms
function valid(formName)
{
	// check the form for restricted words
	for(i=0; i<formName.elements.length; i++)
	{
		if(check_text(formName.elements[i])!=null)
		  {
			alert("\'www\', \'http\' or \'href\' not allowed.");
			formName.elements[i].focus();
			return 1;
		  }
	}
	return null;
}
function check_text(field)
{
	msg_str = trim(field.value);
	msg_str = msg_str.toLowerCase();
	if(field.name=='website' || field.name=='page_url')
	{
	return null;
	}
	else if((msg_str.search(/www/i)!=-1) || (msg_str.search(/http/i)!=-1) || (msg_str.search(/href/i)!=-1))
	{
		return 1;
	}
	else 
	return null;
}

function checkforblockchar(strdate)
{
	return true;
}

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 toggle(showHideDiv, switchTextDiv, showText, hideText) {
	var ele = document.getElementById(showHideDiv);
	var text = document.getElementById(switchTextDiv);
	if(ele.style.display == "block") {
    		ele.style.display = "none";
		text.innerHTML = showText;
  	}
	else {
		ele.style.display = "block";
		text.innerHTML = hideText;
	}
}

function checkListAll(obj)
{
	var listObj = document.getElementById(obj);
	var list_len = listObj.length;
			 
	for(i=0;i<list_len;i++)
	listObj[i].selected=true;
}

function disableList(val, fieldname)
{
	obj1 = document.getElementById(fieldname);
	obj1.disabled = val;
	obj1 = document.getElementById('temp_'+fieldname);
	obj1.disabled = val;
}

function toggleDiv(showHideDiv)
{
	var ele = document.getElementById(showHideDiv);
	if(ele.style.display == "block") {
    		ele.style.display = "none";
  	}
	else {
		ele.style.display = "block";
	}
}

function shift_multilist_rows(objname1,objname2)
{
	obj1 = document.getElementById(objname1);
	obj2 = document.getElementById(objname2);	
	var i;
	var k;
	k=obj2.length;
	for(i=0;i<obj1.length;i++)
	if(obj1[i].selected)
	{
		ch=obj1[i].value;
		caption=obj1[i].text;
		// Add selected item		
		obj2[k++]=new Option(caption,ch);		
	}
	
	for(i=0;i<obj1.length;)
	if(obj1[i].selected)
	obj1[i]=null;
	else
	i++;
	
//	sortObject(obj2);
}

function getXMLHttp()
{  
	var xmlHttp;
	try
	{    
		// Firefox, Opera 8.0+, Safari
		return xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{    
		// Internet Explorer    
		try
		{      
			return xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      
		}
		catch (e)
		{      
			try
			{        
			return xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
			}
			catch (e)
			{        
				alert("Your browser does not support AJAX!");        
				return false;        
			}      
		}    
	}
	
}

// common ajax message sender
function SendAjaxMessage(fileUrl, argNameArr, argValueArr, loaderID, reusultAreaID, waitMess)
{
	xmlHttp1 = getXMLHttp();
	xmlHttp1.onreadystatechange = showAjaxMess;
	
	var arr_str = '';
	arrLength = argNameArr.length;
	for(i=0;i<arrLength;i++)
	{
		arr_str += '&';
		arr_str += argNameArr[i];
		arr_str += '=';
		arr_str += argValueArr[i];
	}
	
	//alert(arr_str);
	xmlHttp1.open("GET", fileUrl+'?'+arr_str, true);
	xmlHttp1.send(null);
	
	if(xmlHttp1.readyState == 1)
	{
		document.getElementById(loaderID).style.display = 'inline';
		document.getElementById(reusultAreaID).innerHTML = waitMess;
	}
	
	function showAjaxMess()
	{
		if(xmlHttp1.readyState == 4)
		{
			document.getElementById(loaderID).style.display = 'none';
			response = xmlHttp1.responseText;
			document.getElementById(reusultAreaID).innerHTML = response;
		}
	}
}


function isNumber(obj, msg_txt)
{
	if(!isNaN(obj.value))
	{
		alert(msg_txt + ' should be a number !');	
		obj.focus();
		return false
	}
	return true;
}


// This function removes non-numeric characters
function stripNonNumeric( str )
{
	str += '';
	var rgx = /^\d|\.|-$/;
	var out = '';
	for( var i = 0; i < str.length; i++ )
	{
		if( rgx.test( str.charAt(i) ) )
		{
			if( !( ( str.charAt(i) == '.' && out.indexOf( '.' ) != -1 ) || ( str.charAt(i) == '-' && out.length != 0 ) ) || str.match(/\D/g) )
			{
				out += str.charAt(i);
			}
		}
	}
	return out;
}


Number.prototype.formatMoney = function(c, d, t){
var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};
