var last_dividing_pointer_X=0;
var protocol_allow=1;
var httpport_allow=1;
var timeout_allow=1;

function IsInteger(interger) {
	var IsInteger = true;
	if (parseInt(interger) == interger) {
		IsInteger = true;
	}
	else {
		IsInteger = false;
	}
	return IsInteger;
}

function allow_upload_config(){
	if(protocol_allow==1&&httpport_allow==1&&timeout_allow==1){
		$("#factory_setting").attr("disabled",false);
	}else{
		$("#factory_setting").attr("disabled",true);
	}
}

function get_factory() {
    $.ajax({
        type: "GET",
        url: "http://" + xc_gethost() + "/get_factory",
        dataType: "xml",
        cache: false,
        error: function () {
        },
        success: function (data) {
            $('#factory_vo').val($(data).find("factory").find("hdmi_vo").text());
            $('#factory_shownosig').val($(data).find("factory").find("shownosingle").text());
            $('#factory_showip').val($(data).find("factory").find("showip").text());
			$('#factory_clearwnd').val($(data).find("factory").find("clearwnd").text());
			$('#factory_showswitcher').val($(data).find("factory").find("showswitcher").text());
			$('#factory_webauth').val($(data).find("factory").find("needauth").text());
			$('#factory_protocol').val($(data).find("factory").find("protocol").text());
			$('#factory_timeout').val($(data).find("factory").find("losttime").text());
			$('#factory_httpport').val($(data).find("factory").find("http_port").text());
            
            $("#main_content").removeAttr("class");
        }
    });
}

function factory_setting() {
    var ptmp = "";
    var factory_vo = $('#factory_vo').val();
    ptmp += "hdmi_vo=" + factory_vo + "&";
    
    var factory_shownosig = $('#factory_shownosig').val();
    ptmp += "shownosingle=" + factory_shownosig + "&";

    var factory_showip = $('#factory_showip').val();
    ptmp += "showip=" + factory_showip + "&";

    var factory_clearwnd = $('#factory_clearwnd').val();
    ptmp += "clearwnd=" + factory_clearwnd + "&";

    var factory_showswitcher = $('#factory_showswitcher').val();
    ptmp += "showswitcher=" + factory_showswitcher + "&";

    var factory_webauth = $('#factory_webauth').val();
    ptmp += "needauth=" + factory_webauth + "&";

    var factory_protocol = $('#factory_protocol').val();
    ptmp += "protocol=" + factory_protocol + "&";

	var factory_timeout = $("#factory_timeout").val();
	ptmp += "losttime=" + factory_timeout + "&";

	var factory_httpport = $("#factory_httpport").val();
	ptmp += "http_port=" + factory_httpport + "&";

	$.ajax({
		type: "GET",
		url: "http://" + xc_gethost() + "/set_factory" + "?" + ptmp,
		cache: false,
		success: function (data) {
			var ret = $(data).find("return").text();
			if ("ok" == ret) {
				alert($.i18n.prop('index_setting_success'));
			}
			else {
				alert($.i18n.prop('index_setting_failed'));
			}
		},
		error: function (){
			alert($.i18n.prop('index_setting_failed'));
		}
	});
}

function check_factory_protocol(str)
{
	var regular = /^([a-zA-Z])+$/;
	return regular.test(str);
}
function check_factory_timeout(str)
{
	var IntStr = IsInteger(str);
	var iStr = parseInt(str);

	if(iStr < 5 || iStr > 60 || IntStr == false) {
		return false;
	}
	return true;
}
function check_factory_httpport(str)
{
	var IntStr = IsInteger(str);
	var iStr = parseInt(str);

	if(iStr < 1 || iStr > 65535 || IntStr == false) {
		return false;
	}
	return true;
}

function init_language() {
    jQuery.i18n.properties({
        name: 'common',
        path: '../lang/',
        mode: 'map',
        language: window.parent.get_language_setup(),
        callback: function () {
            $('#factory_vo_txt').html($.i18n.prop('factory_vo_txt'));
            $('#factory_shownosig_txt').html($.i18n.prop('factory_shownosig_txt'));
            $('#factory_showip_txt').html($.i18n.prop('factory_showip_txt'));
            $('#factory_clearwnd_txt').html($.i18n.prop('factory_clearwnd_txt'));
            $('#factory_showswitcher_txt').html($.i18n.prop('factory_showswitcher_txt'));
			$('#factory_webauth_txt').html($.i18n.prop('factory_webauth_txt'));
			$('#factory_protocol_txt').html($.i18n.prop('factory_protocol_txt'));
			$('#factory_timeout_txt').html($.i18n.prop('factory_timeout_txt'));
            $('#factory_httpport_txt').html($.i18n.prop('factory_httpport_txt'));
            $('#factory_shownosig_disable').html($.i18n.prop('index_common_disable'));
			$('#factory_shownosig_enable').html($.i18n.prop('index_common_enable'));
			$('#factory_showip_disable').html($.i18n.prop('index_common_disable'));
			$('#factory_showip_enable').html($.i18n.prop('index_common_enable'));
			$('#factory_clearwnd_disable').html($.i18n.prop('index_common_disable'));
			$('#factory_clearwnd_enable').html($.i18n.prop('index_common_enable'));
			$('#factory_showswitcher_disable').html($.i18n.prop('index_common_disable'));
			$('#factory_showswitcher_enable').html($.i18n.prop('index_common_enable'));
			$('#factory_showip_disable').html($.i18n.prop('index_common_disable'));
			$('#factory_showip_enable').html($.i18n.prop('index_common_enable'));
			$('#factory_webauth_disable').html($.i18n.prop('index_common_disable'));
			$('#factory_webauth_enable').html($.i18n.prop('index_common_enable'));
            
            $('#factory_setting').val($.i18n.prop('index_setting'));
            $('#header_text').html($.i18n.prop('index_factory'));
        }
    });
}

function setup_pointer_position(element,offset){
	if(offset>0&&offset<241){
		element.find(".rule_pointer").attr("style","margin-left:"+offset+"px;");
		var value=parseInt(offset/240*100);
		element.next(".number").html(value).attr("value",value);
	}
}

function init_dividing_rule(){
	$(".dividing_rule").mousedown(function(e1){
		setup_pointer_position($(this),e1.pageX-$(this).offset().left-5);
		$(this).mousemove(function(e2){
			setup_pointer_position($(this),e2.pageX-$(this).offset().left-5);
		});
	}).mouseup(function(){
		$(this).unbind("mousemove");
	}).mouseleave(function(){
		$(this).unbind("mousemove");
	});
}

$(function () {
	init_language();
    get_factory();
    init_dividing_rule();

	$("#factory_protocol").focusout(function(){
    	var value=$(this).val();
    	if(value==""){
    		$("#protocol_hint_image").removeClass("display_off");
    		$("#protocol_hint_text").removeClass("display_off").html($.i18n.prop('index_common_hint_empty_text'));
    		protocol_allow=0;
    	}else if(!check_factory_protocol(value)){
    		$("#protocol_hint_image").removeClass("display_off");
    		$("#protocol_hint_text").removeClass("display_off").html($.i18n.prop('index_common_hint_format_text'));
    		protocol_allow=0;
    	}else{
    		$("#protocol_hint_image").addClass("display_off");
    		$("#protocol_hint_text").addClass("display_off").html("");
    		protocol_allow=1;
    	}
    	allow_upload_config();
    });

	$("#factory_timeout").focusout(function(){
    	var value=$(this).val();
    	if(value==""){
    		$("#timeout_hint_image").removeClass("display_off");
    		$("#timeout_hint_text").removeClass("display_off").html($.i18n.prop('index_common_hint_empty_text'));
    		timeout_allow=0;
    	}else if(!check_factory_timeout(value)){
    		$("#timeout_hint_image").removeClass("display_off");
    		$("#timeout_hint_text").removeClass("display_off").html($.i18n.prop('index_common_hint_format_text'));
    		timeout_allow=0;
    	}else{
    		$("#timeout_hint_image").addClass("display_off");
    		$("#timeout_hint_text").addClass("display_off").html("");
    		timeout_allow=1;
    	}
    	allow_upload_config();
    });

	$("#factory_httpport").focusout(function(){
    	var value=$(this).val();
    	if(value==""){
    		$("#httpport_hint_image").removeClass("display_off");
    		$("#httpport_hint_text").removeClass("display_off").html($.i18n.prop('index_common_hint_empty_text'));
    		httpport_allow=0;
    	}else if(!check_factory_httpport(value)){
    		$("#httpport_hint_image").removeClass("display_off");
    		$("#httpport_hint_text").removeClass("display_off").html($.i18n.prop('index_common_hint_format_text'));
    		httpport_allow=0;
    	}else{
    		$("#httpport_hint_image").addClass("display_off");
    		$("#httpport_hint_text").addClass("display_off").html("");
    		httpport_allow=1;
    	}
    	allow_upload_config();
    });
});
