﻿var caturl="";
var producturl="";
var prd_attach_url="";
var productList;
var attachmentList;
var productDetails;
var archiveFilter="";
var currentLevel=0;
var currentName="";
var prdNavbarHtml="";
var cat_entry="";
var cat_extend="";
var ddlName;
var sysLang = 'tc';
var i = 1;

var current_lang = "";
var current_cate = "";
var current_type = "";
var current_page = 1;
var new_search = false;

function alpha_35()
{
}

function doFilter(){
	category = '';
	if (frmFilter.frmCategory02.style.display=='none'){
		category = frmFilter.frmCategory01.value;
	} else if(frmFilter.frmCategory03.style.display=='none'){
		if(frmFilter.frmCategory02.value == ''){
			category = frmFilter.frmCategory01.value;
		}else {
			category = frmFilter.frmCategory02.value;
		}
	} else if(frmFilter.frmCategory04.style.display=='none'){
		if(frmFilter.frmCategory03.value == ''){
			category = frmFilter.frmCategory02.value;
		}else {
			category = frmFilter.frmCategory03.value;
		}
	} else if(frmFilter.frmCategory04.value == ''){
		category = frmFilter.frmCategory03.value;
	}else {
		category = frmFilter.frmCategory04.value;
	}
	frmFilter.catCode.value = category;
	//alert(frmFilter.frmKeyword.value + "  " + frmFilter.frmCategory.value)
	doPost();
}

function createDropDownlist(parentCode, combo, Lang, param){

    //combo.options.length = 1;
    removeOption(combo);
    
    ddlName = combo;
    sysLang = Lang;
	if (parentCode != null){
        caturl="/services/category.html?catid="+parentCode+"&lang="+Lang;
//        producturl="/services/productlist_bycode.html?catcode="+parentCode+"&lang="+"tc";
//        prd_attach_url="/services/product_attachment.html?cat="+parentCode+"&type=catalogue";
        cat_entry=parentCode;
//            alert("test1");
        new Ajax.Request( caturl, {
		    onSuccess:updateSelection,
		    onFailure:function(transpot){
		    },
		    method: 'get'
	    });
//            alert("test1");
	    
	} else{
		combo.style.display='none';
//		id.style.display='none';
	}
        setDisplay(param);
}



function updateSelection(transport,json)
{
try{
        productList=('/*-secure-\n'+transport.responseText+'\n*/').evalJSON();
        if(productList.length !=0)
        {
            ddlName.disabled=false;               

            updateProductSelection();           
        }
        else
        {
            ddlName.disabled=true;
        }
        
        
//        //writeDebug('finish evaluate!');
        
        
    }catch(e){
       //writeDebug(e);
       return;
    }
    //writeDebug($('prd_filter').innerHTML);
}

function updateProductSelection()
{
    var prdfilterHtml="";
    productList.each(
        function(productlist)
        {
            //ddlName.options[ddlName.options.length] =  new Option(productlist.name, productlist.id);
            //ddlName.options[ddlName.options.length - 1].title = productlist.code;
            
            ddlName.options[ddlName.options.length] =  new Option(converCode(productlist.name), productlist.code);
            //ddlName.options[ddlName.options.length - 1].title = productlist.id;
            $(ddlName.options[ddlName.options.length - 1]).writeAttribute("prd",productlist.id);
            
            //writeDebug(productlist.name);
        }
    )    
}

function setDisplay(param){

    if (param == '1'){
//        document.frmFilter.frmCategory01.style.display='inline';       
       
//      document.getElementsById('title01').style='';
	    document.frmFilter.frmCategory02.disabled=true;
	    document.frmFilter.frmCategory03.disabled=true;
	    document.frmFilter.frmCategory04.disabled=true;
//		    
//      document.title02.style.display='none';
//		document.title03.style.display='none';
//		document.title04.style.display='none';
	    //document.frmFilter.frmCategory02.options.length = 1;
	    //document.frmFilter.frmCategory03.options.length = 1;
	    //document.frmFilter.frmCategory04.options.length = 1;
	    removeOption('frmCategory02');
	    removeOption('frmCategory03');
	    removeOption('frmCategory04');
	}
	if (param == '2'){        
//        document.frmFilter.frmCategory02.style.display='inline';
        	
//      document.title02.style.display='inline';
		document.frmFilter.frmCategory03.disabled=true;
		document.frmFilter.frmCategory04.disabled=true;
        //document.frmFilter.frmCategory03.options.length = 1;
		//document.frmFilter.frmCategory04.options.length = 1;
		removeOption('frmCategory03');
		removeOption('frmCategory04');

//		document.title03.style.display='none';
//		document.title04.style.display='none';	
	}
	if (param == '3'){        
//        document.frmFilter.frmCategory03.style.display='inline';
        
//      document.title03.style.display='inline';
		document.frmFilter.frmCategory04.disabled=true;
        //document.frmFilter.frmCategory04.options.length = 1;
        removeOption('frmCategory04');
//		document.title04.style.display='none';
	}
	if (param == '4'){
//        document.frmFilter.frmCategory04.style.display='inline';        
	}		
}

function doSend(lang, typeObj){
        var type = typeObj.value;
      var catCode = '';
      /*
      if (document.frmFilter.frmCategory04.options.length == 1 || document.frmFilter.frmCategory04.selectedIndex == 0){
        if (document.frmFilter.frmCategory03.options.length == 1 || document.frmFilter.frmCategory03.selectedIndex == 0){
            if (document.frmFilter.frmCategory02.options.length == 1 || document.frmFilter.frmCategory02.selectedIndex == 0){
                if (document.frmFilter.frmCategory01.options.length == 1 || document.frmFilter.frmCategory01.selectedIndex == 0){
                    if (document.frmFilter.cat1.options.length == 1 || document.frmFilter.cat1.selectedIndex == 0){
                        catCode = '-1';
                    }
                    else{
                        catCode = document.frmFilter.cat1.options[document.frmFilter.cat1.selectedIndex].title;
                    }
                } else{                    
                    catCode = document.frmFilter.frmCategory01.options[document.frmFilter.frmCategory01.selectedIndex].title;
                }
            } else{
                catCode = document.frmFilter.frmCategory02.options[document.frmFilter.frmCategory02.selectedIndex].title;
            }
        } else{
              catCode = document.frmFilter.frmCategory03.options[document.frmFilter.frmCategory03.selectedIndex].title;
        }
      } else{
            catCode = document.frmFilter.frmCategory04.options[document.frmFilter.frmCategory04.selectedIndex].title;
      }
      */
      
      if (document.frmFilter.frmCategory04.options.length == 1 || document.frmFilter.frmCategory04.selectedIndex == 0){
        if (document.frmFilter.frmCategory03.options.length == 1 || document.frmFilter.frmCategory03.selectedIndex == 0){
            if (document.frmFilter.frmCategory02.options.length == 1 || document.frmFilter.frmCategory02.selectedIndex == 0){
                if (document.frmFilter.frmCategory01.options.length == 1 || document.frmFilter.frmCategory01.selectedIndex == 0){
                    if (document.frmFilter.cat1.options.length == 1 || document.frmFilter.cat1.selectedIndex == 0){
                        catCode = '-1';
                    }
                    else{
                        catCode = document.frmFilter.cat1.value;
                    }
                } else{                    
                    catCode = document.frmFilter.frmCategory01.value;
                }
            } else{
                catCode = document.frmFilter.frmCategory02.value;
            }
        } else{
              catCode = document.frmFilter.frmCategory03.value;
        }
      } else{
            catCode = document.frmFilter.frmCategory04.value;
      }
      
      new_search = true;
      update_product_list(lang,catCode, type);       
}

function update_product_list(lang,catcode, type, page){
    current_lang = lang;
    current_cate = catcode;
    current_type = type;
    if (page == null) {
        current_page = 1;
    } else {
        current_page = parseInt(page,10);
    } 


    producturl="/services/productlistSupport_bycode.html?catcode="+catcode+"&lang="+lang;
//    prd_attach_url="/services/product_attachment.html?cat="+catcode+"&type=Catalogue";
    prd_attach_url="/services/product_attachment_download.html?cat="+catcode+"&type=" + type + "&lang="+lang;
    //writeDebug(catid+","+cat+","+catcode);
    //alert(catid+","+cat+","+catcode);
    //alert(catcode.indexOf(cat));
//    cat_entry=catcode;
//    
//    var cat_image=catcode.substring(4,catcode.length);
//    
//    var dot_pos=cat_image.indexOf('.');
//    if (dot_pos>0)
//    {
//        cat_image=cat_image.substring(0,dot_pos);
//    }
    $('prd_list').hide();
    $('prd_details').hide();
     
     prdNavbarHtml="";

     //writeDebug('NAV BAR:'+$('prd_navbar').innerHTML);
     //writeDebug("Filter URL:"+url);
     //alert(url);
    //$('section_img').src=sectionImage;
//	new Ajax.Request( url, {
//		onSuccess:updateSelection,
//		onFailure:function(transpot){
//		},
//		method: 'get'
//	});
	//writeDebug("Product URL:"+producturl);
//	    new Ajax.Request( prd_attach_url, {
//		    onSuccess:function(transpot){
//		    },
//		    onFailure:function(transpot){
//		    },
//		    method: 'get'
//	    });	

/*
new Ajax.Request( producturl, {
		onSuccess:displaySelection,
		onFailure:function(transpot){
		},
		method: 'get'
	});
*/

new Ajax.Request( prd_attach_url, {
		onSuccess:displaySelection,
		onFailure:function(transpot){
		},
		method: 'get'
	});

}

function displaySelection(transport,json)
{
try{

        //productList=('/*-secure-\n'+transport.responseText+'\n*/').evalJSON();
/*
        //writeDebug('product list display!');
        //writeDebug('Product attachment URL:'+prd_attach_url);
        
	new Ajax.Request(prd_attach_url, {
		//onSuccess:extendSelection,
		onSuccess:function(transpot)
		{
		*/
        //attachmentList=('/*-secure-\n'+transpot.responseText+'\n*/').evalJSON();
        /*
        displayProductSelection(1);
        },
        onFailure:function(transpot){
		//writeDebug('fail');
		},
		method: 'get'
		});
*/

        attachmentList=('/*-secure-\n'+transport.responseText+'\n*/').evalJSON();
        
        if (!new_search) {
            displayProductSelection(current_page);
        } else {
            new_search = false;
            window.open("/alpha.attach.blank.htm?page_value="+current_page,"prd_iframe");
        }
        
    }catch(e){
       //writeDebug(e);
       return;
    }
}

function displayProductSelection(page)
{

    var prdListHtml="<div style=\"height:460px;\"><table border=0 cellpadding=0 cellspacing=1>";
    attachmentList.each(
        
        function(attachmentList)
        {
            if(i >= (page - 1)*10 +1 && i <= (page * 10))
            {
                //writeDebug(productlist.name);
//                productlist.attach="none";
                    
                    /*
                  if(attachmentList.label == "")
                    {             
                               
                        productList.each(
                            function(productlabel)
                            {        
                                //attachmentList.label = attachmentList.label + productlabel.id + "=" + attachmentList.prd_id + "<br/>";
                                //alert(productlabel.id + "=" + attachmentList.prd_id);
                                if (productlabel.id==attachmentList.prd_id)
                                {
                                    if(attachmentList.image == "")
                                            attachmentList.image = productlabel.image; 

                                    if(attachmentList.label == "")                                              
                                            attachmentList.label=productlabel.name;
                                }
                            }
                        );
                    }
                    */
                    
                    if(attachmentList.image == "")
                        attachmentList.image = attachmentList.prd_image; 

                    if(attachmentList.label == "")                                              
                        attachmentList.label=attachmentList.prd_name;
                        
                        
                    attachmentList.download_image = '/templates/alpha/' + info.lang + '/images/btn_down_catalogue_2.gif';
                    
                //writeDebug("Attachment: "+productlist.attach);
//                if (productlist.type==productlist.cat_name) productlist.cat_name="";
                if(i%5 == 1)
                    prdListHtml+="<tr valign='top' style='border:solid 1px #bfbfbf;'>";


                if (attachmentList.image !="")
                {               
                    prdListHtml+="<td width='160' style='border:solid 1px #bfbfbf;'>" + templates.AttachDownloadWithImage.evaluate(attachmentList) + "</td>";

                }
                else
                {
                    
                        attachmentList.image = "/files/images/no_photo_l.gif";
                        prdListHtml+="<td width='160' style='border:solid 1px #bfbfbf;'>" + templates.AttachDownloadWithImage.evaluate(attachmentList) + "</td>";
                    
                }

                if (i%5 == 0)
                    prdListHtml+="</tr>";

            }
                i++;
        }
    );

    /*Create page number row Start*/
    var totalPage = 0;
    if(attachmentList.length%10 != 0)
        totalPage = parseInt(attachmentList.length/10) + 1; 
    else
        totalPage = parseInt(attachmentList.length/10); 

    prdListHtml+='</tr></table></div>';
    if (totalPage > 1){
        prdListHtml+='<table border=0 cellpadding=2 cellspacing=2 width="100%"><tr>';
        //prdListHtml+='<td align="center">' + label["alpha.attach.page"] + ': <a href="javascript:alpha_35();" onclick="movePage(1)">&lt;</a>&nbsp;&nbsp;&nbsp;';
        prdListHtml+='<td align="center">' + label["alpha.attach.page"] + ': <a href="/alpha.attach.blank.htm?page_value=1&lang='+current_lang+'&catcode='+current_cate+'&type='+current_type+'">&lt;</a>&nbsp;&nbsp;&nbsp;';
        page = parseInt(page,10);
        if(totalPage > 1){
            for(var p = 1; p <= totalPage ; p++){
                if(p == page) {
                    //prdListHtml+='<b><a href="javascript:alpha_35();" onclick="movePage(' + p + ')">' + p + '</a></b>&nbsp;';
                    prdListHtml+='<b><a href="/alpha.attach.blank.htm?page_value='+p+'&lang='+current_lang+'&catcode='+current_cate+'&type='+current_type+'" >' + p + '</a></b>&nbsp;';
                } else if (p > page - 5 && p < page + 5) {
                    //prdListHtml+='<a href="javascript:alpha_35();" onclick="movePage(' + p + ')">' + p + '</a>&nbsp;';
                    prdListHtml+='<a href="/alpha.attach.blank.htm?page_value='+p+'&lang='+current_lang+'&catcode='+current_cate+'&type='+current_type+'" >' + p + '</a>&nbsp;';
                } else if (p > page - 6 && p < page) {
                    //prdListHtml+='<a href="javascript:alpha_35();" onclick="movePage(' + p + ')">...</a>&nbsp;';
                    prdListHtml+='<a href="/alpha.attach.blank.htm?page_value='+p+'&lang='+current_lang+'&catcode='+current_cate+'&type='+current_type+'" >...</a>&nbsp;';
                } else if (p < page + 6 && p > page) {
                    //prdListHtml+='<a href="javascript:alpha_35();" onclick="movePage(' + p + ')">...</a>&nbsp;';
                    prdListHtml+='<a href="/alpha.attach.blank.htm?page_value='+p+'&lang='+current_lang+'&catcode='+current_cate+'&type='+current_type+'">...</a>&nbsp;';
                }
        }
    }       
    //prdListHtml+='&nbsp;&nbsp;&nbsp;<a href="javascript:alpha_35();" onclick="movePage(' +  totalPage + ')">&gt;</a>';
    prdListHtml+='&nbsp;&nbsp;&nbsp;<a href="/alpha.attach.blank.htm?page_value='+totalPage+'&lang='+current_lang+'&catcode='+current_cate+'&type='+current_type+'">&gt;</a>';
    prdListHtml+='</td></tr></table>';
     /*Create page number row End*/
    }
    if(attachmentList.length == 0){
        prdListHtml=label["alpha.attach.download.result"] + label["alpha.attach.download.result.no"];
    } 
    //$('prd_list').update(prdListHtml);
    $('prd_details').hide();
    //$('prd_list').show();    
    $('prd_list').hide();
    
    $('prd_iframe').contentWindow.document.body.innerHTML = prdListHtml;
    resizeInfoFrame('prd_iframe');
    //$('prd_iframe').height = "500px";

    i=1;
}

function movePage(page){
    displayProductSelection(page);
}


function removeOption(input){
	var inputObj = $(input);
	
	for(var i=inputObj.options.length; i>1; i--){
		inputObj.options[i - 1] = null;
	}
}


function resizeInfoFrame(f) {
  //alert($(f).contentWindow.document.body.scrollHeight + "," + $(f).contentWindow.document.body.offsetHeight)
  /*
  if ($(f).contentWindow.document.body) {
    $(f).style.height = $(f).contentWindow.document.body.scrollHeight + 0 + "px";
  } else if ($(f).contentWindow.document.body.offsetHeight) {    
    $(f).style.height = $(f).contentWindow.document.body.offsetHeight + 0 + "px";
  }
  */
  var new_h = 0;
  if ($(f).contentWindow.document.body.scrollHeight >= $(f).contentWindow.document.body.offsetHeight) {
    new_h = $(f).contentWindow.document.body.scrollHeight;
  } else {
    new_h = $(f).contentWindow.document.body.offsetHeight;
  }    
  $(f).style.height = new_h + 0 + "px";
}
        
        
function converCode(str) {
    str= str.replace(/\&amp;/g,'&');
    str= str.replace(/\&lt;/g,'<');
    str= str.replace(/\&gt;/g,'>');
    return str;
}
