﻿function ValidateCategory(hdnCategoryID,txtCategoryID,strDuplicateCategoryMes,strConCategoryMes)
{ 
    var strCategory = document.getElementById(txtCategoryID).value;
    var arrayOfCategories = document.getElementById(hdnCategoryID).value.split("#");   
    var isDuplicateCategory = false;
    
    if (Page_ClientValidate() == false )
        return false;        
    if (CheckValidFolderName(strCategory)==false)
    {    
        ShowMessage(strConCategoryMes);
        return false;
    }
    if (arrayOfCategories && arrayOfCategories.length > 0)
    {
        for(i=0;i<arrayOfCategories.length;i++)
		{
				if(strCategory==arrayOfCategories[i])
				{
					isDuplicateCategory  = true;
					break;
				}
	    }
    } 
    if (isDuplicateCategory == true)
    {
        ShowMessage(strDuplicateCategoryMes);
        return false;
    }    
    
}

function setWindowProperties()
{
    winTop=0; winLeft=0;
	winWidth = screen.availWidth - 50;	
	winHeight = screen.availHeight - 50;
	winLeft=Math.floor((Math.abs(screen.availWidth-winWidth))/2);
	winTop=  Math.floor((Math.abs(screen.availHeight-winHeight))/2); 
	strWin = "'top= "+ winTop +"px,left=" + winLeft + "px,width=" + winWidth + "px,height= "+winHeight+"px,toolbar=nomenubar=no,location=no,directories=no,status=no,resizable=yes,scrollbars=yes,scrollbar=yes'";   	
	return strWin; 		
}

function OpeneMagazineWindow(strURL)
{      
    openWindow(strURL, "JetLiteBrochure",setWindowProperties());
    return false;
}

function displayWindow(parentPath,ddlArchivesID,htmlFileName)
{
    if (ddlArchivesID)       
    {
        var ddlArchives = document.getElementById(ddlArchivesID);
        var archivePath = ddlArchives.value;
        var fullPath = parentPath + "/" + archivePath + "/" + htmlFileName;
        return OpeneMagazineWindow(fullPath);         
    }
    else 
    {
        return false;
    }
}
function CheckForDuplicateTitles(strErrorMsg)
{
	var duplicateTitle = false;
	var Title="";
	for(i=0;i<document.forms[0].elements.length;i++)
	{
		elm = document.forms[0].elements[i];
		//alert(elm.type);
		if(document.forms[0].elements[i].type=="text")
		{	
			if(elm.value!="")
			{
				Title = elm.value;
				for(j=i+1;j<document.forms[0].elements.length;j++)
				{
					//var nextTextBox = document.forms[0].elements[j];
					if(document.forms[0].elements[j].type=="text")
					{
						//alert(document.forms[0].elements[j].value);
						var t = document.forms[0].elements[j].value;
						if(t==Title)
						{
							duplicateTitle = true;
							break;
						}
					}
				}
			}
		}
	}//for
	if(duplicateTitle)
	{		
	    ShowMessage(strErrorMsg);
		return false;
	}
}//function

function setAction(hdnId)
{
    hdnTxt = document.getElementById(hdnId);
    if (hdnTxt)
    hdnTxt.value = "true";   
}

function onSaveChange(hdnId,strDuplicateTitleMsg)
{
    if (Page_ClientValidate() == false )
        return false;   
    setAction(hdnId);
    if (strDuplicateTitleMsg)
       return CheckForDuplicateTitles(strDuplicateTitleMsg);     
}

function onDelete(hdnId,deleteMsg,confirmMsg)
{
    setAction(hdnId); 
    if (deleteMsg)	    
        return DeleteSubmit(deleteMsg,confirmMsg); 	        	   
}




function DuplicateArchive(hdnArchiveID,strNewArchiveName,strDuplicateArchiveMes)
{     
    var arrayOfArchives = document.getElementById(hdnArchiveID).value.split("#");   
    var isDuplicate = false;        
    
    if (arrayOfArchives && arrayOfArchives.length > 0)
    {
        for(i=0;i<arrayOfArchives.length;i++)
		{
				if(strNewArchiveName==arrayOfArchives[i])
				{
					isDuplicate  = true;
					break;
				}
	    }
    } 
    if (isDuplicate == true)
    {
        ShowMessage(strDuplicateArchiveMes);
        return false;
    }        
}

function ValidateArchive(fromYearID,fromMonthID,toYearID,toMonthID,strInvalidPeriod,hdnArchiveID,strDuplicateArchiveMsg) 
{    
 var ddlFromYear = document.getElementById(fromYearID);
 var ddlFromMonth = document.getElementById(fromMonthID);
 var ddlToYear = document.getElementById(toYearID);
 var ddlToMonth = document.getElementById(toMonthID); 
 var strNewArchive;
    if (ddlToMonth.value!="00")
    {  
        var fromPeriod = ddlFromYear.value + ddlFromMonth.value;
        var toPeriod = ddlToYear.value + ddlToMonth.value;  
            
        strNewArchive = ddlFromMonth.options[ddlFromMonth.selectedIndex].text + " " + ddlFromYear.value;         
                   
        if (fromPeriod != toPeriod)
        {           
            strNewArchive = strNewArchive + " - " +  ddlToMonth.options[ddlToMonth.selectedIndex].text + " " + ddlToYear.value;         
        }
        if (parseInt(toPeriod) < parseInt(fromPeriod))   
        {
            ShowMessage(strInvalidPeriod);
            return false;
        }       
    }
    else 
    {
        strNewArchive = ddlFromMonth.options[ddlFromMonth.selectedIndex].text + " " + ddlFromYear.value;
    }
    return DuplicateArchive(hdnArchiveID,strNewArchive,strDuplicateArchiveMsg);    
 } 
 
 