/**
 * Common javascript function used in the web
 */

/**
 * Trim spaces from right
 */
function rightTrim(strIn) {
	return strIn.replace(/\s+$/gi, "");
}


function WindowOnload(f) {
    var prev=window.onload;
    window.onload=function(){ if(prev)prev(); f(); }
}

/**
 * Trim spaces from left
 */
function leftTrim(strIn) {
	return strIn.replace(/^\s*/gi, "");
}

/**
 * Trim spaces from right/left
 */
function trim(strIn) {
	return leftTrim(rightTrim(strIn));
}

/**
 * A field made an error in validation, show error and more
 * @param theField A form component were the error occured
 * @param msg The message to show, if empty, then no alert
 */
function errorOnField(theField, theForm, msg) {
	if (msg.length>0) {
		alert(msg);
	}

	// Change old elements to use default style
	elements = theForm.elements;
	esize = elements.length;
	for (i=0; i<esize; i++) {
		elements[i].className="";
	}
	
	theField.select();
	theField.focus();
	
	theField.className="form-error";
}

/**
 * Allow only numeral input in form input.
 * To use with form <input type.... onkeypress="return numeralsOnly(event)">
 * @param evt The event from the form field
 * @param boolean showAlert Iff true then show alert
 * @return boolean True if input was a number and false otherwise
 */
function numeralsOnly(evt, showAlert) {
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
	if (charCode > 31 && (charCode < 48 || charCode > 57)) {
		if (showAlert) {
			alert("Only numerals allowed");
		}
		return false;
	} else {
		return true;
	}
}

/**
 * Validate if a field length is within a certain range
 * @param String value The value to check. Spaces from start/end are trimmed
 * @param int min Minimum length, if -1 then don't check lower limit
 * @param int max Maxmimum length, if -1 then don't check high limit
 * @return true if validates and false otherwise
 */
function validateSizeLimit(value, min, max) {
	tValue = trim(value);

	if (min>0 && min>tValue.length) {
		return false;
	}
	if (max>0 && max<tValue.length) {
		return false;
	}

	return true;
}


/**
 * Validate a email address
 * @param string value The address to check
 * @return bool True if email is valid and false otherwise
 */
function checkEmailAddress(value) {
	return value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
}				  

/**
 * Check an Icelandic kt
 * 
 * @param string kt The kt to check
 * @return bool True if a legal kt and false otherwise
 */
function checkKt ( kt ) {
    var temp=kt;
	// Athuga hvort lengdin Ã¡ kennitÃ¶lunni sÃ© rÃ©tt
	if (temp.length != 10) {
		return false;
	}
	// Athuga hvort kennitalan sÃ© rÃ©tt formuÃ°
	var subs1=temp.substr(0,2);
	var subs2=temp.substr(2,2);
	var subs3=temp.substr(4,2);
	var subs4=temp.substr(9,1);
	if (subs1 > 31 || subs1 < 1 || subs2 > 12 || subs2 < 1 || (subs4%9) != 0) {
		return false;
	}
	// Athuga kennitÃ¶lu meÃ° vartÃ¶lu Ãºtreikningi
	var studull="327654321";
	var total=0;
	for (i = 0; i < 9; i++)	{
		total+=temp.substr(i,1)*studull.substr(i,1);
	}
	var mess ="";
	if ((total%11) != 0) {
		mess+= "* Vinsamlegast sláðu inn löglega kennitölu\n";
	}

	if (mess != "") {
		return false;
	}
	return true ;
}
																																

