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

/**
 * Building list of products for product search
 */
function buildProducts()
{
	$('#productsList>select').empty().append(createOption('','Select Product'));
	$('#productSeriesList>select').empty().append(createOption('','Select Series'));
    $.ajax(
            {   url:'/ajax/search/get-product-categories-list/lan/En',
                data:{},
                type:'GET',
                dataType:'json',
                success : function(data) {
                    $('#productSeriesList>select').empty().append(createOption('','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/lan/En',
                    data:{category:$(this).val()},
                    type:'GET',
                    dataType:'json',
                    success : function(data) {
                        $('#productsList>select').empty().append(createOption('','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(!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('','Select Guide'));
	$('#guideSeries > select').empty().append(createOption('','Select Series'));
    $.ajax(
            {   url:'/ajax/search/get-product-categories-list/lan/En',
                data:{},
                type:'GET',
                dataType:'json',
                success : function(data) {
                    $('#guideSeries>select').empty().append(createOption('','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/lan/En/category/'+$(this).val(),
                    data:{},
                    type:'GET',
                    dataType:'json',
                    success : function(data) {
                        $('#selectGuide>select').empty().append(createOption('','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">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.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"){
			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(''); 
	 }); 	 
});