﻿// JScript File

//==============================================================================================================
function noCopyKey(e) 
{
    var forbiddenKeys = new Array('c','x','v');
	var isCtrl;

        if(window.event) {
		if(window.event.ctrlKey)
			isCtrl = true;
		else
			isCtrl = false;
        }
        else {
                if(e.ctrlKey)
                	isCtrl = true;
                else
                	isCtrl = false;	}

	if(isCtrl) {
		for(i=0; i < forbiddenKeys.length; i++) {
			if(forbiddenKeys[i] == String.fromCharCode(window.event.keyCode).toLowerCase()) {
				alert('You can not copy and paste here');
				return false;
			}
        		}
	}
	return true;
}


//====================================================================================================
// check to see if input is alphabetic
function ForceAlphabetic(objField, strFieldName)
{
	if (objField.value.match(/^[.a-zA-Z\s]+$/))
	{
		return true;
	}
	else
	{
		alert(strFieldName + " must have only Alphabetic values");
		objField.focus();
		return false;
	} 
}
//===============================================================================================================
function noCopyMouse(e) 
{
//debugger;
    var message="";
	if (event.button == 2 || event.button == 3) 
	{
	    //this.focus();
		alert('You can not copy and paste here');
		return false;
    }
	return true;
}
//===============================================================================================================
//~ Check whether a given Email is Valid or not.
function IsEmailValid(objField, strFieldName,IsMandatory)
{
 // To find out the Value of the control
 //debugger;
 var strFieldValue=objField.value;
 if (IsMandatory=="T")
    {
        if (IsEmpty(strFieldValue))
        {
            alert(strFieldName + " can not be left blank");
            objField.focus();
            return false;
        }    
    }
    if (IsMandatory=="F")
    {
        if (IsEmpty(strFieldValue))
        {
            return true;
        }
    }
    var at="@";
    var dot=".";
    var underscore="_";
    var lat=strFieldValue.indexOf(at);
    var lstr=strFieldValue.length;
    var ldot=strFieldValue.indexOf(dot);
    var lastdot=strFieldValue.lastIndexOf(dot);
    var lunderScore=strFieldValue.indexOf(underscore);
    var lastunderScore=strFieldValue.lastIndexOf(underscore);
    var i,d=0,s=0,ui=0,countDot=0,countUS=0;
    var count=0;
 
    if (strFieldValue.indexOf(at)==-1)
    {
        alert("E - mail should be in abc@xyz.com format");
       objField.focus();
       return false;
    }
    if (strFieldValue.indexOf(at) == -1 || strFieldValue.indexOf(at) == 0 || strFieldValue.indexOf(at) == lstr)
    {
       alert("E - mail should be in abc@xyz.com format");
       objField.focus();
       return false;
    }
    if (strFieldValue.lastIndexOf(dot)== lstr-1 || strFieldValue.indexOf(dot)== -1 || strFieldValue.indexOf(dot)== 0  )
    {
       alert("E - mail should be in abc@xyz.com format");
       objField.focus();
       return false;
    }
 
    if(!((strFieldValue.charCodeAt(0)>=65 && strFieldValue.charCodeAt(0)<=90) || (strFieldValue.charCodeAt(0)>=97 && strFieldValue.charCodeAt(0)<=122)))
    {   
        alert("E - mail first letter should be character ");
         objField.focus();
         return false;
     }
 
    if ((ldot+1)  == lstr)
    {
 
        alert("E - mail should be in abc@xyz.com format");
        objField.focus();
        return false;
    }
     if (strFieldValue.indexOf(at,(lat+1))!= -1)
     {
        alert("E - mail should be in abc@xyz.com format");
        objField.focus();
        return false;
     }
     if (strFieldValue.substring(lat-1,lat) == dot || strFieldValue.substring(lat+1,lat+2)== dot)
     {
       alert("E - mail should be in abc@xyz.com format");
       objField.focus();
       return false;
     }
 
     if (strFieldValue.indexOf(dot,(lat+2))== -1)
     {
        alert("E - mail should be in abc@xyz.com format");
        objField.focus();
        return false;
     }
 
     if (strFieldValue.indexOf(" ")!= -1)
     {
         alert("E - mail should be in abc@xyz.com format");
         objField.focus();
         return false;
     }
      
     
     for(i=lat;i<lstr;i++)
     {
     if(strFieldValue.charAt(i)== dot)
     count=count+1;
     }
     if(count>2)
     { 
     alert("E - mail should be in abc@xyz.com format");
         objField.focus();
         return false;
     }
 

    for(i=strFieldValue.lastIndexOf(dot)+1;i<lstr;i++)
    {
    d=d+1;
    if(!((strFieldValue.charCodeAt(i)>=65 && strFieldValue.charCodeAt(i)<=90) || (strFieldValue.charCodeAt(i)>=97 && strFieldValue.charCodeAt(i)<=122)))
    { 
     alert("Domain in E - mail should be only characters");
         objField.focus();
         return false;
     }
    }
    if(d>5)
    { 
     alert("Domain in E - mail should not be more then 5");
         objField.focus();
         return false;
     }
 

    for(i=strFieldValue.indexOf(at)+1;i<=strFieldValue.lastIndexOf(dot);i++)
    {
    s=s+1;
    if(!((strFieldValue.charCodeAt(i)>=65 && strFieldValue.charCodeAt(i)<=90) || (strFieldValue.charCodeAt(i)>=97 && strFieldValue.charCodeAt(i)<=122) || (strFieldValue.charAt(i)>=0 && strFieldValue.charAt(i)<=9) || (strFieldValue.charAt(i)==dot)))
        {   
             alert(" E - mail should be in abc@xyz.com format ");
             objField.focus();
             return false;
         }
     }     
    if(s>20)
    {   
             alert(" E - mail should be in abc@xyz.com format ");
             objField.focus();
             return false;
         }
         
     if(strFieldValue.charAt(lastdot-1)==dot)
     {   
             alert(" E - mail should be in abc@xyz.com format ");
             objField.focus();
             return false;
         }
         
         if(strFieldValue.charAt(lastunderScore-1)==underscore)
        {   
             alert(" E - mail should be in abc@xyz.com format ");
             objField.focus();
             return false;
         }
         
         if(strFieldValue.charAt(lunderScore+1)==dot)
     {   
             alert(" E - mail should be in abc@xyz.com format ");
             objField.focus();
             return false;
         }
         
         if(strFieldValue.charAt(ldot+1)==underscore)
        {   
             alert(" E - mail should be in abc@xyz.com format ");
             objField.focus();
             return false;
         }
         
         if(strFieldValue.charAt(lat-1)==underscore)
        {   
             alert(" E - mail should be in abc@xyz.com format ");
             objField.focus();
             return false;
         }
         
         if(strFieldValue.charAt(lat-1)==dot)
        {   
             alert(" E - mail should be in abc@xyz.com format ");
             objField.focus();
             return false;
         }
     
    for(i=0;i<lat;i++)
    {
    if(strFieldValue.charAt(i)==dot)
    {
    countDot=countDot+1;
    }
    ui=ui+1;
    }
    if(ui>50)
    {   
             alert("E - mail should be less then 50 character ");
             objField.focus();
             return false;
     }
     if(countDot>1)
    {   
             alert("E - mail should not contain more then one dot");
             objField.focus();
             return false;
     }
 

    for(i=0;i<lat;i++)
    {
    if(strFieldValue.charAt(i)==underscore)
    {
    countUS=countUS+1;
    }
    }
 
    if(countUS>2)
    {
    alert(" E - mail should be in abc@xyz.com format ");
             objField.focus();
             return false;
     }
        
     for(i=1;i<lstr;i++)
     {
        if(!((strFieldValue.charCodeAt(i)>=65 && strFieldValue.charCodeAt(i)<=90) || (strFieldValue.charCodeAt(i)>=97 && strFieldValue.charCodeAt(i)<=122) || (strFieldValue.charAt(i)== '_') || (strFieldValue.charAt(i)>=0 && strFieldValue.charAt(i)<=9) || (strFieldValue.charAt(i)==dot) || (strFieldValue.charAt(i)==at)))
        {   
             alert(" E - mail should be in abc@xyz.com format ");
             objField.focus();
             return false;
         }
      }
   return true;     
       
}
//===============================================================================================================
//^ Returns true if the string passed is a valid non-decimal numeric value
//^ no characters except a -ve sign at the begining is accepted
//^ otherwise, it displays an error message
//^ Also Forces the user to enter a value > 0

function ForceNumericEntry(objField, strFieldName,IsMandatory)
{
	var strFieldValue = new String(objField.value);
    var intLoopCounter = 0;
    
    //^ If mandatory field => testing for blank or spaces
    if (IsMandatory=="T")
    {
        if (IsEmpty(strFieldValue))
        {
            alert(strFieldName + " can not be left blank");
            objField.focus();
            return false;
        }
    }
    else    //^ If non-mandatory field => blank or spaces is ignored
    if (IsMandatory=="F")
    {
        if (IsEmpty(strFieldValue))
        {
            return true;
        }
    }

    // Checking for spaces at the begining of text.
    if (strFieldValue.indexOf(" ")!= -1)
    {
        alert(strFieldName + " must be a valid No.");
        objField.focus();
        return false;
    }

    // Validating for a numeric non-decimal value.
	for (intLoopCounter = 0; intLoopCounter < strFieldValue.length; intLoopCounter++)
	{
		//if ((strFieldValue.charAt(intLoopCounter) < '0' || strFieldValue.charAt(intLoopCounter) > '9') && (strFieldValue.charAt(0) != '-'))
		if  (strFieldValue.charAt(intLoopCounter) == '.')
		{
			alert(strFieldName + " does not accept a decimal value.");
			objField.focus();
			return false;
		}
		if  ((strFieldValue.charAt(intLoopCounter) > '9') && (strFieldValue.charAt(0) != '-'))
		{
			alert(strFieldName + " must be a valid numeric value.");
			objField.focus();
			return false;
		}
	}
		
    //^ Validating for the Valid Numeric value to be > 0		
    var strLength = strFieldValue.length;
    intLoopCounter = 0;
    var ZeroValue = "Y";
    
    while (intLoopCounter < strLength)
    {
            if (strFieldValue.charAt(intLoopCounter) == '0')
            {
                intLoopCounter = intLoopCounter + 1;
            }
            else
            {
                ZeroValue = "N"  
                break ;
            }
    }
    if (ZeroValue == "Y")
    {
		alert(strFieldName + " must be greater than 0");
		objField.focus();
		return false;
    }
    
	return true;
}



//===============================================================================================================
 //===============================================================================================================
//# Function for restricting Multiline textbox length upto a passed parameter length (General Function)
function check(obj,objLen)
{
	//debugger;
	var oLen = objLen
	var value = obj.innerText
    //var value=document.getElementById("txtDescription").innerText;  
    var len = value.length;
    if(Number(len)>Number(oLen))
    {
      if((event.keyCode >= 96 && event.keyCode <= 105) ||
      (event.keyCode > 32 && event.keyCode < 48) ||
      (event.keyCode > 57 && event.keyCode < 65) ||
      (event.keyCode > 90 && event.keyCode < 96) ||
      (event.keyCode == 189 || event.keyCode == 109))
    
        {
            return true; 
        }
         else
        {
             return false;
        }
    }
    else
    {
        return true;
    }
}
//===============================================================================================================
//Function for validating a textbox for not to be blank and not to take spaces
function ForceDataEntry(objField, strFieldName)
{
	// To find out the Value of the control
	var strFieldValue=objField.value;
	if (trimAll(objField.value) == '0' || trimAll(objField.value) == '0.00' || trimAll(objField.value) == '') 
	{
		alert("You need to enter information for " + strFieldName);
		objField.focus();
		return false;
	}
	else if (IsEmpty(strFieldValue))
	{
		alert("You need to enter information for " + strFieldName);
		objField.focus();
		return false;
	}
	return true;
}
//==============================================================================================================
 //^ Function for trim any string
function trimAll( strValue )
{				
	var objRegExp = /^(\s*)$/;
     //check for all spaces
	if(objRegExp.test(strValue))
	 {
		strValue = strValue.replace(objRegExp, '');
		if( strValue.length == 0)
			return strValue;
		 }
     //check for leading & trailing spaces
    objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
    if(objRegExp.test(strValue)) 
     {
    	//remove leading and trailing whitespace characters
		strValue = strValue.replace(objRegExp, '$2');
	 }
		return strValue;
}
 
//================================================================================================================
//* Check whether a given string is empty or not.
function IsEmpty(strTxtBoxValue)
{   if ((strTxtBoxValue == null) || (strTxtBoxValue.length == 0))
	{
		return true;			
	}	
}

//===============================================================================================================

//~ Check whether a given DropDown value is empty or not.
function IsComboEmpty(ddlValue)
{ 
    if  ((ddlValue == 0) || (ddlValue == '--Select--'))
    {
    return true; //^ Returning True, if Empty Text Box found
    } 
}
//===============================================================================================================
//~ Checks to see if a DropDownList is empty. If so, an error message is displayed.
function ForceComboItems(ddlName, strDdlName)
{
	// To find out the Value of the control0.
	var ItemCount=ddlName.value;
	if (IsComboEmpty(ItemCount))
		{
			alert("First Select the " + strDdlName);
			ddlName.focus();
			return false;
		}
	return true;
}
//===============================================================================================================
//# Function for validating Today Special Menu Form
function ValidateTodaySpecial()
{
	if (!ForceDataEntry(document.Form1.txtSubMenuName,'Sub Menu Name')) return false;
	if (!ForceComboItems (document.Form1.ddlOrder,'Set Order')) return false;
}
//===============================================================================================================
//# Function for validating Today Special Item Form
function ValidateTodaySpecialItem()
{
	if (!ForceComboItems (document.Form1.ddlSubMenu,'Sub Menu')) return false;
	if (!ForceComboItems (document.Form1.ddlOrder,'Set Order')) return false;
	if (!ForceDataEntry(document.Form1.txtFoodItemName,'Sub Menu Item Name')) return false;
	if (!ForceDataEntry(document.Form1.txtPrice,'Price')) return false;
	var val = document.Form1.txtPrice.value;
	if(isNaN(val))
	{
			alert("Price must be a valid number");
            document.Form1.txtPrice.focus();
            return false;
	}
	 var value = document.Form1.txtPrice.value;
            var len = value.length;
            var index = value.indexOf('.');
            if (Number(index)!= -1)
            {
                value = value.substring(Number(index)+1,Number(len));
                len = value.length;
                if (len>2)
                {
                    alert("Price can have value upto 2 decimal places only");
                    document.Form1.txtPrice.focus();
                    return false;
                }
             }
}
//===============================================================================================================
//# Function for validating Login Form
function validateLogin()
{
	if (!ForceDataEntry(document.Form1.txtLoginId,'Login Id')) return false;
	if (!ForceDataEntry(document.Form1.txtPassword,'Password')) return false;
}
//==============================================================================================================

// function validateRegister()
// {
//// debugger;
//	if (!IsEmailValid(document.Form1.txtemail,'Email Address','T')) return false;
//	if (!ForceDataEntry (document.Form1.txtpwd,'Password')) return false;
//	if (document.Form1.txtpwd.value.length<6)
//	{
//		alert("Password should be of atleast 6 characters");
//		document.Form1.txtpwd.focus();
//		return false;
//	}
//	if (!ForceDataEntry (document.Form1.txtConfirmPassword,'Confirm Password')) return false;
//	if (document.Form1.txtpwd.value != document.Form1.txtConfirmPassword.value)
//	{
//		alert("Password and Confirm Password should be same");
//		document.Form1.txtConfirmPassword.focus();
//		return false;
//	}
//	if (!ForceComboItems (document.Form1.ddltitle,'Title')) return false;
//	if (!ForceDataEntry (document.Form1.txtfname,'First Name')) return false;
//	if (!ForceDataEntry (document.Form1.txtaddress1,'Delivery Address')) return false;
//	if (!ForceDataEntry (document.Form1.txtcity,'City')) return false;
//	if (!ForceDataEntry (document.Form1.txtstate,'State')) return false;
//	if (!ForceNumericEntry(document.Form1.txtzip,'Zip Code','T')) return false;
//	if (!ForceNumericEntry(document.Form1.txtphone,'Phone','T')) return false;
//	
// }

 //===========================================================================================================
  function validateUserDetail()
 {
//debugger;
	
//	if (!ForceComboItems (document.Form1.ddltitle,'Title')) return false;
	if (!ForceDataEntry (document.Form1.txtfname,'First Name')) return false;
	if (!ForceAlphabetic(document.Form1.txtfname,'First Name')) return false;
	if(document.Form1.txtlastname.value ==""){}
	else
	{
	    if (!ForceAlphabetic(document.Form1.txtlastname,'Last Name')) return false;
	}

	if (!ForceDataEntry (document.Form1.txtaddress1,'Address')) return false;
	if (!ForceDataEntry (document.Form1.txtcity,'City')) return false;
	if (!ForceAlphabetic(document.Form1.txtcity,'City')) return false;
	if (!ForceDataEntry (document.Form1.txtstate,'State')) return false;
	if (!ForceAlphabetic(document.Form1.txtstate,'State')) return false;
	if (!ForceNumericEntry(document.Form1.txtzip,'Zip Code','T')) return false;
	if (!ForceNumericEntry(document.Form1.txtphone,'Phone','T')) return false;
	if (document.Form1.txtphone.value.length<10)
	{
		alert("Phone no should be of atleast 10 digits");
		document.Form1.txtphone.focus();
		return false;
	}
	
	
 }
 
 //==============================================================================================================
 function validateRegister()
 {
// debugger;
	if (!IsEmailValid(document.Register.txtemail,'Email Address','T')) return false;
	if (!ForceDataEntry (document.Register.txtpwd,'Password')) return false;
	if (document.Register.txtpwd.value.length<6)
	{
		alert("Password should be of atleast 6 characters");
		document.Register.txtpwd.focus();
		return false;
	}
	if (!ForceDataEntry (document.Register.txtConfirmPassword,'Confirm Password')) return false;
	if (document.Register.txtpwd.value != document.Register.txtConfirmPassword.value)
	{
		alert("Password and Confirm Password should be same");
		document.Register.txtConfirmPassword.focus();
		return false;
	}
	if (!ForceComboItems (document.Register.ddltitle,'Title')) return false;
	if (!ForceDataEntry (document.Register.txtfname,'First Name')) return false;
	if (!ForceAlphabetic(document.Register.txtfname,'First Name')) return false;
	if(document.Register.txtlastname.value ==""){}
	else
	{
	    if (!ForceAlphabetic(document.Register.txtlastname,'Last Name')) return false;
	}
	if (!ForceDataEntry (document.Register.txtaddress1,'Delivery Address')) return false;
	if (!ForceDataEntry (document.Register.txtcity,'City')) return false;
	if (!ForceAlphabetic(document.Register.txtcity,'City')) return false;
	if (!ForceDataEntry (document.Register.txtstate,'State')) return false;
	if (!ForceAlphabetic(document.Register.txtstate,'State')) return false;
	if (!ForceNumericEntry(document.Register.txtzip,'Zip Code','T')) return false;
	if (!ForceNumericEntry(document.Register.txtphone,'Phone','T')) return false;
		if (document.Register.txtphone.value.length<10)
	{
		alert("Phone no should be of atleast 10 digits");
		document.Register.txtphone.focus();
		return false;
	}
	
 }
 
  //==============================================================================================================
 function validateForgotPassword()
 {
// debugger;
	if (!IsEmailValid(document.ForgotPwd.txtemail,'Email Address','T')) return false;
		
 }
 
 //===============================================================================================================
//# Function for validating VIP List Form
function validateVIPList()
{
	if (!IsEmailValid(document.Form1.txtEmail,'E - mail','T')) return false;
	if (!ForceDataEntry (document.Form1.txtName,'Name')) return false;
	if (!ForceAlphabetic(document.Form1.txtName,'Name')) return false;
	if (!ForceDataEntry (document.Form1.txtAddress,'Address')) return false;
	if (document.Form1.txtAddress.value.length>255)
	{
		alert("Only 255 characters can be entered for address");
		document.Form1.txtAddress.focus();
		return false;
	}
	if (!ForceDataEntry (document.Form1.txtcity,'City')) return false;
	if (!ForceAlphabetic(document.Form1.txtcity,'City')) return false;
	if (!ForceDataEntry (document.Form1.txtstate,'State')) return false;
	if (!ForceAlphabetic(document.Form1.txtstate,'State')) return false;
	if (!ForceNumericEntry(document.Form1.txtzip,'Zip Code','T')) return false;
	if (!ForceDataEntry (document.Form1.txtphone,'Phone')) return false;
	//if (!ForceNumericEntry(document.Form1.txtphone,'Phone','T')) return false;
	if (!ForceNumericEntry(document.Form1.txtextension,'Extn','F')) return false;
}

function validateOrder()
{
   
    if (!ForceDataEntry (document.myorders.txtDate,'Date')) return false;   
}
