﻿

function fnAdvSrchMethods(funName)
{
    var syopsisValidationYN = "N";
    if(funName == "funSaveSearch" || funName == "funCopyAsNewSearch")
    {
        var SynopsisKeywords = window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_txtSynopsisKeywords");
        var chkShortSynopsis = window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_chkShortSynopsis");
        var chkMediumSynopsis = window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_chkMediumSynopsis");
        var chkLongSynopsis = window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_chkLongSynopsis");

        if (Trim(SynopsisKeywords.value) != "" && chkShortSynopsis.checked == false && chkMediumSynopsis.checked == false && chkLongSynopsis.checked == false)
        {
            syopsisValidationYN = "Y";
        }
    }
    if(syopsisValidationYN == "Y")
    {
        funBuildAlterMsg("Please select at lease on Synopsis to operate on.", "OK")
        toggleVis('saveSearchDialog'); 
    }
    else
    {
        if(funName == "funCopyAsNewSearch" || funName == "funRenameSavedSearch")
        {
            funBuildSaveSearchMsg("Save the title search as:", "SAVE", "CANCEL", "funAdvSrchEvents()");
        }
        else
        {
            funBuildSaveSearchMsg("Save new title search as:", "SAVE", "CANCEL", "funAdvSrchEvents()");
        }
        toggleVis('saveSearchDialog', funName); 
        if(window.document.getElementById("titleSearchTitle"))
        {
            window.setTimeout("setFocus()",500);
        }
    }
    return false;
}

function setFocus()
{
    if(window.document.getElementById("titleSearchTitle") && window.document.getElementById("saveSearchDialog").style.display == '')
    {
        window.document.getElementById("titleSearchTitle").focus();
    }
}

function funAdvSrchEvents()
{
    var hdnfunName = window.document.getElementById("hdnfunName").value;
    var strSaveSeachAs = window.document.getElementById("titleSearchTitle").value;
    var SavedSearchId = window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchSaveSearch");
    
    if(Trim(strSaveSeachAs) != "")
    {
        if(hdnfunName == "funSaveSearch")
        {
            funSaveSearch(strSaveSeachAs);
        }
        else if(hdnfunName == "funCopyAsNewSearch")
        {
            funCopyAsNewSearch(strSaveSeachAs);
        }
        else if(hdnfunName == "funRenameSavedSearch")
        {
            funRenameSavedSearch(strSaveSeachAs);
        }
    }
    
    window.document.getElementById("hdnfunName").value = "";
    if(window.document.getElementById("titleSearchTitle"))
    {
        window.document.getElementById("titleSearchTitle").value = "";
    }
    
    toggleVis('saveSearchDialog');
    return false;
}

/*On Search Button Click.*/
function funAdvanceSearch()
{
    // Added for showing Loading wait.. Message.
    toggleVis('divProgressDialog');
        
    window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchMode").value  = "search";
    window.document.forms[0].action = "AdvancedSearch.aspx";
    window.document.forms[0].method = "post";
    window.document.forms[0].__VIEWSTATE.name = "";
    window.document.forms[0].submit();
}
/*On Cancel button click just submiting the page to get the previous event values in control.*/
function funCancel()
{
    window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchMode").value  = "cancel";
    window.document.forms[0].action = "AdvancedSearch.aspx";
    window.document.forms[0].method = "post";
    window.document.forms[0].__VIEWSTATE.name = "";
    window.document.forms[0].submit();
}
function funClear() {
    window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchMode").value = "clear";
    window.document.forms[0].action = "AdvancedSearch.aspx";
    window.document.forms[0].method = "post";
    window.document.forms[0].__VIEWSTATE.name = "";
    window.document.forms[0].submit();
}

function funConfirmDeleteSavedSearch()
{
    var SavedSearchId = window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchSaveSearch").value;
    var hdnAdvSrchSearchName = window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchSearchName").value;
    if(SavedSearchId == "")
    {
        funBuildAlterMsg("Please select a Title Search to Delete.", "OK")
        toggleVis('saveSearchDialog');
    }
    else
    {
        if(hdnAdvSrchSearchName == "")
        {
            funBuildAlterMsg("Please select a Title Search to Delete.", "OK")
        }
        else
        {
            funBuildConfirmationMsg("Do You Want to Delete the record.", "OK", "Cancel", "funDeleteSavedSearch()");
        }
        toggleVis('saveSearchDialog');
    }
}
function funDeleteSavedSearch()
{
    toggleVis('saveSearchDialog');
    var strResult; 
    var SavedSearchId = window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchSaveSearch").value;
    var vardate = new Date();
    strResult = funGetHtmlFromJQuery("Xmlhttp.aspx","SearchId=" + SavedSearchId +"~"+ vardate +"&Type=DeleteSavedSearch");  
    funCancel();
    return false;    
}
function funCopyAsNewSearch(strSaveSeachAs)
{
    var strResult;
    var vardate = new Date();
    var SavedSearchId = window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchSaveSearch").value;
    
    strSaveSeachAs = Trim(strSaveSeachAs);
    strResult = funGetHtmlFromJQuery("Xmlhttp.aspx","SearchId=" + SavedSearchId +"~"+ vardate +"&SearchName="+strSaveSeachAs+"&Type=CopyAsNewSearch");  

    if(Trim(strResult) == "")
    {
        window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchSearchName").value = strSaveSeachAs;
        window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchSaveSearch").value = "";
        window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchMode").value  = "savesearch";
        window.document.forms[0].action = "AdvancedSearch.aspx";
        window.document.forms[0].method = "post";
        window.document.forms[0].__VIEWSTATE.name = "";
        window.document.forms[0].submit();
    }
    else
    {
        funBuildAlterMsg(strResult, "OK")
        toggleVis('saveSearchDialog');
    }
}

function funRenameSavedSearch(SavedSearchName)
{
    var strResult;
    var vardate = new Date();
    var SavedSearchId = window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchSaveSearch").value;
    
    SavedSearchName = Trim(SavedSearchName);
    strResult = funGetHtmlFromJQuery("Xmlhttp.aspx","SearchId=" + SavedSearchId +"~"+ vardate +"&SearchName="+SavedSearchName+"&Type=RenameSavedSearch");  
    if(Trim(strResult) == "")
    {
        window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchSearchName").value = SavedSearchName;
        window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchSaveSearch").value = SavedSearchId;
        window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchMode").value  = "getsavedsearch";
        window.document.forms[0].action = "AdvancedSearch.aspx";
        window.document.forms[0].method = "post";
        window.document.forms[0].__VIEWSTATE.name = "";
        window.document.forms[0].submit();
        
    }
    else
    {
        funBuildAlterMsg(strResult, "OK")
        toggleVis('saveSearchDialog');
    }    
}
function funGetAdvSrchDetails(SavedSearchId, SavedSearchName)
{
    window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchSaveSearch").value = SavedSearchId;
    window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchSearchName").value = SavedSearchName;
    window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchMode").value = "getsavedsearch";
    window.document.forms[0].action = "AdvancedSearch.aspx";
    window.document.forms[0].method = "post";
    window.document.forms[0].__VIEWSTATE.name = "";
    window.document.forms[0].submit();
}

function funSaveSearch(strtxtSaveSeachAs)
{
    if(window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchSaveSearch").value == "")
    {    
        var vardate = new Date();
        var strReturnValue = funGetHtmlFromJQuery("Xmlhttp.aspx","SearchName=" + strtxtSaveSeachAs +"~"+ vardate +"&Type=SearchNameValidation");  
        
        if(Trim(strReturnValue) != "")
        {
            funBuildAlterMsg(Trim(strReturnValue), "OK")
            toggleVis('saveSearchDialog');
        }
        else
        { 
            window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchSearchName").value = strtxtSaveSeachAs;
            window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchMode").value  = "savesearch";
            window.document.forms[0].action = "AdvancedSearch.aspx";
            window.document.forms[0].method = "post";
            window.document.forms[0].__VIEWSTATE.name = "";
            window.document.forms[0].submit();
        }
    }
    else
    {
        var flag = "Y";
        if(window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchSearchName").value == "")
        {
            var vardate = new Date();
            var strReturnValue = funGetHtmlFromJQuery("Xmlhttp.aspx","SearchName=" + strtxtSaveSeachAs +"~"+ vardate +"&Type=SearchNameValidation");  
            
            if(Trim(strReturnValue) != "")
            {
                flag = "N";
                funBuildAlterMsg(Trim(strReturnValue), "OK")
                toggleVis('saveSearchDialog');
            }            
        }
        
        if(flag == "Y")
        {
            window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchSearchName").value = strtxtSaveSeachAs;
            window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchMode").value  = "savesearch";
            window.document.forms[0].action = "AdvancedSearch.aspx";
            window.document.forms[0].method = "post";
            window.document.forms[0].__VIEWSTATE.name = "";
            window.document.forms[0].submit();
        }
    }
}

/*Function to concatenate the selected Items.*/
function funGetChkdItem(strValue,hdnVar)
{
    var hdnId= window.document.getElementById(hdnVar);
    if(hdnId.value.indexOf(strValue) > -1)
    {
        hdnId.value = hdnId.value.replace(strValue+",","");
    }
    else
    {
        hdnId.value += strValue + ",";
    }   
}
function funSetRadioValues(value, hdnId)
{
    var contentPlaceHolderId = "ctl00_ctl00_BodyContent_SubBodyContent_";     
    window.document.getElementById(contentPlaceHolderId + hdnId).value = "";
    window.document.getElementById(contentPlaceHolderId + hdnId).value = value;
    
    return false;
}




function funAlphaNumeric(obj)
{
    var objRegExp = /^[\w ]+$/;
    if(obj.value != "")
    {
        if(!objRegExp.test(obj.value))
        {
            funBuildAlterMsg("Please Enter AlphaNumerics", "OK")
            toggleVis('saveSearchDialog');
            obj.value = "";
            obj.focus();
        }

    }
}

function funValidateYear(obj)
{
   var objRegExp = /^\d{4,4}$/;
    if(obj.value != "")
    {
        if(!objRegExp.test(obj.value))
        {
           funBuildAlterMsg("Please Enter year in this format YYYY", "OK")
            toggleVis('saveSearchDialog');
            obj.value = "";
            obj.focus();
        }
    }
}
 
 function funCharacters(obj)
{
    var objRegExp = /^[A-Z ]+$/i;
    if(obj.value != "")
    {
        if(!objRegExp.test(obj.value))
        {
           funBuildAlterMsg("Please Enter characters", "OK")
            toggleVis('saveSearchDialog');
            obj.value = "";
            obj.focus();
        }

    }
}

function  funDecimal(obj) 
{
	var objRegExp  =   /(^\d\d*\.\d\d$)|(^\d\d*\.\d$)|(^\d\d*\.$)|(^\d*\,\d*\.$)|(^\d*\,\d*\.\d$)|(^\d*\,\d*\.\d\d$)|(^\d\d*$)|(^\d*\,\d*$)|(^\d*\,\d*\,\d*$)|(^\d*\,\d*\,\d*\.$)|(^\d*\,\d*\,\d*\.\d$)|(^\d*\,\d*\,\d*\.\d\d$)|(^\d*\,\d*\,\d*\,\d*$)|(^\d*\,\d*\,\d*\,\d*\.$)|(^\d*\,\d*\,\d*\,\d*\.\d$)|(^\d*\,\d*\,\d*\,\d*\.\d\d$)/;
	//check for numeric characters
	if (obj.value != "")
	{
		if (!objRegExp.test(obj.value))
		{
			funBuildAlterMsg("Please Enter Numeric value", "OK")
            toggleVis('saveSearchDialog');
			obj.value = "";
			obj.focus();
		}
	}
}

function funNumeric(obj)
{
   var objRegExp = /^\d*$/;
    if(obj.value != "")
    {
        if(!objRegExp.test(obj.value))
        {
           funBuildAlterMsg("Please Enter Numeric value", "OK")
            toggleVis('saveSearchDialog');
            obj.value = "";
            obj.focus();
        }

    }
}

function funSetMatchTypes()
{
    if(window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnThemesMatchType").value == "Any")
    {
        window.document.getElementById("rdoThemesAny").checked = true;
    }
    else
    {
        window.document.getElementById("rdoThemesAll").checked = true;
    }
    
    if(window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnGenreMatchType").value == "Any")
    {
        window.document.getElementById("rdoGenreAny").checked = true;
    }
    else
    {
        window.document.getElementById("rdoGenreAll").checked = true;
    }
    
}

function funSetCheckedYN(strValue,obj)
{
    window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_"+obj).value = strValue;
}


//Description		: Adding/Removing all the CategoryIds to Hiddenvariable.
function funCheckAllFilOptionsForAdvSrch(chkAllObj,chkObj)
{
  var obj=new Array();
  var objAll;
  var intCount;
  var vardate = new Date();
  var hdnCategories = window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchProductCat");  
  hdnCategories.value = "";
  var objAll = window.document.getElementById(chkObj);
  obj=window.document.getElementsByName(chkAllObj);         
  if(objAll.checked)
  {     
  }
  else
  {
    for (intCount=0;intCount<obj.length;intCount++)
    {  
        if(!obj[intCount].disabled)
        {		   
	        var strValue = obj[intCount].value;
	        hdnCategories.value += strValue + ",";		    
        }
    }
  }
}

function funGetSelectedCategoryDetailsForAdvSrch(strValue)
{
    var vardate = new Date();
    var hdnCategories = window.document.getElementById("ctl00_ctl00_BodyContent_SubBodyContent_hdnAdvSrchProductCat");

    if(hdnCategories.value.indexOf(strValue) > -1)
    {
        hdnCategories.value = hdnCategories.value.replace(strValue+",","");
    }
    else
    {
        hdnCategories.value += strValue + ",";
    }
}

