// JavaScript Document

function validate_blocked()
{   
    alert("This book has been added to another user's cart.\n\nWe suggest you refresh this page in 300 seconds (5 minutes).\nIf the user has not processed his order, it will become available to you.");
    return false;
}

function validate_add_to_cart(FormName, ElementName)
{   
    var SKU = document.forms[FormName].elements[ElementName].value; 
    var WhiteSpaces = /\s/g; //Match any white space including space, tab, form-feed, etc.
    
    if(SKU.replace(WhiteSpaces, '') == '')
    {
        alert('There seems to be a problem with this link. Try refreshing the page if this happens more than once.');
        return false;
    }
    else
    {
        return true;
        /*
        if(confirm('Add this book to your cart?') )
        {
            return true;
        }
        else
        {
            return false;
        }
        */
    }
}

function validate_full_cart(FormName, ElementName)
{
    alert("Your cart is full.\n\n1. Process this cart by clicking on the 'CHECK OUT' button. \n                                       OR\n2. Remove an item from your cart by clicking on the 'View' link.");
    return false;
}

function validate_remove_from_cart(FormName, ElementName)
{   
    var CartContentID = document.forms[FormName].elements[ElementName].value; 
    var WhiteSpaces = /\s/g; //Match any white space including space, tab, form-feed, etc.
    
    if(CartContentID.replace(WhiteSpaces, '') == '')
    {
        alert('There seems to be a problem with this link. Try refreshing the page if this happens more than once.');
        return false;
    }
    else
    {
        if(confirm('Do you really want to remove this book from your cart?') )
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

function validate_search_inventory(FormName, ElementName)
{
    var Title = 'Search Text';
    
    if(validate_blank(FormName, ElementName) )
    {
        return true;
    }
    else
    {
        alert(Title + ' cannot be blank');
        document.forms[FormName].elements[ElementName].select();   
        document.forms[FormName].elements[ElementName].focus();    
        return false;
    }
} 

function validate_email(FormName, ElementName)
{
    var EmailId = document.forms[FormName].elements[ElementName].value;
    var WhiteSpaces = /\s/g; //Match any white space including space, tab, form-feed, etc. 
    var atSymbol = '@';
  	var dotSymbol = '.';
  	var LengthOfId = EmailId.length;
  	var atSymbolPosition = EmailId.indexOf(atSymbol);
  	
  	//
  	if(EmailId.replace(WhiteSpaces, '') == '')
    {
        alert('Please enter the email');
        document.forms[FormName].elements[ElementName].select();   
        document.forms[FormName].elements[ElementName].focus();    
        return false;
    }
  	
  	//checks for occurrence of '@' symbol		
  	if(EmailId.indexOf(atSymbol)==-1 || EmailId.indexOf(atSymbol)==0 || EmailId.indexOf(atSymbol)==LengthOfId)
  	{
  			alert("Email id does not contain '@' symbol");
        document.forms[FormName].elements[ElementName].select();   
        document.forms[FormName].elements[ElementName].focus();    
        return false;
  	}
  
  	//checks for occurrence of '.' symbol		
  	if(EmailId.indexOf(dotSymbol)==-1 || EmailId.indexOf(dotSymbol)==0 || EmailId.indexOf(dotSymbol)==(LengthOfId-1))
  	{
  	   	alert("Email id does not contain '.' symbol");
        document.forms[FormName].elements[ElementName].select();   
        document.forms[FormName].elements[ElementName].focus();    
        return false;
  	}
  
  	//checks for recurrence of @ symbol
  	if(EmailId.indexOf(atSymbol,(atSymbolPosition+1))!=-1)
  	{
  			alert("Email id cannot have more than one '@' symbol");
        document.forms[FormName].elements[ElementName].select();   
        document.forms[FormName].elements[ElementName].focus();    
        return false;
  	}
  
  	//checks if '.' symbol occurs next to '@" symbol
  	if(EmailId.substring(atSymbolPosition-1,atSymbolPosition)==dotSymbol || EmailId.substring(atSymbolPosition+1,atSymbolPosition+2)==dotSymbol)
  	{
  			alert("Please enter valid email-id");
        document.forms[FormName].elements[ElementName].select();   
        document.forms[FormName].elements[ElementName].focus();    
        return false;
  	}
  
  	//checks if '.' symbol occurs two-characters-after-'@'-symbol or later
  	if(EmailId.indexOf(dotSymbol,(atSymbolPosition+2))==-1)
  	{
  			alert("Please enter valid email-id");
        document.forms[FormName].elements[ElementName].select();   
        document.forms[FormName].elements[ElementName].focus();    
        return false;
  	}
  	
  	//checks for at least 2 alphabets after last '.' symbol
  	
  	if(!isAlphabet(EmailId.charAt(LengthOfId - 1) ) || !isAlphabet(EmailId.charAt(LengthOfId - 2) ))
    {
  			alert("Please enter valid email-id");
        document.forms[FormName].elements[ElementName].select();   
        document.forms[FormName].elements[ElementName].focus();    
        return false;
    }
  		
  	//checks for blank spaces		
  	if(EmailId.indexOf(" ")!=-1)
  	{
  			alert("Email id cannot have blank spaces");
        document.forms[FormName].elements[ElementName].select();   
        document.forms[FormName].elements[ElementName].focus();    
        return false;
  	}
    
    return true;
}


function validate_password(FormName, ElementName, Title)
{   
    if(validate_blank(FormName, ElementName) )
    {
        if(validate_blank_spaces(FormName, ElementName) )
        {            
            if(validate_alpha_numeric(FormName, ElementName, '') )
            {            
                if(validate_length(FormName, ElementName, 5) )
                {            
                    return true;
                }
                else
                {
                    alert(Title + ' must be at least 5 characters');
                    document.forms[FormName].elements[ElementName].select();   
                    document.forms[FormName].elements[ElementName].focus();    
                    return false;
                }
            }
            else
            {
                alert(Title + ' can contain only alphabets and numbers');
                document.forms[FormName].elements[ElementName].select();   
                document.forms[FormName].elements[ElementName].focus();    
                return false;
            }
        }
        else
        {
            alert(Title + ' cannot contain blank spaces');
            document.forms[FormName].elements[ElementName].select();   
            document.forms[FormName].elements[ElementName].focus();    
            return false;
        }
    }
    else
    {
        alert(Title + ' cannot be blank');
        document.forms[FormName].elements[ElementName].select();   
        document.forms[FormName].elements[ElementName].focus();    
        return false;
    }  
} 


function validate_numeric(FormName, ElementName)
{
    var InputString = document.forms[FormName].elements[ElementName].value;
      
    for(var LoopIndex = 0; LoopIndex < InputString.length; LoopIndex++)
    {
        if(!isDigit(InputString.charAt(LoopIndex) ) )
        {
            return false;
        }
    }
    
    return true;
}

function isDigit(CurrentChar) 
{
    if(CurrentChar.length > 1)
    {
        return false;
    }
    
    var DigitsString = '0123456789';
    
    if(DigitsString.indexOf(CurrentChar) != -1)
    {
        return true;
    }
    else
    {
        return false;
    }    
}

function validate_alphabets(FormName, ElementName)
{
    var WhiteSpaces = /\s/g; //Match any white space including space, tab, form-feed, etc. 
    var InputString = document.forms[FormName].elements[ElementName].value;    
    var InputTrimmed = InputString.replace(WhiteSpaces, ''); 
    
    for(var LoopIndex = 0; LoopIndex < InputTrimmed.length; LoopIndex++)
    {
        if(!isAlphabet(InputTrimmed.charAt(LoopIndex) ) )
        {
            return false;
        }
    }
    
    return true;   
}

function isAlphabet(CurrentChar) 
{
    if(CurrentChar.length > 1)
    {
        return false;
    }
    
    if((CurrentChar >= 'a' && CurrentChar <= 'z') || (CurrentChar >= 'A' && CurrentChar <= 'Z') )
    {
        return true;
    }
    else
    {
        return false;
    }    
}

function validate_alpha_numeric(FormName, ElementName, SpecialCharacters)
{
    var WhiteSpaces = /\s/g; //Match any white space including space, tab, form-feed, etc. 
    var InputString = document.forms[FormName].elements[ElementName].value;    
    var InputTrimmed = InputString.replace(WhiteSpaces, ''); 
    
    for(var LoopIndex = 0; LoopIndex < InputTrimmed.length; LoopIndex++)
    {
        if(!isAlphaNumeric(InputTrimmed.charAt(LoopIndex), SpecialCharacters) )
        {
            return false;
        }
    }
    
    return true;   
}

function isAlphaNumeric(CurrentChar, SpecialCharacters) 
{
    if (CurrentChar.length > 1)
    {
        return false;
    }
    
    var WhiteSpaces = /\s/g; //Match any white space including space, tab, form-feed, etc.     
    var DigitsString = '0123456789';
    
    SpecialCharacters = SpecialCharacters.replace(WhiteSpaces, ''); 
    DigitsString += SpecialCharacters;
    
    if((CurrentChar >= 'a' && CurrentChar <= 'z') || (CurrentChar >= 'A' && CurrentChar <= 'Z') || (DigitsString.indexOf(CurrentChar) != -1) )
    {
        return true;
    }
    else
    {
        return false;
    }    
}

function validate_decimal(FormName, ElementName, DecimalPlaces)
{
    var InputNumber = document.forms[FormName].elements[ElementName].value;   
    return(parseFloat(InputNumber, DecimalPlaces) == (InputNumber * 1) );
}

function validate_blank(FormName, ElementName)
{   
    var WhiteSpaces = /\s/g; //Match any white space including space, tab, form-feed, etc. 
    var InputString = document.forms[FormName].elements[ElementName].value;   
    var InputTrimmed = InputString.replace(WhiteSpaces, ''); 
    
    if(InputTrimmed == '' || InputTrimmed == null)
    {
        document.forms[FormName].elements[ElementName].value = InputTrimmed;
        return false;
    }
    else
    {
        return true;
    }
}

function validate_blank_spaces(FormName, ElementName)
{   
    var WhiteSpaces = /\s/g; //Match any white space including space, tab, form-feed, etc. 
    var InputString = document.forms[FormName].elements[ElementName].value;   
    var InputTrimmed = InputString.replace(WhiteSpaces, ''); 
    
    if(InputTrimmed != InputString)
    {
        return false;
    }
    else
    {
        return true;
    }
}

function validate_set_length(FormName, ElementName, Length)
{   
    var InputString = document.forms[FormName].elements[ElementName].value;
    return (InputString.length == Length);
}

function validate_length(FormName, ElementName, Length)
{   
    var InputString = document.forms[FormName].elements[ElementName].value;
    return (InputString.length >= Length);
}

function validate_dd_common(FormName, ElementName, Title)
{
    if(validate_drop_down(FormName, ElementName) )
    {
        return true;
    }
    else
    {
        alert('Please select '+Title+' from the drop-down list');
        document.forms[FormName].elements[ElementName].focus();    
        return false;
    }
}  

function validate_drop_down(FormName, ElementName)
{   
    if(document.forms[FormName].elements[ElementName].selectedIndex == 0)
    {
        return false;
    } 
    else
    {
        return true;
    }
}



function validate_date(FormName, ElementName)
{
    var InputDate = document.forms[FormName].elements[ElementName].value;
    var ValidCharString = '0123456789/';
    
    if(InputDate.length < 10)
    {
        return false;
    } 
    
    for(var LoopIndex = 0; LoopIndex < InputDate.length; LoopIndex++)
    {
        var CurrentChar = InputDate.charAt(LoopIndex);
        
        if(ValidCharString.indexOf(CurrentChar) == -1)
        {
            return false;
        }
    }
    
    var year = InputDate.substring(6);
    var month = InputDate.substring(3,5);
    var day = InputDate.substring(0,2);
    
    if(year < 0 || year > 9999)
    {
        return false;
    }
    if(month < 0 || month > 12)
    {
        return false;
    }
    if(day < 0 || day > 31)
    {
        return false;
    }
    return true;
}

function validate_date_past(FormName, ElementName)
{
    var InputDate = document.forms[FormName].elements[ElementName].value;
    
    var now = new Date();
    now = now.getTime(); 
    
    var year = InputDate.substring(6);
    var month = InputDate.substring(3, 5);
    var day = InputDate.substring(0, 2); 
    
    var dateStart = new Date();
    
    dateStart.setYear(year);
    dateStart.setMonth(month - 1);
    dateStart.setDate(day);     
         	
    var checkStart = dateStart.getTime();  
    
    if(checkStart > now)
    {        
        return false;
    }
    else
    {
        return true;
    }
}

function LeftTrim(InputString) 
{
    if (InputString == null)
    {
        return InputString;
    }
    
    for(var LoopIndex = 0; InputString.charAt(LoopIndex) == ' ' || InputString.charAt(LoopIndex) == '\n' || InputString.charAt(LoopIndex) == '\t'; LoopIndex++);
    return InputString.substring(LoopIndex, InputString.length);
}

function RightTrim(InputString) 
{
    if (InputString==null)
    {
        return InputString;
    }
    
    for(var LoopIndex = InputString.length - 1; InputString.charAt(LoopIndex) == ' ' || InputString.charAt(LoopIndex) == '\n' || InputString.charAt(LoopIndex) == '\t'; LoopIndex--);
    return InputString.substring(0, LoopIndex + 1);
}

function validate_trim(FormName, ElementName)
{   
    var InputString = document.forms[FormName].elements[ElementName].value; 
      
    if(InputString == LeftTrim(RightTrim(InputString) ) )
    {
        return true;
    }
    else
    {
        return false;
    }
}

function validate_radio(FormName, ElementName)
{   
    var RadioButton = document.forms[FormName].elements[ElementName]; 
    var SelectedButton = -1;
    for (var index = 0; index < RadioButton.length; index++) 
    {
        if (RadioButton[index].checked) 
        {
            SelectedButton = index; 
            break;
        }
    }
    if (SelectedButton > -1) 
    {
        return true;
    }
    else
    {
        return false;
    }
}

function validate_name(FormName, ElementName, Title, MinLength)
{      
    if(validate_blank(FormName, ElementName) )
    {
        if(validate_blank_spaces(FormName, ElementName) )
        {            
            if(validate_alpha_numeric(FormName, ElementName, '') )
            {            
                if(validate_length(FormName, ElementName, MinLength) )
                {            
                    return true;
                }
                else
                {
                    alert(Title+' must be at least ' + MinLength + ' characters');
                    document.forms[FormName].elements[ElementName].select();   
                    document.forms[FormName].elements[ElementName].focus();    
                    return false;
                }
            }
            else
            {
                alert(Title+' can contain only alphabets and numbers');
                document.forms[FormName].elements[ElementName].select();   
                document.forms[FormName].elements[ElementName].focus();    
                return false;
            }
        }
        else
        {
            alert(Title+' cannot contain blank spaces');
            document.forms[FormName].elements[ElementName].select();   
            document.forms[FormName].elements[ElementName].focus();    
            return false;
        }
    }
    else
    {
        alert(Title+' cannot be blank');
        document.forms[FormName].elements[ElementName].select();   
        document.forms[FormName].elements[ElementName].focus();    
        return false;
    }  
}


function validate_amount(FormName, ElementName, Title)
{   
    if(validate_blank(FormName, ElementName) )
    {
        if(validate_trim(FormName, ElementName) )
        {            
            if(validate_decimal(FormName, ElementName, 2) )
            {
                return true;
            }
            else
            {
                alert(Title+' must be decimal');
                document.forms[FormName].elements[ElementName].select();   
                document.forms[FormName].elements[ElementName].focus();    
                return false;
            }
        }
        else
        {
            alert(Title+' cannot be start or end with blank spaces');
            document.forms[FormName].elements[ElementName].select();   
            document.forms[FormName].elements[ElementName].focus();    
            return false;
        }
    }
    else
    {
        alert(Title+' cannot be blank');
        document.forms[FormName].elements[ElementName].select();   
        document.forms[FormName].elements[ElementName].focus();    
        return false;
    }  
}