$(document).ready(function() {
    //$('.rounded').corners();
    resetform();
    $("#field_companies").bind("change", companies_change);
    $("#field_companies2").bind("change", companies_change2);
    $("#field_producttype").bind("change", prodtype_change);
    $("#field_producttype2").bind("change", prodtype_change2);
    $("#product").bind("change", product_change);
    $("#submit").bind("click", sendform);
    jQuery('#qty').keyup(function () {
        this.value = this.value.replace(/[^0-9]/g, '');
    });
	
});

function send_reseller_form()
{

	var userid  =	$('#uid').val();
	var year 	= 	$('#year').val();
	var month 	=	$('#month').val();
	var itemcode=	$('#itemcode').val();
	var itemname=	$('#itemname').val();
	var ostock	=	$('#ostock').val();
	var purchases=	$('#purchases').val();
	var sqty	=	$('#sqty').val();
	var sammount=	$('#sammount').val();
	var rqty	=	$('#rqty').val();
	var rammount=	$('#rammount').val();
	var oiqty	=	$('#oiqty').val();
	var country	=	$('#country').val();


	 $.ajax({
			type: "POST",
			url: global.base_url + "main/add_reseller_data",
			data: "userid=" + userid + "&year=" + year + "&month=" +  month + "&itemcode=" +  itemcode + "&itemname=" +  itemname + "&ostock=" +  ostock + "&purchases=" +  purchases + "&sqty=" +  sqty + "&sammount=" +  sammount + "&rqty=" +  rqty + "&rammount=" +  rammount + "&oiqty=" +  oiqty + "&country=" +  country ,
			processData: $('#i1').show(),
			success: function(msg) {
				$('#i3').hide();
				var mySplitResult = msg.split("|");
				if (mySplitResult[0] == "ok")
				{
					$('#table1').hide();
					$('#okk').show();
				}
				else
				{
					$('#i3').hide();
					alert('Somthing wrong, please try again letter ...');
				}
			}
		});
}


function ch_data()
{
    var storename = $('#sname').val();
    var email     = $('#email').val();

    if (storename.length < 2 )
    {
        alert('Store name must be more than 2 characters');
        return false;
    }

    if (email.length < 8 )
    {
        alert('Email must be more than 8 characters');
        return false;
    }
	
    var uid = $('#uid').val();

    $.ajax({
        type: "POST",
        url: global.base_url + "main/change_data",
        data: "d1=" + storename + '&d2=' + email + "&uid=" +  uid,
        processData: $('#i1').show(),
        success: function(msg) {
            $('#i3').hide();
            var mySplitResult = msg.split("|");
            if (mySplitResult[0] == "ok")
            {
                $('#table2').hide();
                $('#okk2').show();
            }
            else
            {
                $('#i3').hide();
                alert(mySplitResult[1]);
            }
        }
    });

}

function ch_passwd()
{
    var npass1 = $('#npasswd1').val();
    var npass2 = $('#npasswd2').val();

    npass1 = npass1.split(' ').join('');
    npass2 = npass2.split(' ').join('');

    if (npass1.length < 3 || npass2.length < 3)
    {
        alert('Password must be more than 3 characters');
        return false;
    }

    if (npass1.length != npass2.length )
    {
        alert('Passwords not equal');
        return false;
    }
    var uid = $('#uid').val();

    $.ajax({
        type: "POST",
        url: global.base_url + "main/change_passwd",
        data: "passwd1=" + npass1 + '&passwd2=' + npass2 + "&uid=" +  uid,
        processData: $('#i1').show(),
        success: function(msg) {
            $('#i3').hide();
            var mySplitResult = msg.split("|");
            if (mySplitResult[0] == "ok")
            {
                $('#table1').hide();
                $('#okk').show();
            }
            else
            {
                $('#i3').hide();
                alert('Somthing wrong, please try again letter ...');
            }
        }
    });

}


function resetform()
{
    $("form").each(function() {
        this.reset();
    });
    $("#field_producttype").html('');
    $("#field_producttype").html('<option value="0">Select</option>');
    $("#product").html('');
    $("#product").html('<option value="0">Select</option>');
    $("#discription").html('');
	$('#spob').hide();
}

function resetform2()
{
    $("form").each(function() {
        this.reset();
    });
    $("#field_producttype2").html('');
    $("#field_producttype2").html('<option value="0">Select</option>');
    $("#show_editproductf").hide('');
    $("#prodeditdiv").hide();
}

function resetproduct()
{
    $("#product").html('');
    $("#product").html('<option value="0">Prduct</option>');
    $("#discription").html('');
}

function resetproduct2()
{
    $("#product").html('');
    $("#product").html('<option value="0">Prduct</option>');
    $("#discription").html('');
    $("#show_editproductf").hide();
    $("#prodeditdiv").hide();

}

function companies_change()
{
    var c_id = $('#field_companies').val();
    if (c_id != 0)
    {
        fill_producttype(c_id);
        resetproduct();
		special_offer();
    }
    else
    {
        resetform();
    }
}

function special_offer()
{
            $("#spob").attr("href", "");
            var selectedvalue = $("#field_companies").val();
            var stext  = $("#field_companies option[value="+selectedvalue+"]").html()
            stext = stext.replace(" ","_");
            var newlink_path = global.base_url + 'so/' + stext + ".html";
            $("#spob").attr("href", newlink_path);
			$('#spob').show();
}

function companies_change2()
{
    var c_id = $('#field_companies2').val();
    if (c_id != 0)
    {
        fill_producttype2(c_id);
        resetproduct2();
        $('#show_addproductf').show();
        $('#prodadddiv').hide();
    }
    else
    {
        resetform2();
        $('#show_addproductf').hide();
        $('#prodadddiv').hide();
    }
}
function fill_producttype(c_id)
{
    $.ajax({
        type: "POST",
        url: global.base_url + "main/ret_all_companies",
        data: "company_id=" + c_id,
        processData: $('#i1').show(),
        success: function(msg) {
            $('#field_producttype').html(msg);
            $('#i1').hide();
        }
    });
}
function fill_producttype2(c_id)
{
    $.ajax({
        type: "POST",
        url: global.base_url + "main/ret_all_companies",
        data: "company_id=" + c_id,
        processData: $('#i1').show(),
        success: function(msg) {
            $('#field_producttype2').html(msg);
            $('#i1').hide();
        }
    });

}
function prodtype_change()
{
    var p_id = $('#field_producttype').val();
    if (p_id != 0)
    {
        fill_product(p_id);
    }
    else
    {
        resetproduct();
    }
}

function prodtype_change2()
{
    var p_id = $('#field_producttype2').val();
    if (p_id != 0)
    {
        fill_product(p_id);
        $('#show_editproductf').show();
        $('#show_addproductf').hide();
        $('#prodadddiv').hide();
        $('#prodeditdiv').hide();

    }
    else
    {
        resetproduct2();
        $('#show_editproductf').hide();
        $('#show_addproductf').show();
        $('#prodadddiv').hide();
        $('#prodeditdiv').hide();
    }
}

function prodtype_change3()
{
    var p_id = $('#field_producttype3').val();
    if (p_id != 0)
    {
        fill_product(p_id);
        $('#show_additem').show();
        $('#show_addproductf').hide();


    }
    else
    {
        resetproduct2();
        $('#show_additem').hide();
        $('#show_addproductf').show();
        $('#prodadddiv').hide();
        $('#prodeditdiv').hide();
    }
}

function fill_product(p_id)
{
    $.ajax({
        type: "POST",
        url: global.base_url + "main/ret_all_products",
        data: "productype_id=" + p_id,
        processData: $('#i2').show(),
        success: function(msg) {
            $('#product').html(msg);
            $('#i2').hide();
        }
    });
}
function product_change()
{
    var p_id = $('#product').val();
    if (p_id != 0)
    {
        fill_discription(p_id);
    }
    else
    {
//resetproduc_disc();
}
}

function searchitm()
{
	var s_string = $('#itmname').val();
	
    $.ajax({
        type: "POST",
        url: global.base_url + "main/ret_product_search2",
        data: "s_string=" + s_string,
        processData: $('#i3').show(),
        success: function(msg) {
			
            $('#discription').html(msg);   
			$('#i3').hide();			
        }
    });
}


function searchitm_det(id)
{
    $.ajax({
        type: "POST",
        url: global.base_url + "main/ret_product_search",
        data: "id=" + id,
        processData: $('#i3').show(),
        success: function(msg) {
			
            $('#discription').html(msg);   
			$('#i3').hide();			
        }
    });
}

function fill_discription(p_id)
{
    $.ajax({
        type: "POST",
        url: global.base_url + "main/ret_product_discription",
        data: "product_id=" + p_id,
        processData: $('#i3').show(),
        success: function(msg) {

            $('#discription').html(msg);   
			$('#i3').hide();			
        },
		error: function(msg)
        {
            alert(msg.responseText);
        }
    });
}

function sendform()
{
    var q = $('#qty').val();
    if (q == '' || parseInt(q) == 0)
    {
        alert('Please Select Product Qty');
    }
    else
    {
        var prod_id = $('#phid').val();
        if (prod_id == '' || parseInt(prod_id) == 0 || typeof(prod_id) == 'undefined')
        {
            alert('Please select Product');
        }
        else
        {
            process_form();
        }
    }
}
function process_form()
{
    var phid = $('#phid').val();
    var qty = $('#qty').val();
    $.ajax({
        type: "POST",
        url: global.base_url + "main/process_form",
        data: "phid=" + phid + '&qty=' + qty,
        processData: $('#i3').show(),
        success: function(msg) {
            $('#i3').hide();
            var mySplitResult = msg.split("|");
            if (mySplitResult[0] == "ok")
            {
                $('#reqform').hide();
                $('#status_field').show();
            }
            else
            {
                $('#i3').hide();
                alert('Somthing wrong, please try again letter ...');
            }
        },
        error: function(msg)
        {
            $('#i3').hide();
            alert(msg.responseText);
        }
    });
}

function deluser(id)
{
    var answer = confirm("Delete selected user ?")
    if (answer){

        $.ajax({
            type: "POST",
            url: global.base_url + "admin/deluser",
            data: "id=" + id,
            processData: $('#i3').show(),
            success: function(msg) {
                $('#i3').hide();
                var mySplitResult = msg.split("|");
                if (mySplitResult[0] == "ok")
                {
                    var objname = '#row' + id;
                    $(objname).hide();
                }
                else
                {
                    $('#i3').hide();
                    alert('Somthing wrong, please try again letter ...');
                }
            },
            error: function(msg)
            {
                $('#i3').hide();
                alert(msg.responseText);
            }
        });
    }
}

function edit_user()
{
    var uid 	= $('#uid').val();
    var fname 	= $('#fname').val();
    var uname 	= $('#uname').val();
    var passwd 	= $('#passwd').val();
    var email 	= $('#email').val();
    var comments 	= $('#comments').val();
    var isAdmin 	= $('#isAdmin').is(':checked');

    $.ajax({
        type: "POST",
        url: global.base_url + "admin/edit_user",
        data: "uid=" + uid + '&fname=' + fname + '&uname=' + uname + '&passwd=' + passwd + '&email=' + email + '&comments=' + comments + '&isAdmin=' + isAdmin,
        processData: $('#i3').show(),
        dataFilter: function(data) {
            if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') return JSON.parse(data);
            else
                return eval('(' + data + ')');
        },
        success: function(JSONtext) {
            $('#i3').hide();

            if (JSONtext.ok  == "1")
            {
                $('#editform').hide();
                $('#divok').show();
            }
            else
            {
                $('#i3').hide();
                alert(JSONtext.msg);
            }
        },
        error: function(JSONtext)
        {
            $('#i3').hide();
            alert(JSONtext.responseText);
        }
    });
}

function add_user()
{
    var fname 	= $('#fname').val();
    var uname 	= $('#uname').val();
    var passwd 	= $('#passwd').val();
    var email 	= $('#email').val();
    var comments 	= $('#comments').val();
    var isAdmin 	= $('#isAdmin').is(':checked');

    $.ajax({
        type: "POST",
        url: global.base_url + "admin/add_user",
        data: 'fname=' + fname + '&uname=' + uname + '&passwd=' + passwd + '&email=' + email + '&comments=' + comments + '&isAdmin=' + isAdmin,
        processData: $('#i3').show(),
        dataFilter: function(data) {
            if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') return JSON.parse(data);
            else
                return eval('(' + data + ')');
        },
        success: function(JSONtext) {
            $('#i3').hide();

            if (JSONtext.ok  == "1")
            {
                $('#editform').hide();
                $('#divok').show();
            }
            else
            {
                $('#i3').hide();
                alert(JSONtext.msg);
            }
        },
        error: function(JSONtext)
        {
            $('#i3').hide();
            alert(JSONtext.responseText);
        }
    });
}

function delcompany(id)
{
    var answer = confirm("Delete this company ? \n\n If yes, will be deleted all related items all products with description  !!! ")
    if (answer){

        $.ajax({
            type: "POST",
            url: global.base_url + "admin/delcompany",
            data: "id=" + id,
            processData: $('#i3').show(),
            success: function(msg) {
                $('#i3').hide();
                var mySplitResult = msg.split("|");
                if (mySplitResult[0] == "ok")
                {
                    var objname = '#row' + id;
                    $(objname).hide();
                }
                else
                {
                    $('#i3').hide();
                    alert('Somthing wrong, please try again letter ...');
                }
            },
            error: function(msg)
            {
                $('#i3').hide();
                alert(msg.responseText);
            }
        });
    }
}


function edit_comp()
{
    var cid 	= $('#cid').val();
    var cname 	= $('#cname').val();

    $.ajax({
        type: "POST",
        url: global.base_url + "admin/edit_company",
        data: "cid=" + cid + '&cname=' + cname,
        processData: $('#i3').show(),
        dataFilter: function(data) {
            if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') return JSON.parse(data);
            else
                return eval('(' + data + ')');
        },
        success: function(JSONtext) {
            $('#i3').hide();

            if (JSONtext.ok  == "1")
            {
                $('#editform').hide();
                $('#divok').show();
            }
            else
            {
                $('#i3').hide();
                alert(JSONtext.msg);
            }
        },
        error: function(JSONtext)
        {
            $('#i3').hide();
            alert(JSONtext.responseText);
        }
    });
}


function add_company()
{
    var cname 	= $('#cname').val();

    $.ajax({
        type: "POST",
        url: global.base_url + "admin/add_company",
        data: 'cname=' + cname,
        processData: $('#i3').show(),
        dataFilter: function(data) {
            if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') return JSON.parse(data);
            else
                return eval('(' + data + ')');
        },
        success: function(JSONtext) {
            $('#i3').hide();

            if (JSONtext.ok  == "1")
            {
                $('#editform').hide();
                $('#divok').show();
            }
            else
            {
                $('#i3').hide();
                alert(JSONtext.msg);
            }
        },
        error: function(JSONtext)
        {
            $('#i3').hide();
            alert(JSONtext.responseText);
        }
    });
}


function delete_prodf()
{
    $("#prodeditdiv").hide();
    var prod_id = $('#field_producttype2').val();

    var answer = confirm("Delete this product ? \n\n If yes, will be deleted all related products with description  !!! ")
    if (answer){

        $.ajax({
            type: "POST",
            url: global.base_url + "admin/delproduct",
            data: "id=" + prod_id,
            processData: $('#i3').show(),
            success: function(msg) {
                $('#i3').hide();
                var mySplitResult = msg.split("|");
                if (mySplitResult[0] == "ok")
                {
                    var objname = '#show_editproductf' ;
                    $(objname).hide();
                    $("#field_producttype2 option[value='"+mySplitResult[1]+"']").remove();
                    $("#field_producttype2").val("0");
                }
                else
                {
                    $('#i3').hide();
                    alert('Somthing wrong, please try again letter ...');
                }
            },
            error: function(msg)
            {
                $('#i3').hide();
                alert(msg.responseText);
            }
        });
    }

}

function edit_prodf()
{
    var prod_id = $('#field_producttype2').val();
    $('#prodeditdiv').show();
    var edtxt = $('#field_producttype2 :selected').text();
    $('#pfname').val(edtxt);
}

function editproductf()
{
    var prod_id = $('#field_producttype2').val();
    var newname = $('#pfname').val();

    $.ajax({
        type: "POST",
        url: global.base_url + "admin/edit_product",
        data: "id=" + prod_id + '&newname='+newname,
        processData: $('#i3').show(),
        dataFilter: function(data) {
            if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') return JSON.parse(data);
            else
                return eval('(' + data + ')');
        },
        success: function(JSONtext) {
            $('#i3').hide();

            if (JSONtext.ok  == "1")
            {
                $('#field_producttype2 :selected').text(JSONtext.nname);
                $('#prodeditdiv').hide();
            }
            else
            {
                $('#i3').hide();
                alert(JSONtext.msg);
            }
        },
        error: function(msg)
        {
            $('#i3').hide();
            alert(msg.responseText);
        }
    });
}


function show_prodf()
{
    $("#prodadddiv").show();
}
function addproductf() {

    var lob_id = $("#field_companies2").val();
    var name = $("#npfname").val()

    $.ajax({
        type: "POST",
        url: global.base_url + "admin/add_productf",
        data: "id=" + lob_id + '&newname='+name,
        processData: $('#i3').show(),
        dataFilter: function(data) {
            if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') return JSON.parse(data);
            else
                return eval('(' + data + ')');
        },
        success: function(JSONtext) {
            $('#i3').hide();

            if (JSONtext.ok  == "1")
            {
                //alert(JSONtext.msg);
                $("#field_producttype2").append('<option selected=selected value="'+JSONtext.newid+'">'+JSONtext.newname+'</option>');
                $("#field_producttype2").trigger("change");
                $('#prodadddiv').hide();
            }
            else
            {
                $('#i3').hide();
                alert(JSONtext.msg);
            }
        },
        error: function(msg)
        {
            $('#i3').hide();
            alert(msg.responseText);
        }
    });

}

function init_bubble(){

}
