

// (c) CogWork. Website www.cogwork.se, e-mail: info@cogwork.se

jsDirPath  = "js/";
cssDirPath = "css/";

relJsDirPath = relTopDirPath + jsDirPath;

// From www.netlobo.com/javascript_getelementsbyclassname.html
document.getElementsByClassName = function(clsName) {
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for(var i = 0;i < elements.length;i++){
        if(elements[i].className.indexOf(" ") >= 0){
            var classes = elements[i].className.split(" ");
            for(var j = 0;j < classes.length;j++){
                if(classes[j] == clsName)
                    retVal.push(elements[i]);
            }
        }
        else if(elements[i].className == clsName)
            retVal.push(elements[i]);
    }
    return retVal;
};

function reqNews() {
	if ( !org || org=='*' ) {
		alert( 'Fel: Aktuell organisation kan inte identifieras' );
		return;
	}
//	if ( relTopDirPath && relTopDirPath.substring(0,4) == '/cw/' ) host = relTopDirPath;
//	else host = 'http://swingweb.se/';
//	url = host + 'tools/req/join_group/?org=' + org;
	url = relTopDirPath + 'tools/req/join_group/?org=' + org;
	target = '_blank';
	attributes = 'width=360, height=310, location=no, menubar=no, status=no, toolbar=no, scrollbars=yes, resizable=yes';
	window.open( url, target, attributes );
}

function view( type, id, addParams ) {
	url = relTopDirPath;
	if (!type) type = '0';
	url+= 'view/?type=' + type;
	if ( id        ) url+= ';id=' + id;
	if ( addParams ) url+= addParams;
	target = '_blank';
	attributes = 'width=500, height=750, location=no, menubar=no, status=no, toolbar=no, scrollbars=yes, resizable=yes';
	ptr = window.open(url,target,attributes);
	if ( ptr ) ptr.focus();
//	return( false );
}

function newWin( url ) { 
	target = '_blank';
	attributes = 'width=500, height=750, location=no, menubar=no, status=no, toolbar=no, scrollbars=yes, resizable=yes';
	ptr = window.open( url, target, attributes );
//	ptr = window.open( url, '_blank' );
	if ( ptr ) ptr.focus();
}

function popUpWin( url ) {
	target = '_blank';
	attributes = 'width=500, height=750, location=no, menubar=no, status=no, toolbar=no, scrollbars=yes, resizable=yes';
	window.open( url, target, attributes );
}

function deletePost( id, dir ) {
	if ( confirm("Vill du radera den här posten permanent?") ) {
		if ( dir === undefined ) dir = '';
		path = dir + 'delete.php?id='+id;
		window.location = path;
	}
}

function check_all( cb ) {
	for ( var i=0; i<cb.form.elements.length; i++ ) { 
		if ( cb.form.elements[i].type=="checkbox" ) { cb.form.elements[i].checked = cb.checked; } 
	} 
}

// Checks if a string has the form of an email address. Returns true if so, false otherwise.
function validEmail( str ) {
	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,4}(?:\.[a-z]{2})?)$/i;
	return( filter.test( str ) );
}

// Add a trim method to the String class
String.prototype.trim = function() { 
	var str = this;
	str = str.replace( /^\s+|\s+$/g, '' ); 
	str = str.replace( /^(\&nbsp\;)+|(\&nbsp\;)+$/g, '' );
	return str;
};

function render_dynamic_tables() {
	
	tables = document.body.getElementsByTagName( "table" );
	for ( i = 0; i < tables.length; i++ ) {
	
		if ( tables[i].className == 'dynamicTable' ) {
			tbodies = tables[i].getElementsByTagName('tbody');
			if ( tbodies.length > 0 ) {
				tbody = tbodies[0];
				rows = tbody.getElementsByTagName( 'tr' );
				even = true;
				for ( j = 0; j < rows.length; j++ ) {
					row = rows[j];
					if ( row.parentNode.parentNode.className == 'dynamicTable' ) {
						if ( row.className != 'ignore' ) even = !even;
						if ( row.className != 'ignore' ) row.onmouseover = function() { this.style.backgroundColor='#FFFF00';};
						if ( even ) {
							row.style.backgroundColor = '#E0E0FF';
							if ( row.className != 'ignore' ) row.onmouseout  = function() { this.style.backgroundColor = '#E0E0FF';};
						} else {
							row.style.backgroundColor = '#F8F8FF';
							if ( row.className != 'ignore' ) row.onmouseout  = function() { this.style.backgroundColor = '#F8F8FF';};
						}
					}
				}
			}
		}
	}
}

function clearForm( elementID ) {

	what = document.getElementById( elementID );
	
    for (var i=0, j=what.elements.length; i<j; i++) {
        myType = what.elements[i].type;
        if (myType == 'checkbox' || myType == 'radio') 
        	what.elements[i].checked = false;
        if (myType == 'password' || myType == 'text' || myType == 'textarea') 
        	what.elements[i].value = '';
        if (myType == 'select-one' || myType == 'select-multiple') 
        	for (var k=0, l=what.elements[i].options.length; k<l; k++) what.elements[i].options[k].selected = false;
    }
    if ( document.getElementById('maxRows') && document.getElementById('maxRowsDefault') ) document.getElementById('maxRows').value = document.getElementById('maxRowsDefault').value;
}

// 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 "";
}

function cw_formatNumber( numberValue, numberOfDecimals, decimalPoint, thousandSeparator ) {
	
	var val = Number( numberValue );
//alert( numberValue );
	if ( isNaN( val ) ) return 'NaN';
	if ( val == 0.0   ) return '';
	
	if ( !numberOfDecimals  ) numberOfDecimals  = 2;
	if ( !decimalPoint      ) decimalPoint      = ',';
	if ( !thousandSeparator ) thousandSeparator = ' ';
	
	var x = Math.round( val * Math.pow( 10, numberOfDecimals ) );
	
	var isNegative = ( x < 0 );

	var digits = ( '' + Math.abs(x) ).split('');
	var numIntegers = digits.length - numberOfDecimals;

	if ( numIntegers < 0 ) numIntegers--;

	for ( var i = numIntegers; i < 0; i++ ) digits.unshift('0');

	digits.splice( numIntegers, 0, decimalPoint );
	if ( digits[0] == decimalPoint ) digits.unshift('0');

	z = numIntegers;
	while ( z > 3 ) {
		z-= 3;
		digits.splice( z, 0, thousandSeparator );
	}

	var str = digits.join('');
	if ( isNegative ) str = '-' + str;
	
	return str;
}

function cw_parseFloat( str ) {
	
	var str = String( str );
	if ( str === null || str == '' ) return 0.0;
	
	str = str.replace(',', '.');
	str = str.replace(' ', '');
	str = str.replace(' ', '');

	var val = parseFloat( str );

	if ( isNaN( val ) ) return 0.0;
	return val ;
}

function checkSearchOptions() {
	if ( dirtyForm() == true ) showAdvancedSearchOptions = 1;
	else                       showAdvancedSearchOptions = 0;
	updateSearchOptions();
}

// updateSearchOptionsControl() must be called before toggleSearchOptions()
// in order to set the variable showAdvancedSearchOptions
function toggleSearchOptions() {
	if ( showAdvancedSearchOptions ) showAdvancedSearchOptions = 0;
	else                             showAdvancedSearchOptions = 1;
	updateSearchOptions();
}

function cwj_CheckSplitDate( year, month, day, period ) {

	year  = parseInt( year  );
	month = parseInt( month );
	day   = parseInt( day   );
		
	if ( year  < 0 || year  > 9999 ) return "Ogiltigt värde för år"; // Year 0 does not exist and we will not handle dates before year one. 0 means no value
	if ( month < 0 || month > 12   ) return "Ogiltigt värde för månad";		
	if ( day   < 0 || day   > 31   ) return "Ogiltigt värde för dag";		
	
	if ( month > 0 && day == 31 ) {
		switch ( month ) {
			case  4 : return "Det finns bara 30 dagar i april";
			case  6 : return "Det finns bara 30 dagar i juni";
			case  9 : return "Det finns bara 30 dagar i september";
			case 11 : return "Det finns bara 30 dagar i november";
		}
	}
	
	if ( month == 2 && day > 29 ) return "Det finns som mest 29 dagar i februari";
		
	if ( year > 0 && month == 2 && day == 29 ) {
		// Check number of days in februari at selected year
		leapYear = false;
        if ( year%4 == 0 ) {
            if ( year%100 != 0 ) { leapYear = true; }
            else { if( year%400 == 0) { leapYear = true; } }
        }
        if ( !leapYear ) return "År " + year + " är inget skottår";
	}
	if ( year > 0 && period == 'historic' ) {
    	var today = new Date();
    	var curYear = today.getYear();
    	if ( curYear < 1900 ) curYear = curYear + 1900; // FireFox returns years after 1900...
    	if ( year > curYear ) return "Datumet måste ha passerat";
    	if ( year == curYear && month > 0 ) {
    		curMonth = 1 + today.getMonth();
    		if ( month > curMonth ) return "Datumet måste ha passerat";
    		if ( month == curMonth && day > 0 ) {
    			curDay = today.getDate();
    			if ( day > curDay ) return "Datumet måste ha passerat";
    		}
    	}
	}
	return '';
}

/*
// Usage:  onkeydown="inhibitCtrlKey(event);"
function inhibitCtrlKey( e ) {

	return;
	
	// Inhibits AltGr as well on some computers! Fix before activating again!
	
	var evt = ( typeof window.event != 'undefined' ? window.event : e );
//	var val = evt.keyCode == 86;

//	if ( evt.ctrlKey && val) {
	if ( evt.ctrlKey ) {
		if ( evt.stopPropagation ) {
			evt.preventDefault();
			evt.stopPropagation();
		} else {
			evt.cancleBubble = true;
			evt.returnValue = false;
		}
	}
}
*/
