// Bayionline javascript functions. Mostly utilities
function submit_form(id) {
    return document.getElementById(id).submit();
}

function submit_form_with_confirm(id, message) {
    if (confirm(message)) {
        return submit_form(id);
    }
    return false;
}

function make_option(id, value, label) {
	return "<option id=\"" + id + "\" value=\"" + value + "\">" + label + "</option>";
}

function window_print() {
    return call_with_return(window.print(), false);
}

// Functional programming for self satisfaction
function call_with_return(expr, retval) {
    return retval;
}

function change_input_value(input_id, value) {
	$('#'+input_id)[0].value = value;
}

function toggleDiv(divid) {
    var theDiv = '#' + divid;
    if ($(theDiv).is(':visible')){
        $(theDiv).hide();
    } else {
        $(theDiv).show();
    }
}

function emailCheck(str) { 
    var i,ch; 
    var re ;
    re = new RegExp("^([\\w-]+(?:\\.[\\w-]+)*)@((?:[\\w-]+\\.)*\\w[\\w-]{0,66})\\.([a-z]{2,6}(?:\\.[a-z]{2})?)");   
    if (str.length == 0) return(false);
    if (str.match(re) == null){
        return(false);
    }
    return(true);
}   
    
function displayCheck(formName, divhata, divrapor, divonay){
    var emailAddress= document.getElementById('email').value;
    if (emailCheck(emailAddress)) {
        document.forms[formName].submit();
    } else {
        $('#'+divhata).show();
        $('#'+divonay).hide();
    }
    return false;
}

function sendReportMail() {
	$('#hataEposta').hide();
	$('#hata').hide();
	$('#onay').hide();
	if (emailCheck($('#email').val())) {
		$.get('_fatura_listesi',{
			'act': 'imei_mail',
			'docid' : $('#docid').val(),
			'email': $('#email').val()
		},
		function (data){
			if (data == 1) {
				$('#onay').show();
			} else if (data == 0) {
				$('#hataEposta').show();
			} else {
				alert("Unknown Error")
			}
		});
	} else {
		$('#hata').show();
	}
}

//
// This function uses at gts reporting. It changes order status in database
//
function change_order_status(val, el, formName, txt, error) {
	if (val) {
	    var sure = confirm(txt);
		if (sure) {
			document.forms[formName].elements[el].value = val;
			return submit_form(formName);
		} else {
			return false;
		}
	} else {
		alert(error);
	}
 }


/**
 * Check if the given process type is cancelled.
 * if it is not, notify the user;
 * if it is, go to the cancel detail page
 *
 */
//Ex: <a href="#" onclick="document.location='<?=linkbylabel('gts_onaylandi')?>&orderid=' + $('input[@name=cancel_radio][@checked]').val()" class="printButton right"></a>
function cancel_detail_page(val, link, error) {
	if (val) {
		window.open(link + '&orderid=' + val, 'mywindow', 'width=900, height=900, scrollbars=1')
	} else {
		alert(error);
	}
}

function formcontrol(formName){
    var theform= document.forms[formName];
        
    theform.submit();
}

function formclean(formName,p1){

    var p = new Array();
    p = p1.split(',');
    var theform= document.forms[formName];
    var length = p.length;
     for (var i=0; i < length; i++)
     {
        if (p[i] != ""){
            if ( i< 2){
                theform.elements[p[i]].value ="";
            }
            else
                theform.elements[p[i]].value = 0;
        }
    }

}

/*
 * calculateScreenSize
 *
 * calculates the whole width and size of page inside the browser
 * window and returns it as an [x, y] array
 *
 */
function calculateScreenSize() {
	var sizes = {}

	sizes["width"] = document.body.scrollWidth || 1024
	sizes["height"] = document.body.scrollHeight || 768
	sizes["inner_width"] = document.body.offsetWidth || window.innerWidth || 1024
	sizes["inner_height"] = document.body.offsetHeight || window.innerHeight || 768

	return sizes
}

/**
 * used in GTS Report page to hide the 'cancel details' button for the approved orders
 *
 */
function hide_detail_button() {
	$('#cancel_detail_id').hide();
	$('#cancel_order_id').show();
}

/**
 * used in GTS Report page to hide the 'cancel order' button for the cancelled orders
 *
 */
function hide_cancel_button() {
	$('#cancel_detail_id').show();
	$('#cancel_order_id').hide();
}

/**
 * used in GTS Report page to either print the report page or print the selected order's detail page
 *
 */
function redirectOrPrint(val, link, user) {
	if (val) {
        window.open(link + '&orderid=' + val + '&userid=' + user + '&report=true', 'mywindow', 'width=900, height=900, scrollbars=1');
	} else {
		window.print();
	}
}

/**
 * This function resizes the image within the range of gicen parameter 
 * but also keeps the ratio of the width/height value
 * 
 *  Use this in the onLoad() function of an img tag
 *
 */
function reDimensionImage(img, height, width) { 
    var w = img.width;
    var h = img.height;
    if (h>height || w>width) 
        if(h>w){
            w = w*height/h;
            h = height;    
        }
        else {
            h = h*width/w;
            w = width;
        }
    img.width = w;
    img.height = h;
}

function checkFileExists(file, link, txt) { 
	if (file) {
		window.open(link, 'mywindow', 'toolbar=yes, location=yes, directories=yes, status=yes, menubar=yes, scrollbars=yes, copyhistory=yes, resizable=yes')
	} else {
		alert(txt);
	}
}

function showDiv(divid) {
    var theDiv = '#' + divid;
    $(theDiv).show();
}

function hideDiv(divid) {
    var theDiv = '#' + divid;
    $(theDiv).hide();
}

function changeUserGTSValue(userName) {
	$('#userGTS').val(userName);
}
