// Adds an is_blank method to strings.
// Returns true if the string contains any non-whitespace characters, false otherwise

String.prototype.is_blank = function() {
	return ( this.match(/\S/) == null );
};

String.prototype.is_valid_email = function() {
    var email_expression = /^([A-Za-z0-9_\-\.\+])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return ( this.match(email_expression) != null);
};

String.prototype.is_valid_password = function() {
	var return_val = {};
	return_val['success'] = true;
	return_val['errors'] = Array();
	
	if(this.length < 8) {
		return_val['success'] = false;
		return_val['errors'].push('The password must be longer (min. 8 characters)');
	}
	return return_val;
};

String.prototype.is_valid_serial_number = function() {
	var sn_expression = /^[A-Z*]{0,2}[0-9]{1,8}[A-Z\*]?$/;
    return ( this.match(sn_expression) != null);
};

String.prototype.is_valid_price = function() {
	var price_expression = /^[0-9]*(\.[0-9][0-9]?)?$/;
	
	return ( this.match(price_expression) != null);
};

String.prototype.is_standard_chars_only = function() {
	var char_expression = /^[A-Za-z0-9\*\s-_:]*$/;
	
	return ( this.match(char_expression) != null );
};

String.prototype.is_valid_date = function() {
	var parts = this.split('-');
	if(parts.length != 3) {
		return false;
	}
	
	var date = mktime(0, 0, 0, parts[0], parts[1], parts[2] );
	return date !== false;
};

String.prototype.is_valid_attribute = function() {
	return this.is_standard_chars_only();
}

function mktime () {
    var d = new Date(),
        r = arguments,
        i = 0,
        e = ['Hours', 'Minutes', 'Seconds', 'Month', 'Date', 'FullYear'];
 
    for (i = 0; i < e.length; i++) {
        if (typeof r[i] === 'undefined') {
            r[i] = d['get' + e[i]]();
            r[i] += (i === 3); // +1 to fix JS months.
        } else {
            r[i] = parseInt(r[i], 10);
            if (isNaN(r[i])) {
                return false;
            }
        }
    }
 
    // Map years 0-69 to 2000-2069 and years 70-100 to 1970-2000.
    r[5] += (r[5] >= 0 ? (r[5] <= 69 ? 2e3 : (r[5] <= 100 ? 1900 : 0)) : 0);
 
    // Set year, month (-1 to fix JS months), and date.
    // !This must come before the call to setHours!
    d.setFullYear(r[5], r[3] - 1, r[4]);
 
    // Set hours, minutes, and seconds.
    d.setHours(r[0], r[1], r[2]);
 
    // Divide milliseconds by 1000 to return seconds and drop decimal.
    // Add 1 second if negative or it'll be off from PHP by 1 second.
    return (d.getTime() / 1e3 >> 0) - (d.getTime() < 0);
}
