
function validate(form)
{
	var valid = false;
	var errors = 0;
	//var form = document.getElementById('registerform');
	var reqfields = form.required.value.split(',');
	var errorObj = form.errors;
	for(i=0;i<reqfields.length;i++) {

		valid = false;
		field = form.elements[reqfields[i]];
		switch(field.id.toLowerCase())
		{
			case 'email':
				if (validEmail(field.value))
					valid = true;
			break;
			case 'phone':
				if (validPhone(field.value))
					valid = true;
			break;
			case 'ssn':
				if (validSsn(field.value))
					valid = true;			             
			break;
			case 'ccnr':
				if (validCcnr(field.value))
					valid = true;
			break;
			case 'ccmonth':
				if (validMonth(field.value))
					valid = true;
			break;
			case 'ccyear':
				if (validYear(field.value))
					valid = true;			              
			break;
			case 'code':
				if (validCode(field.value))
					valid = true;			              
			break;
			case 'pin':
				if (validPin(field.value))
					valid = true;			              
			break;
			default:
				if (field.value != "")
					valid = true;
			break;
		}

		if (valid)
		{
			field.style.background="#fff";
		}
		if (!valid)
		{
			field.style.background="#fcc";
			errors++;
		}

	}
	
	if (errors == 0) {
		var tImageFile = '/sms-kubbur/sendi_sms.gif';
		rS = document.getElementById('rSending');
		rS.innerHTML = '<img src="' + tImageFile + '" border="0" hspace="5">';		
		return true;	
	}
	else {
		error = document.createTextNode("Athugið að allir reitir séu rétt fylltir inn!");
		document.getElementById('rSending').innerHTML = "";
		document.getElementById('rSending').appendChild(error);
		return false;
	}







	/* Functions */

	function validEmail(str) 
	{
		return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
	}
	function validPhone(str) 
	{
		return str.match(/^[0-9]{7}$/);
	}
	function validSsn(str) 
	{
		return str.match(/^[0-9]{10}$/);
	}
	function validCcnr(str) 
	{
		if (str.match(/^[0-9]{13}$/))
		{
			return true;
		}
		else if (str.match(/^[0-9]{15}$/))
		{
			return true;
		}
		else if (str.match(/^[0-9]{16}$/))
		{
			return true;
		}
		else
			return false;
	}
	function validMonth(str) 
	{
		num = str * 1;
		if (num > 0 && num <= 12)
		{
			return true;
		}
		else {
			return false;
		}
	}
	function validYear(str) 
	{
		num = str * 1;
		if (num >= 7 && num <= 30)
		{
			return true;
		}
		else {
			return false;
		}
	}
	function validCode(str) 
	{
		return str.match(/^[0-9]{4}$/);
	}
	function validPin(str) 
	{
		return str.match(/^[0-9]{4}$/);
	}

}
