function reloadPage(mode)
{
    var url = '';

    switch(mode)
    {
        case 'bank_name' :
            if($('#blank').val() == '')
                return false;
            if($('#blank').val() != '') url = var_path + '/' + var_module + '/' + var_action + '/edit/false/' + 'bank_name/' + $('#blank').val();
            break;

        case 'branch_name' :
            if($('#blank2').val() == '')
                return false;
            if($('#blank2').val() != '') url = var_path + '/' + var_module + '/' + var_action + '/edit/false/' + 'bank_name/' + $('#blank').val() + '/' + 'branch_name/' + $('#blank2').val();
            break;

        case 'req' :
            if($('#reqType').val() != '') { url = var_path + '/' + var_module + '/' + var_action + '/' + 'req/' + $('#reqType').val(); }
            else
                url = var_path + '/' + var_module + '/' + var_action + '/' + 'req/';
            break;
     
        case 'state' :
            if($('#blank').val() != '') url = var_path + '/' + var_module + '/' + var_action + '/' + 'state/' + $('#blank').val();
            break;

        case 'office' :
            if($('#blank2').val() != '') url = var_path + '/' + var_module + '/' + var_action + '/' + 'state/' + $('#blank').val() + '/office/' + $('#blank2').val();
            break;

        case 'role' :
            if($('#blank3').val() != '') url = var_path + '/' + var_module + '/' + var_action + '/' + 'state/' + $('#blank').val() + '/office/' + $('#blank2').val() + '/role/' + $('#blank3').val();
            break;
	
        case 'category' :
            if(var_id == ''){
                if($('#blank4').val() != '') url = var_path + '/' + var_module + '/' + var_action + '/category/' + $('#blank4').val();
            }else {
                if($('#blank4').val() != '') url = var_path + '/' + var_module + '/' + var_action + '/id/'+ var_id + '/category/' + $('#blank4').val() + '/n/' + $('#n').val()+ '/c/' + $('#c').val()+ '/p/' + $('#p').val();
            }
            break;
	
        case 'subcategory' :
            if($('#blank5').val() != '')
                if(var_id == ''){
                    if($('#blank4').val() != '') url = var_path + '/' + var_module + '/' + var_action + '/category/' + $('#blank4').val() + '/subcategory/' + $('#blank5').val();
                }else {
                    if($('#blank4').val() != '') url = var_path + '/' + var_module + '/' + var_action + '/id/'+ var_id + '/category/' + $('#blank4').val() + '/subcategory/' + $('#blank5').val() + '/n/' + $('#n').val()+ '/c/' + $('#c').val()+ '/p/' + $('#p').val();
                }
            break;

        case 'req_category' :
            if(action == 'add')
                if(var_id == ''){
                    if($('#blank4').val() != '') url = var_path + '/' + var_module + '/' + var_action + '/category/' + $('#blank4').val() + '/requisitionid/' + $('#requisitionid').val() + '/delivery_date/' + $('#delivery_date').val();
                }else {
                    if($('#blank4').val() != '') url = var_path + '/'  + var_module + '/' + var_action + '/' + var_id + '/category/' + $('#blank4').val() + '/requisitionid/' + $('#requisitionid').val()+ '/delivery_date/' + $('#delivery_date').val();
                }
            else if(action == 'edit')
                if(var_id == ''){
                    if($('#blank4').val() != '') url = var_path + '/' + var_module + '/' + var_action + '/category/' + $('#blank4').val() + '/requisitionid/' + $('#requisitionid').val()+ '/delivery_date/' + $('#delivery_date').val();
                }else {
                    if($('#blank4').val() != '') url = var_path + '/'  + var_module + '/' + var_action + '/' + var_id + '/category/' + $('#blank4').val() + '/requisitionid/' + $('#requisitionid').val()+ '/delivery_date/' + $('#delivery_date').val();
                }
            break;

        case 'req_subcategory' :
            if($('#blank5').val() != '')
                if(action == 'add')
                    if(var_id == ''){
                        if($('#blank4').val() != '') url = var_path + '/' + var_module + '/' + var_action + '/category/' + $('#blank4').val() + '/subcategory/' + $('#blank5').val() + '/requisitionid/' + $('#requisitionid').val()+ '/delivery_date/' + $('#delivery_date').val();
                    }else {
                        if($('#blank4').val() != '') url = var_path + '/' + var_module + '/' + var_action + '/'+ var_id + '/category/' + $('#blank4').val() + '/subcategory/' + $('#blank5').val() + '/requisitionid/' + $('#requisitionid').val()+ '/delivery_date/' + $('#delivery_date').val();
                    }
                else if(action == 'edit')
                    if(var_id == ''){
                        if($('#blank4').val() != '') url = var_path + '/' + var_module + '/' + var_action + '/category/' + $('#blank4').val() + '/subcategory/' + $('#blank5').val() + '/requisitionid/' + $('#requisitionid').val()+ '/delivery_date/' + $('#delivery_date').val();
                    }else {
                        if($('#blank4').val() != '') url = var_path + '/' + var_module + '/' + var_action + '/' + var_id + '/category/' + $('#blank4').val() + '/subcategory/' + $('#blank5').val() + '/requisitionid/' + $('#requisitionid').val()+ '/delivery_date/' + $('#delivery_date').val();
                    }
            break;

        case 'req_product_type' :
            if($('#blank6').val() != ''){
                if(action == 'add')
                    if(var_id == ''){
                        if($('#blank4').val() != '') url = var_path + '/' + var_module + '/' + var_action + '/category/' + $('#blank4').val() + '/subcategory/' + $('#blank5').val() + '/requisitionid/' + $('#requisitionid').val() + '/product_type/' + $('#blank6').val() + '/delivery_date/' + $('#delivery_date').val();
                    }else {
                        if($('#blank4').val() != '') url = var_path + '/' + var_module + '/' + var_action + '/'+ var_id + '/category/' + $('#blank4').val() + '/subcategory/' + $('#blank5').val() + '/product_type/' + $('#blank6').val() + '/requisitionid/' + $('#requisitionid').val() + '/delivery_date/' + $('#delivery_date').val();
                    }
                else if(action == 'edit')
                    if(var_id == ''){
                        if($('#blank4').val() != '') url = var_path + '/' + var_module + '/' + var_action + '/category/' + $('#blank4').val() + '/subcategory/' + $('#blank5').val() + '/requisitionid/' + $('#requisitionid').val() + '/product_type/' + $('#blank6').val() + '/delivery_date/' + $('#delivery_date').val();
                    }else {
                        if($('#blank4').val() != '') url = var_path + '/' + var_module + '/' + var_action + '/'+ var_id + '/category/' + $('#blank4').val() + '/subcategory/' + $('#blank5').val() + '/product_type/' + $('#blank6').val() + '/requisitionid/' + $('#requisitionid').val() + '/delivery_date/' + $('#delivery_date').val();
                    }
            }
            break;
	
        default:
            url = var_path + '/' + var_module + '/' + var_action;
            break;
    }

    window.location = url;

}

function setPreviousAmount(){
    var count = getCount()
    getAmount(count);
}

function getCount(){

    var count =0;
    //var a = document.getElementsByTagName('checkbox');
    for(i=0, j=0, count=0; i< $('input:checkbox').length; i++)
    {
        j++;

        if( $('#sf_guard_user_vendorInfo_category_list_'+j).attr('checked') )
        {
            count++;
        }
    }
    return count;
}


function set_payment(id){

    if(id!="")
    {
        if($('#'+id).attr('checked')){

            var count = getCount()
            getAmount(count);
        }else{
            count = getCount()
            getAmount(count);
        }
    }
}
function getAmount(count){

    var  amount = 0;
    if(count == 1){
        amount = 10000;
    }else if(count == 2){
        amount = 10000 + 5000;
    }else if(count >= 3){
        amount = 5000 + (10000*(count-1));
    }
    if(amount != 0){
        $('#amount_val_div').css('display','block');
        $('#amount_val').html(amount);
        $('#amount_to_paid').val(amount);
    }else{
        $('#amount_val_div').css('display','none');
        $('#amount_val').html(0);
        $('#amount_to_paid').val(0);
    }
}

//script for multi-select check box
//var formblock;
//    var forminputs;
//
//   function prepare() {
//   formblock= document.getElementById('collate_Requests');
//   forminputs = formblock.getElementsByTagName('input');
//   }
//
//   function select_all(name, value) {
//   for (i = 0; i < forminputs.length; i++) {
//   // regex here to check name attribute
//   var regex = new RegExp(name, "i");
//   if (regex.test(forminputs[i].getAttribute('name'))) {
//   if (value == '1') {
//   forminputs[i].checked = true;
//   } else {
//   forminputs[i].checked = false;
//   }
//   }
//   }
//   }
//
//   if (window.addEventListener) {
//   window.addEventListener("load", prepare, false);
//   } else if (window.attachEvent) {
//   window.attachEvent("onload", prepare)
//   } else if (document.getElementById) {
//   window.onload = prepare;
//   }


function validate_required(field,alerttxt)
{
    with (field)
    {
        if (value==null||value=="")
        {
            alert(alerttxt);return false;
        }
        else
        {
            return true;
        }
        }
}

function validate_grp(thisform)
{
    with (thisform)
    {
        if (validate_required(gpTitle,"Please input Group title to collate request!")==false)
        {
            gpTitle.focus();return false;
        }

        if (validate_required(chk_sel,"You have not selected any item to submit!")==false)
        {
            chk_sel.focus();return false;
        }
        }
}

function update(){
    document.getElementById('chk_sel').value = 1;
}

function scoreSheetPrompt()
{
    if( confirm( 'Do you want to add Criteria Main header? \nClick "OK" for Yes, or "Cancel" for No.' ) )
        window.location = var_path + '/' + var_module + '/' + var_action + '?step=3a';
    else
        window.location = var_path + '/' + var_module + '/' + var_action + '?step=3b';
}

function scoreSheetPromptEdit(id)
{
    if( confirm( 'Do you want to add Criteria Main header? \nClick "OK" for Yes, or "Cancel" for No.' ) )
        window.location = var_path + '/' + var_module + '/' + var_action + '?id='+id+'&step=3a';
    else
        window.location = var_path + '/' + var_module + '/' + var_action + '?id='+id+'&step=3b';
}

function showOffices(mode)
{
    switch(mode)
    {
        case 's':
            $('#office_h').fadeOut();
            $('#office_z').fadeIn();
            $('#select_h').attr('disabled', true);
            $('#select_z').attr('disabled', false);
            break;

        case 'z':
            $('#office_z').fadeOut();
            $('#office_h').fadeIn();
            $('#select_z').attr('disabled', true);
            $('#select_h').attr('disabled', false);
            break;

        case 'h':
            $('#office_z').fadeOut();
            $('#office_h').fadeOut();
            $('#select_h').attr('disabled', true);
            $('#select_z').attr('disabled', true);
            break;
    }
}

function checkVal(obj, val, parent)
{    
    if( $(obj).val() > val )
    {
        alert('The alloted score cannot be more than the maximum score.');
        $(obj).val('');
        $(obj).focus();
        return false;
    }

    if( isNaN($(obj).val()) )
    {
        alert('The alloted score has to be a number.');
        $(obj).val('');
        $(obj).focus();
        return false;
    }

    if( $(obj).val() == '' )
    {
        alert('Please enter a value in the alloted score field.');
        $(obj).val('');
        $(obj).focus();
        return false;
    }

    temp = $('#hid_'+parent).val();
    temp2 = temp.split(',');
    total = 0;

    for(i=0; i<temp2.length; i++)
    {
        total = parseFloat(total) + parseFloat($('#value_'+temp2[i]).val());
        total = Math.round(total*100)/100;
    }
    $('#total_'+parent).html(total);

    temp = $('#parent_ids').val();
    temp2 = temp.split(',');
    grand_total_var = 0;
    for(i=0; i<temp2.length; i++)
    {
        if($('#total_'+temp2[i]).html() == NaN)
            $('#total_'+temp2[i]).html(0);
        grand_total_var = parseFloat(grand_total_var) + parseFloat($('#total_'+temp2[i]).html());
        grand_total_var = Math.round(grand_total_var*100)/100;
    }

    $('#grand_total').html(grand_total_var);
}

function exportCSV()
{
    if($('#blank2').val() == '')
        alert('Please select a template to export.');
    else
        window.location.href = var_path + '/bid/' + 'exportTemplate?id=' + $('#blank2').val();
}

function quantityFormula(quantity,rowName){
    var te = 'iQ'+rowName;
    var te1 = 'bQ'+rowName;
    var issuedQuantity = document.getElementById(te).value;
    if(isNaN(issuedQuantity))
    {
        alert('Issued quantity has to be a numeric value.');
        document.getElementById(te1).value = '';
        document.getElementById(te).value = '';
        return false;
    }

    if(issuedQuantity > quantity){
        alert('Issued quantity cannot be greater then requested quantity.');
        document.getElementById(te1).value = '';
        return false;
    }else if(issuedQuantity == '' ){
        return false;
    }else{
        document.getElementById(te1).value = quantity - issuedQuantity;
    }
}
function quantityCheck(rowName){
    var te = 'iQ'+rowName;
    var te1 = 'bQ'+rowName;
    var refnum = 'rfN'+rowName;
    
    var issuedQuantity = document.getElementById(te).value;
    if(issuedQuantity == '' || document.getElementById(te1).value == '' || document.getElementById(refnum).value == ''){
        return false;
    }
}

function checkUploadedImage(status)
{
    if($('#img_uploaded').val() == '')
    {
        alert('Please upload a profile image.');
        return false;
    }
    else
    {
        if(status == 'save')
        {
            if(confirm('Please Confirm your Information before saving [Ok] to Confirm or [Cancel] to Abort saving information.'))
                return true;
            else
                return false;
        }
    }
}

function checkBankDetails()
{
    if($('#tbl_vendor_bank_details_bank_name').val() == '' || $('#tbl_vendor_bank_details_branch_name').val() == '' || $('#tbl_vendor_bank_details_sort_code').val() == '')
    {
        alert('The bank name, branch name and the sort code are required.');
        return false;
    }
}

function checkVendorClass()
{
    if( parseInt($('#start_amount').val()) > parseInt($('#end_amount').val()) )
    {
        alert('The start amount cannot be greater than the end amount.');
        return false;
    }
}

function makeUsername()
{
    var fname = $('#sf_guard_user_details_first_name').val();
    var lname = $('#sf_guard_user_details_last_name').val();
    
    if( fname == '' && lname == '')
        return false;

    var username = fname+'.'+lname;

    var username_len = username.length;

    if(username_len > 100)
        username = username.substr(0,100);

    $('#sf_guard_user_username').val(username);
}

function getCmdList(state_id_edit, command_office_id)
{
    if(state_id_edit == 'undefined' || state_id_edit == '' || !state_id_edit)
        var state_id = $('#blank1').val();
    else
        var state_id = state_id_edit;

    if(state_id == '')
        return false;

    $.ajax({
        type: "POST",
        url: var_path + '/' + var_module + '/getCommandsForState/',
        data: 'state_id='+state_id,
        dataType: 'json',
        success: function(msg){

            if( msg.status == 'true' )
            {               
                select_obj = document.getElementById('blank2');
                select_obj.options[0] = new Option('--Please Select--', '');
                i = 1;
                $.each(msg.data, function(key,val){
                    if(key == command_office_id)                        
                        select_obj.options[i] = new Option(val, key, true);                        
                    else
                        select_obj.options[i] = new Option(val, key);
                    i++;
                });
                $('#blank2').val(command_office_id);
            }
        }
    });
}

function calculateBidPrice()
{
    if( $.trim($('#tbl_bids_tender_quantity').val()) == '' || $.trim($('#tbl_bids_bid_unit_price').val()) == '')
    {
        $('#tbl_bids_total_bid_amount').val('');
        return false;
    }

    if( isNaN($('#tbl_bids_tender_quantity').val()) || isNaN($('#tbl_bids_bid_unit_price').val()) || $('#tbl_bids_bid_unit_price').val() == 0 || $('#tbl_bids_tender_quantity').val() == 0 )
    {
        $('#tbl_bids_total_bid_amount').val('');
        return false;
    }

    if( $.trim($('#tbl_bids_tender_quantity').val()) != '' && $.trim($('#tbl_bids_bid_unit_price').val()) != '')
    {
        val = $('#tbl_bids_tender_quantity').val() * $('#tbl_bids_bid_unit_price').val();
        $('#tbl_bids_total_bid_amount').val(val);
    }
}
