function preg_match(regEx, string, returnMatch){
    if( typeof returnMatch === "undefined"){
        return regEx.test(string);
    } else if (returnMatch === true) {
        return regEx.exec(string);
    }
}

function str_replace(replace, find, string){
    return string.replace(find, replace);
}

function browser_class_names() {
    var $browser, $classes, $ch_version, $sf_version, $op_version, $ff_version, $matches;
	// add 'class-name' to the $classes array
	// $classes.push('class-name';
	$browser = navigator.userAgent;
	$classes = [];
	
	// Mac, PC ...or Linux
	if ( preg_match(/Mac/, $browser ) ){
		$classes.push('mac');
			
	} else if ( preg_match(/Windows/, $browser ) ){
	           $classes.push('windows');
	               
    } else if ( preg_match(/Linux/, $browser ) ) {
       $classes.push('linux');

    } else {
       $classes.push('unknown-os');
    }
	           
    // Checks browsers in this order: Chrome, Safari, Opera, MSIE, FF
    if ( preg_match(/Chrome/, $browser ) ) {
        $classes.push('chrome');

        $matches = preg_match(/Chrome\/(\d.\d)/i, $browser, true);
        $ch_version = 'ch' + str_replace( '.', '-', $matches[1] );      
        $classes.push($ch_version);

    } else if ( preg_match(/Safari/, $browser ) ) {
        $classes.push('safari');
   
        $matches = preg_match(/Version\/(\d.\d)/i, $browser, true);
        $sf_version = 'sf' + str_replace( '.', '-', $matches[1] );      
        $classes.push($sf_version);
           
    } else if ( preg_match(/Opera/, $browser ) ) {
        $classes.push('opera');
   
        $matches = preg_match(/Opera\/(\d.\d)/i, $browser, true);
        $op_version = 'op' + str_replace( '.', '-', $matches[1] );      
        $classes.push($op_version);
           
    } else if ( preg_match(/MSIE/, $browser ) ) {
        $classes.push('msie');
   
        if( preg_match(/MSIE 6.0/, $browser ) ) {
           $classes.push('ie6');
        } else if ( preg_match(/MSIE 7.0/, $browser ) ){
           $classes.push('ie7');
        } else if ( preg_match(/MSIE 8.0/, $browser ) ){
           $classes.push('ie8');
        } else if ( preg_match(/MSIE 9.0/, $browser ) ){
           $classes.push('ie9');
        }
   
    } else if ( preg_match(/Firefox/, $browser ) && preg_match(/Gecko/, $browser ) ) {
        $classes.push('firefox');

        $matches = preg_match(/Firefox\/(\d)/i, $browser, true);
        $ff_version = 'ff' + str_replace( '.', '-', $matches[1] );      
        $classes.push($ff_version);
           
    } else {
        $classes.push('unknown-browser');
    }
      // return the $classes string
	return $classes.join(" ");
}

document.body.className = "hasJS " + browser_class_names();

