//trim function
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function showhide(targetID, init, specific){
	
	targetDiv = document.getElementById(targetID, init);
	
	if(specific != ''){
	
		targetDiv.style.display = specific;
		
		return true;
	
	}
	
	if(targetDiv.style.display == 'none'){

		targetDiv.style.display = 'block';
		return true;
		
	} else if(targetDiv.style.display == 'block') {
		
		targetDiv.style.display = 'none';
		return true;
		
	} else if(targetDiv.style.display == '' && init == 'open'){
		
		targetDiv.style.display = 'none';
		return true;
		
	} else if(targetDiv.style.display == '' && init == 'closed'){
	
		targetDiv.style.display = 'block';
		return true;
	
	}
	
	return false;
	
}

//spry filters
function usersFilter(ds, row, rowNumber){
	
	//ok?
	ok = true;
	
	//if all status is not selected
	if(document.getElementById('userStatus').value != 'all'){
		
		//if its not the right status we dont want it
		if(trim(row['status']) != trim(document.getElementById('userStatus').value)){
			ok = false;	
		}
		
	}
	
	//if filter isnt empty
	if(document.getElementById('userFilter') != ''){
		
		var regExpStr = document.getElementById('userFilter').value;     
		var regExp = new RegExp(regExpStr, "i");
		
		if(row['fname'].search(regExp) != -1 || row['sname'].search(regExp) != -1){
			//cool
		} else {
			ok = false;	
		}
		
	}
	
	//give em what we got
	if(ok){
		return row;	
	} else {
		return null;	
	}
	
}

function changeImage(image, target){

	image.src = target;

}

function isset(  ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: FremyCompany
    // +   improved by: Onno Marsman
    // *     example 1: isset( undefined, true);
    // *     returns 1: false
    // *     example 2: isset( 'Kevin van Zonneveld' );
    // *     returns 2: true
    
    var a=arguments, l=a.length, i=0;
    
    if (l===0) {
        throw new Error('Empty isset'); 
    }
    
    while (i!==l) {
        if (typeof(a[i])=='undefined' || a[i]===null) { 
            return false; 
        } else { 
            i++; 
        }
    }
    return true;
}


//pushing enter will not submit a form
function noenter() {
  return !(window.event && window.event.keyCode == 13); 
}

function in_array(needle, haystack, argStrict) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true
    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
 
    var key = '', strict = !!argStrict;
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
				
				//alert(haystack[key]+' same as '+needle);
                return true;
            }
        }
    }
 
    return false;
}

function reciept2form(man){
	
	//add members to recipient field

	if(recipArr.length > 0 || !man){
	
		for(a=0; a<recipArr.length; a++){
			
			ident = recipArr[a];
			document[man].reciept.value += ident+',';

		}
		
		return true;
		
	} else {

		return false;
	
	}

}

function reciept2formSpec(form, arr, field, man){
	
	//add members to recipient field
	
	if(recipArr.length > 0 || !man){
	
		for(a=0; a<arr.length; a++){
			
			ident = arr[a];
			document[form].field.value += ident+',';

		}
		
		return true;
		
	} else {

		return false;
	
	}

}

function CheckInbox()
{
	
	i=0;
	dir = document.inbox.Check_ctr.checked

	while(document.getElementById('check_list'+i)){

		document.getElementById('check_list'+i).checked = dir;
		i++;
		
	}
	
}

//change whitespace to underscores
function white_space(str)
{
	var spaces = str.length;
	  for(var x = 1; x<spaces; ++x){
	   str = str.replace(' ', "_");
	 }
	return str;
}

function sortNumber(a,b)
{
	return a - b;
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

//get root url
function root(){

	var url = document.location.toString() ; //url
	var e_url = '' ; //edited url
	var p = 0 ; //position
	var p2 = 0 ;//position 2
	p = url.indexOf("//") ;
	e_url = url.substring(p+2) ;
	p2 = e_url.indexOf("/") ;
	root_url = url.substring(0,p+p2+3);
	
	if(root_url == 'http://192.168.0.101/'){
		//test server
		return root_url+'PTAdmin/bank/system/v3/3.0/';
	} else {
		//live
		return root_url;
	}
	
}

// proper case string prptotype (JScript 5.5+)

String.prototype.toProperCase = function()
{
  return this.toLowerCase().replace(/^(.)|\s(.)/g, 
      function($1) { return $1.toUpperCase(); });
}

//confirm a user wants that action
function confirmClick(url){
	
		showDialog('Confirmation','Are you sure?<br/><br/><a href="'+url+'"><img src="'+root()+'kernel/images/generic_yes_btn.gif" alt="Yes"/></a><img src="'+root()+'kernel/images/generic_no_btn.gif" onclick="hideDialog();" alt="No"/>','prompt');
	
}

String.prototype.isTextOrHyphen = function () 
{
	
	return /^[A-Za-z\055]*$/.test(this);
	
}


function checkNameForIllegalChars(input) {
	
	if(!input.value.isTextOrHyphen()){
		
		input.value = input.value.replace(/[^A-Za-z\055]*$/, '')
		input.focus();

		showDialog('Error','Please only use letters and hypens.','error','2');
		
	} 

}

function dropDownValue(id){

	return document.getElementById(id).options[document.getElementById(id).selectedIndex].value;

}

function getContentFromIframe(iFrameName)
{

    var myIFrame = document.getElementById(iFrameName);
    var content = myIFrame.contentWindow.document.body.innerHTML;

    //Do whatever you need with the content
	
	return content;

}

function goToPage(input){
	
	url = unescape(input.value);
	
	if(validateURL(url)){
		input.value = '';
		document.location = url;
	} else {
		return false;	
	}
	
}


function validateURL(url){
	
	lengthValue = url;
	lengthValue = lengthValue.length;
	
	if(lengthValue != 0){
		var j = new RegExp();
		j.compile("^[A-Za-z]+://[A-Za-z0-9-]+\.[A-Za-z0-9]+");
		lengthValue = url;
	
		if (!j.test(lengthValue)){
			return false;
		} else {
			return true;	
		}
	}
}

function showDescription(select){
    
    //hide all descriptions
    jQuery("#description_div").children().each(function(){
        
        jQuery(this).css("display","none");
        
    });
    
    //show the selected description
    jQuery("#"+jQuery(select).val()).css("display","block");
    
}