﻿var AddTag = "Click to add tag.";
var AddQuote = "Click to add a Quote. To add multiple Quotes use upload.";
var RequiredField = "Required field.";
var FieldToLong = "Too long.";

function KeyDownHandler(btn)
{
    // process only the Enter key
    if (event.keyCode == 13)
    {
        // cancel the default submit
        event.returnValue=false;
        event.cancel = true;
        // submit the form by programmatically clicking the specified button
        btn.click();
    }
}

function ValidatorSearch()
{

    var txtSearch = document.getElementById("ctl00_Content_txtSearch");
    var lblSearchError = document.getElementById("lblSearchError");
    
    if (trim(txtSearch.value).length >= 3){
        lblSearchError.style.visibility = "hidden";
        return true;
    }
    else
    {
        lblSearchError.style.visibility = "visible";
        return false;
    }
}

function trim(value) {
  value = value.replace(/^\s+/,''); 
  value = value.replace(/\s+$/,'');
  return value;
}

function ValidatorAddQuote()
{
    var quoteInput = document.getElementById("ctl00_Content_txtQuote");
    var tagInput = document.getElementById("ctl00_Content_txtTag");
    var validationError;
    
    //validate Quotation
    if (quoteInput.value == AddQuote){
        txtQuoteError.style.visibility = "visible";
        validationError = true;
    }
    else{txtQuoteError.style.visibility = "hidden";
    }
    //validate Tag
    if (GenericValidator(tagInput, txtTagError, AddTag)){
        validationError = true;
    }
    //return value           
    if (validationError){
        return false;
    }
    else{
        return true;
    }
}      

function ValidatorAddQuoteCopy()
{
    var quoteInput = document.getElementById("ctl00_Content_txtQuoteCopy");
    var tagInput = document.getElementById("ctl00_Content_txtTagCopy");
    var validationError;
    
    //validate Quotation
    if (quoteInput.value == AddQuote){
        txtQuoteErrorCopy.style.visibility = "visible";
        validationError = true;
    }
    else{txtQuoteErrorCopy.style.visibility = "hidden";
    }
    //validate Tag
    if (GenericValidator(tagInput, txtTagErrorCopy, AddTag)){
        validationError = true;
    }
    //return value           
    if (validationError){
        return false;
    }
    else{
        return true;
    }
}      

function ValidatorAddQuoteFile()
{
    var quoteFileUpload = document.getElementById("ctl00_Content_quoteFileUpload");
    var tagInput = document.getElementById("ctl00_Content_txtTagFile");
    var validationError;
    
    //validate QuotationFile
    if (quoteFileUpload.value == "") {
        txtQuoteFileError.style.visibility = "visible";
        validationError = true;
    }
    else {txtQuoteFileError.style.visibility = "hidden";
    }
    //validate Tag
    if (GenericValidator(tagInput, txtTagFileError, AddTag)){
        validationError = true; 
    }
    //return value
    if (validationError){
        return false;
    }
    else{
        return true;
    }
}      

function GenericValidator(input, error, watermark)
{
    if (input.value == watermark){                
        error.style.visibility = "visible";
        error.innerText = RequiredField;
        return true;
    }
    else{
        if (input.value.length < 40){
            error.style.visibility = "hidden"; 
        }
        else{
            error.innerText = FieldToLong;
            error.style.visibility = "visible";
            return true;                        
        }
    }
}

function ShowTag(a)
{
    prefix = "ctl00_Content_QuotationControl";
    idStr = a.id;
    idStr = idStr.replace(prefix, "")
    idStr = idStr.replace("_dotsAddTag", "")

    var tagText = document.getElementById(prefix + idStr + "_txtQuotationsTag")
    var addButton = document.getElementById(prefix + idStr + "_btnQuoteTag")

    if (tagText.style.visibility == "visible"){
        tagText.style.visibility = "hidden";
        addButton.style.visibility = "hidden";    
    }
    else{
        tagText.style.visibility = "visible";
        addButton.style.visibility = "visible";        
    }

}

