function createOption(val,label){
	var opt = document.createElement('option');
    opt.value = val;
    opt.appendChild(document.createTextNode(label));
    return opt;
}

var lan = (getCookie('language') == 'Fr')?2:1;

function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
}

/**
 * Building list of products for product search
 */
function buildProducts()
{
	$('#productsList>select').empty().append(createOption('',_getText('select-product')));
	$('#productSeriesList>select').empty().append(createOption('',_getText('select-series')));
    $.ajax(
            {   url:'/ajax/search/get-product-categories-list',
                data:{},
                type:'GET',
                dataType:'json',
                success : function(data) {
                    $('#productSeriesList>select').empty().append(createOption('',_getText('select-series')));
                    for(x in data) {
                        $('#productSeriesList>select').append(createOption(data[x]['id'],data[x]['Name']));
                    };
                    $('#productsList>select').attr('disabled','disabled');
                },
                error : function(xhr) {
                	ErrorMessage(xhr);
                }
            });
    $('#productSeriesList>select').bind('change', function() {
    	if($(this).val()=="") return;
    	$.ajax(
                {   url:'/ajax/search/get-product-list',
                    data:{category:$(this).val()},
                    type:'GET',
                    dataType:'json',
                    success : function(data) {
                        $('#productsList>select').empty().append(createOption('',_getText('select-product')));
                        for(x in data) {
                            $('#productsList>select').append(createOption(data[x]['id'],data[x]['Name']));
                        };
                        $('#productsList>select').removeAttr('disabled');
                    },
                    error : function(xhr) {
                    	ErrorMessage(xhr);
                    }
                });
    	});
}

/**
 * When a product selection happens in the product search form...
 */
function productSelection()
{
    $('#productsList>select').bind('change', function() {
    	var productID = $(this).val();
        if(productID!=""){
	    	if(typeof(myUpdater)!="undefined") {
	            if (lan == 2)
	            {
		    		if(!override && !confirm("Êtes-vous sure de vouloir terminer le processus?")) return;
		            else delete myUpdater;
	            }	            	
	            else
	            {
		    		if(!override && !confirm("Are you sure you want to exit the updating process?")) return;
		            else delete myUpdater;
	            };
	            override = true;
	        };

        
        	detail = new productDetail(productID);
        	detail.getProductDetail();
        }
    });
}

/**
 * Building list of install guides
 */
function buildInstallGuides()
{
	$('#selectGuide>select').empty().append(createOption('',_getText('select-guide')));
	$('#guideSeries > select').empty().append(createOption('',_getText('select-series')));
    $.ajax(
            {   url:'/ajax/search/get-product-categories-list',
                data:{},
                type:'GET',
                dataType:'json',
                success : function(data) {
                    $('#guideSeries>select').empty().append(createOption('',_getText('select-series')));
                    for(x in data) {
                        $('#guideSeries>select').append(createOption(data[x]['id'],data[x]['Name']));
                    };
                    $('#selectGuide>select').attr('disabled','disabled');
                },
                error : function(xhr) {
                	ErrorMessage(xhr);
                }
            });
    $('#guideSeries>select').bind('change', function() {
    	if($(this).val()=="") return;
    	$.ajax(
                {   url:'/ajax/search/get-install-guides-list/category/'+$(this).val(),
                    data:{},
                    type:'GET',
                    dataType:'json',
                    success : function(data) {
                        $('#selectGuide>select').empty().append(createOption('',_getText('select-guide')));
                        for(x in data) {
                            $('#selectGuide>select').append(createOption(data[x]['id'],data[x]['Name']));
                        };
                        $('#selectGuide>select').removeAttr('disabled');
                    },
                    error : function(xhr) {
                    	ErrorMessage(xhr);
                    }
                });
    	});
}
/**
 * When a guide selection happens in the install guide search form...
 */
function guideSelection(path)
{
    $('#selectGuide>select').bind('change', function() {
    	if($(this).val()!=""){
    		location.href=path+$(this).val();
    	}
    });
}

// Handling Callback request button
$(document).ready(function() {
	$('#callback_request').live('click', function() {
		var form = '<table><tr><td colspan="4" align="right"><span id="close_box">Close</span></td></tr>';
		form += '<tr><td colspan="4" align="left">'+((lan == 2)?'S\'il vous plait, entrez votre numéro de téléphone':'Please, enter your phone number')+'</td></tr>';		
		form += '<tr><td><input type="text" maxlength="3" value="000" id="number1" class="phone_number"></td>';
		form += '<td><input type="text" maxlength="3" value="000" id="number2" class="phone_number"></td>';
		form += '<td><input type="text" maxlength="4" value="0000" id="number3" class="phone_number"></td>';
		form += '<td><span id="get_callback_now"><img src="/idatalink/images/support/helpdesk/buttons/get_callback'+((lan == '2')?'.fr':'')+'.gif"></span></td></tr></table>';
		whiteBox({curtain:true, escapable:true, offset: {x:-80, y:-40}, content:form, 'class':'whiteBox'});
	});
	
	$('#get_callback_now').live('click', function() {
		var number = $('#number1').val()+$('#number2').val()+$('#number3').val();
		if(isNaN(number) || number==null || number=="" || number == "0000000000"){
			if (lan == 2) alert("S'il vous plat, entrez le correct numéro");
			else alert("Please enter correct phone number");
		} 
		else {
			location.href="http://cp.fonality.com/call.cgi?server_id=2639&callerid=5148072888&calleridname=Support&number="+number+"&username=livecall&password=AdsLiveCall&submenu=Support&success_redir=http://www.idatalink.com/helpdesk&failed_redir=http://www.idatalink.com/contact/";
		}		
	});
	
	$('#close_box').live('click', function() {
		WhiteBox.flush()
	});
	
	 $('.phone_number').live('click', function() {
		$(this).val(''); 
	 });
	 
	 $('form.frmFindManual').live('submit', function() {
		 var guideCode = $(this).find('input[name=id]').val();	 
		 if (guideCode != parseInt(guideCode)) return false;
			 
		 return true;
	 });
});
