
//pass the textbox value for checking sqlinjection
//It will check for any special character that match with the values we specified. 
//If invalid character is found it will gice alert and return 0.

// to disable back button functionality
function DisableBackButton()
{
    history.go(1);
}

function chktxt(txtvalue)
{
       
    var tval =txtvalue;
    myArray = new Array("<",">","'","!","$","%","(",")",";","=","#","^","|","&","@","*","~","`","?",":","-");
    var len =myArray.length;
    var tlen = tval.length;
    for (var x = 0; x <= len - 1 ; x++)
    {
        for (var y = 0; y <= tlen; y++)
            {
            if (myArray[x] == tval.charAt(y))
            {

            alert("Invalid Characters found in entries..Please Check!!");
            return 0;
            }
        //	else
            //return true;}
        }
    }
}
//*************************************************************************************

function chktxt_houseno(txtvalue)
{

    var tval =txtvalue;
    myArray = new Array("<",">","'","!","$","%",";","=","#","^","|","&","@","*","~","`","?",":","/","\\");
    var len =myArray.length;
    var tlen = tval.length;
    for (var x = 0; x <= len - 1 ; x++)
    {
        for (var y = 0; y <= tlen; y++)
            {
            if (myArray[x] == tval.charAt(y))
            {
                alert("Invalid Characters found in entries. Do not use special characters such as \n\t < > '/ ! $ % ; = # ^ | & @ * ~ ` ? : \\");
                return 0;
            }
        //	else
            //return true;}
        }
    }
}
//*****************************************************88
function chkprocno(txtvalue)
{
      
		var tval =txtvalue;
		myArray = new Array("<",">","'","!","$","%",";","=","#","^","|","&","@","*","~","`","?",":");
		var len =myArray.length;
		var tlen = tval.length;
		for (var x = 0; x <= len - 1 ; x++)
			{
				for (var y = 0; y <= tlen; y++)
					{
					if (myArray[x] == tval.charAt(y))
					{
					
					alert("Invalid Characters found in entries..Please Check!!");
					return 0;					
					}
				//	else
					//return true;}
				}
			}
		}

//*************************************************************************************

//*************************************************************************************
function chktxt_email(txtvalue)
{
       
		var tval =txtvalue;
		myArray = new Array("<",">","'","!","$","%","(",")",";","=","#","^","|","&","*","~","`","?",":","-");
		var len =myArray.length;
		var tlen = tval.length;
		for (var x = 0; x <= len - 1 ; x++)
			{
				for (var y = 0; y <= tlen; y++)
					{
					if (myArray[x] == tval.charAt(y))
					{
					
					alert("Invalid Characters found in entries..Please Check!!");
					return 0;					
					}
				//	else
					//return true;}
				}
			}
		}
//*************************************************************************************

// check for special characters in address field

function chkadd(txtvalue)
{

		var tval =txtvalue;
		myArray = new Array("<",">","!","$","%",";","=","#","^","||","&","@","~","`","*","?",":","'");
		var len =myArray.length;
		var tlen = tval.length;
		for (var x = 0; x <= len - 1 ; x++)
			{
				for (var y = 0; y <= tlen; y++)
					{
					if (myArray[x] == tval.charAt(y))
					{
					
					alert("Invalid Characters found in entries..Please Check!!");
					return 0;					
					}
				//	else
					//return true;}
				}
			}
		}
//************************************************************************************

//check for special characters in price distribution field
function chk_price(txtvalue)
{

		var tval =txtvalue;
		myArray = new Array("<",">","'","!","$","(",")",";","=","#","^","|","&","@","*","~","`","?",":","-");
		var len =myArray.length;
		var tlen = tval.length;
		for (var x = 0; x <= len - 1 ; x++)
			{
				for (var y = 0; y <= tlen; y++)
					{
					if (myArray[x] == tval.charAt(y))
					{
					
					alert("Invalid Characters found in entries..Please Check!!");
					return 0;					
					}
				//	else
					//return true;}
				}
			}
		}

//check whether the value entered is numeric and check the range of acceptable integers


  function IsNumeric(strString)
   //  check for valid numeric strings	
   {	
   var strValidChars = "0123456789.-+";
   var strChar;
   var blnResult = 1;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         alert("Invalid Numbers found in entries..Please Check!!");
         blnResult = 0;
         }
      }
   return blnResult;
   }

 //*************************************************************************************
  
 function sha1auth(element,element2,element3,seed)
 //element : NAME OF USERNAME TEXTBOX,element2: NAME OF PASSWORD TEXTBOX,element3:NAME OF LABEL
		{
				
				var username =document.getElementById(element).value;
						// var rexp = /^\w+$/;
				if (username.length = 0 || username.length >15)
					{
					alert("Invalid username/password ");
					return 0;
					}
				/* if (username.search(rexp)==-1)
				{
					alert("Please enter valid username");
					return 0;
				}*/

			var password = document.getElementById(element2).value;
			
			
				if (password.length = 0 || password.length >15)
					{
						alert("Invalid username/password� ");
						return 0;
					}
					/* if (password.search(rexp)==-1)
					{
						alert("Please enter valid password");
						return 0;
					}*/
				var pwdhash = hex_sha1(password);
								
				var hash= hex_sha1(seed + pwdhash);
								
				document.getElementById(element3).value = hash;
				document.getElementById(element2).value = hash;
				return true;
}

function sha1pwd1(element,element2,element3)
		{
				
				//var password = document.getElementById(element).value;
				//debugger;
				var password = element;
				//alert("sha1pwd1");
				encryption3(element,element2);
				if (password.length = 0 || password.length >10)
					{
						alert("Invalid username/password ");
						return 0;
					}
					
				var pwdhash = hex_sha1(password);
				
				
				//document.getElementById(element).value =pwdhash;
				element=pwdhash;
				org_pwd= document.getElementById(element3).value;
				if (org_pwd == element)
				{
				    return element;
				}
				else
				{
				    alert("Confirm Password does not match with Password!");
				    return "";
				}
				//alert(element);
				
}



//	*******Encryption3*********************// ****************************// ****

function encryption3(element,element1)
		{
		
			var str = element;
			//alert("encryptn");
				
			var asci = '';
//			if (str.length < 8 || str.length >10)
//			{
//				alert("Password should contain characters between 8 and 10");
//				return 0;
//			}
//					
//			else if (PwdCharCheck(str) == false)
//			{
//				alert ("Password should contain atleast one special character.");
//				return 0;
//	        }
	        
			for (var y =0; y < str.length; y++)
			{
				//alert("loop");
				var charc= 128 + ascii_value((str.charAt(y)));
				var asci = asci.concat(String.fromCharCode(charc));
			}
			document.getElementById(element1).value = asci;
			return asci;
			//alert("end");
		}
		
			
//function encryption3(element,element1)
//		{
//		
//			var str = element;
//			//alert("encryptn");
//				
//			var asci = '';
//			if (str.length < 4 || str.length >10)
//					{
//					alert("Password should contain characters between 4 and 10");
//					return 0;
//					}
//				for (var y =0; y < str.length; y++)
//					{
//					    //alert("loop");
//						var charc= 128 + ascii_value((str.charAt(y)));
//						var asci = asci.concat(String.fromCharCode(charc));
//					}
//			document.getElementById(element1).value = asci;
//			return asci;
//			//alert("end");
//		}


//-------for hashing the password field---------------
function sha1pwd(element)
		{
				//debugger;
				//var password = document.getElementById(element).value;
				var password = element;
				//alert("sha1pwd");
				//alert(password);
//				if (password.length = 0 || password.length >10)
//					{
//						alert("Please enter a valid password");
//						return 0;
//					}
				if (password.length < 8 || password.length >10)
					{
					alert("Password should contain characters between 8 and 10");
					return 0;
					}
					
				if (PwdCharCheck(password) == false)
					{
					    alert ("Password should contain atleast one special/numeric character.");
					    return 0;
	  
					}
				//alert("201")	
				var pwdhash = hex_sha1(password);
				
				//alert("203")
				//document.getElementById(element).value =pwdhash;
				element=pwdhash;
				//alert(element);
				return element;
}

// for off-take

  

    function numcheck(ctl1,no1,no2)
	    {
	    
	       num=parseInt(ctl1);
					num_str=num;
					num1=ctl1;
					if (IsNumeric(num1)==0)
					{
					return false;
					}
					num2=num1.length-1;
					int_len =String(num).length;
					
					dec_len =num2-int_len;
					
					max_int=parseInt(no1);
					max_dec=parseInt(no2);
				if  (int_len>max_int)
				{
					alert ("Integer part can contain a maximum of " + max_int + " digits");
					return false;
				}
				if(dec_len>max_dec)
				{
					//debugger;
					alert ("Decimal part can contain a maximum of " + max_dec + " digits");
					return false;	
				}
		}
		
	 //**************************for stock *****************************	
		function issue_fn(ctl1,ctl2,ctl3)
		{
		
			if (numcheck(ctl1,5,3)==false)
			{
			document.forms[0][ctl3].value="";
			return;
			}
		 
			//n1=parseFloat(ctl1);
			//n2=parseFloat(ctl2);
			
			if (ctl1.length==0)
			{n1=parseFloat(0);}
			else
			{n1=parseFloat(ctl1);}
			
			
			if (ctl2.length==0)
			{n2=parseFloat(0);}
			else
			{n2=parseFloat(ctl2);}
			
			if (n1 > n2)
			{
				alert("Issue should not exceed total receipt");
				document.forms[0][ctl3].value=""
				return;
			}
			else
			{
				document.forms[0][ctl3].value= Math.round((n2-n1)*1000)/1000;  
			}
		}
		
		function receipt_fn(ctl1,ctl2,ctl3,ctl4,ctl5)
		{
		//debugger; 
			if (numcheck(ctl1,5,3)==false)
			{
			document.forms[0][ctl4].value="";
			document.forms[0][ctl5].value="";
			//document.forms[0][ctl1].value.focus();
			return;}
				
			if (ctl1.length==0)
			{n1=parseFloat(0);}
			else
			{n1=parseFloat(ctl1);}
			
			
			if (ctl2.length==0)
			{n2=parseFloat(0);}
			else
			{n2=parseFloat(ctl2);}
			
			if (ctl3.length==0)
			{n3=parseFloat(0);}
			else
			{n3=parseFloat(ctl3);}
			
			document.forms[0][ctl4].value=Math.round((n1+n2)*1000)/1000;
			//document.forms[0][ctl4].value=n1+n2;
			tot=parseFloat(document.forms[0][ctl4].value);
			issue=parseFloat(ctl3);
			if (issue > tot)
			{
				alert("Issue should not exceed total receipt");
				document.forms[0][ctl5].value=""
				return;
			}
			document.forms[0][ctl5].value=Math.round((n1+n2-n3)*1000)/1000;
			
			
		}	
		
		//************************************************************************	
		
		//****************************for lifting *******************************
		function compare_lift_dur(ctl1,ctl2)
		{
			if (numcheck(ctl2,5,2)==false)
			{return;}
			if (ctl1.length==0)
			{n1=parseFloat(0);}
			else
			{n1=parseFloat(ctl1);}
			
			
			if (ctl2.length==0)
			{n2=parseFloat(0);}
			else
			{n2=parseFloat(ctl2);}
			
			if (n2>n1)
			{
			 alert("Lifting should not exceed allotment!!!");
			 //document.getElementById(element2).value="";
			}
		}
		
		function compare_lift_upto(ctl1,ctl2,ctl3)
		{
			if (numcheck(ctl3,5,2)==false)
			{return;}
			if (ctl1.length==0)
			{n1=parseFloat(0);}
			else
			{n1=parseFloat(ctl1);}
			
			
			if (ctl2.length==0)
			{n2=parseFloat(0);}
			else
			{n2=parseFloat(ctl2);}
			
			if (ctl3.length==0)
			{n3=parseFloat(0);}
			else
			{n3=parseFloat(ctl3);}
			
			if (n3>n1)
			{
			 alert("Invalid figure! Lifting upto the week should not exceed allotment!");
			 return;
			 //document.getElementById(element2).value="";
			}
			
			if (n2>n3)
			{
			 alert("Invalid figure! Lifting during the week should not exceed Lifting upto the week!");
			 return;
			 //document.getElementById(element2).value="";
			}
		}
		//*********************************************************************

// *******Ascii Value*********************// 
//****************************// ********
function ascii_value (c)
	{
	// restrict input to a single character
		c = c.charAt(0);
	// loop through all possible ASCII values
	var i;
	for (i = 0; i < 256; ++ i)
	{
		// convert i into a 2-digit hex string
		var h = i.toString (16);
		if (h.length == 1)
			h = "0" + h;

		// insert a % character into the string
		h = "%" + h;

		// determine the character represented by the escape code
		h = unescape(h);

		// if the characters match, we've found the ASCII value
		if (h == c)
			break;
	}
	return i;
}


//date
/*function check_date(ctl1)
{
	var now = new Date( );
	now=ctl1;
	now.
}*/


//***************************************


function chktmp(txtvalue)
{
       
		var tval =txtvalue;
		myArray = new Array("<",">","'","!","$","%",";","=","#","^","|","&","@","~","`","?",":");
		var len =myArray.length;
		var tlen = tval.length;
		for (var x = 0; x <= len - 1 ; x++)
			{
				for (var y = 0; y <= tlen; y++)
					{
					if (myArray[x] == tval.charAt(y))
					{
					
					alert("Invalid Characters found in entries..Please Check!!");
					return 0;					
					}
				//	else
					//return true;}
				}
			}
		}
		
//***************************
	
function chkdeal(txtvalue)
{
       
		var tval =txtvalue;
		myArray = new Array("<",">","'","!","$","%","(",")",";","=","#","^","|","&","@","*","~","`","?",":");
		var len =myArray.length;
		var tlen = tval.length;
		for (var x = 0; x <= len - 1 ; x++)
			{
				for (var y = 0; y <= tlen; y++)
					{
					if (myArray[x] == tval.charAt(y))
					{
					
					alert("Invalid Characters found in entries..Please Check!!");
					return 0;					
					}
				//	else
					//return true;}
				}
			}
		}

//***************************FOR STATE CARD ABSTRACT CORRECTION**********************************
function TotalCardCount(apl,bpl,aplaay,bplaay,total)
		{
		
		            if (IsNumeric(apl)==0)
					{
					return false;
					}
					
					if (IsNumeric(bpl)==0)
					{
					return false;
					}
					
					if (IsNumeric(aplaay)==0)
					{
					return false;
					}
					
					if (IsNumeric(bplaay)==0)
					{
					return false;
					}
					 v_apl = parseInt(apl);
		             v_bpl = parseInt(bpl);
		             v_aplaay = parseInt(aplaay);
		             v_bplaay = parseInt(bplaay);
					
		    document.forms[0][total].value=v_apl+v_bpl+v_aplaay+v_bplaay;
		}
		//*********************************************
		
function UnitCalc(adults,minors,units)
		{
		
		            if (IsNumeric(adults)==0)
					{
					return false;
					}
					
					if (IsNumeric(minors)==0)
					{
					return false;
					}
								
					 v_adults = parseInt(adults);
		             v_minors = parseInt(minors);
					
					totunits=v_adults*2+v_minors;
		    document.forms[0][units].value=v_adults*2+v_minors;
		}

//*****************************************************************************************************

//**************************************SHA-256****************************************************

//-----------------------------------In Login form-----------------------------------

function sha256auth(element,element2,element3,seed)

 //element : NAME OF USERNAME TEXTBOX,element2: NAME OF PASSWORD TEXTBOX,element3:NAME OF LABEL
		{
				
				var username =document.getElementById(element).value;
				var password = document.getElementById(element2).value;
				
				if (username.length == 0 || username.length >15)
				{
				    alert("Invalid username/password");
				    return 0;
				}
				
				if (password.length == 0 || password.length >15)
				{
					alert("Invalid username/password");
					return 0;
				}
					
				var pwdhash =SHA256(password);                
				var hash=SHA256(seed + pwdhash);
                
				document.getElementById(element3).value = hash;
				document.getElementById(element2).value = hash;
				return true;
}

//-------------------------------------In Creation/Edit forms----------------------------

//element - Confirm password
//elemen3 - Original password(hash value)
//element2 - encrypted password(hidden field)--output
function sha256pwdConfirm(element,element2,element3)
		{
				
				var password = element;
				encryption3(element,element2);//after encryption ,element2 value changes
//				if (password.length = 0 || password.length >15)
//				{
//					alert("Invalid username/password");
//					return 0;
//				}
					
				var pwdhash = SHA256(password);
				
				element=pwdhash;
				org_pwd= document.getElementById(element3).value;
				if (org_pwd == element)
				{
				    return element;
				}
				else
				{
				    alert("Confirm Password does not match with Password!");
				    return "";
				}
				
}


function sha256pwd(element)
		{
				var password = element;
				
				if (password.length < 8 || password.length >15)
				{
				    alert("Password should contain characters between 8 and 15");
				    return 0;
				}
					
				if (PwdCharCheck(password) == false)
				{
				    //alert ("Password should contain atleast one special/numeric character.");
				    return 0;
  
				}
				var pwdhash = SHA256(password);
				element=pwdhash;
				return element;
}

//----------------------------------------------------
function PwdCharCheck(strPwd)
{
    //to ensure that pwd contains spl chars and no
    //var ValidChar = "<>!$();=#^|&@*~'?:-+%0123456789";
    var ValidChar = "<>!$();=#^|&@*~'?:-+%";
    var ValidNo = "0123456789";
    var strChar;
    var res = false;

    //spl char check
    for (i=0; i < strPwd.length && res == false ; i++)
    {
        strChar =strPwd.charAt(i);
        if (ValidChar.indexOf(strChar) != "-1")
        {
             res = true;
        }
    }

    if (res == false)
        {
            alert ("Password should contain atleast one Special Character like #,*,$,&,@ !");
            return res;
        }
   else
       {
           res = false;
           // no check
           for (i=0; i < strPwd.length && res == false ; i++)
            {
                
                strChar =strPwd.charAt(i);
                if (ValidNo.indexOf(strChar) != "-1")
                {
                     res = true;
                }
            }
       }

    if (res == false)
    {
        alert ("Password should contain atleast one Numeric Character!");
        return res;
    }
}

//------------------------------------------------
// CREATING XMLHTTP OBJECT FOR AJAX ----
/*function GetXmlHttpObject()
	{
		var XMLHttp=null;

		try
		{
			XMLHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			XMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		if (XMLHttp==null)
		{
			XMLHttp=new XMLHttpRequest();
		}

        if (XMLHttp==null)
		{
			alert("Your browser does not support AJAX. Please use latest browsers.");
		}
        
		return XMLHttp;

    }
*/
//-----------------------------------------------

function GetXmlHttpObject()
    {
		
        var XMLHttp=null;

        try
        {
		   XMLHttp=new XMLHttpRequest();
		   //alert("haiiiii");
        }
		
        catch(e)
        {
		  try
		  {
		     XMLHttp=new ActiveXObject("Msxml2.XMLHTTP");
		  }
		  catch(e)
		  {	 
		     XMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }	
        }
		             
        return XMLHttp;
    }

/*-------------------------------------------------------- *
 * TRIM FUNCTION WITH JAVASCRIPT 
 **/

    // Removes leading whitespaces
    function LTrim( value )
	{
        var re = /\s*((\S+\s*)*)/;
        return value.replace(re, "$1");
    }

    //Removes ending whitespaces
    function RTrim( value )
	{
        var re = /((\s*\S+)*)\s*/;
        return value.replace(re, "$1");
    }

    // Removes leading and ending whitespaces
    function trim( value )
	{
        return LTrim(RTrim(value));
    }

//--------------------------------------------------------

//CHECK FOR EMAIL ID
function checkEmail(email)
    {
        if(email == "")
        {
            alert("Enter e-mail address.");
            return false;
        }
		else if(email != "")
		{

		    var validRegExp = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/ ;
			//var validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
			 //strEmail = document.form1.email.value;
			if (email.search(validRegExp) == -1)
				{
					alert(" A valid e-mail address is required");
					return false;
				}
		}
        return true;
    }
