// JavaScript Document

FormChecker = function()
{
	this.forms   = Object();
	this.enabled = Object();
	
	this.addElement = function(form, name, check)
	{
		if(this.forms[form] == null)
			this.forms[form] = new Object();
		
		this.forms[form][name] = check;	
		this.enabled[name] = true;
	}
	
	this.clear = function(form)
	{
		this.forms[form] = null;	
	}
	
	this.enable = function(name, bool)
	{
		this.enabled[name] = bool;
	}
	
	this.checkForm = function(form)
	{
		var check = true;
		
		for (var element in this.forms[form])
		{
			if(this.enabled[element])
			{	
				var ok = false;
				
				try
				{
					eval(this.forms[form][element]);
				}
				catch(e)
				{
					alert(e + ' - ' + e.description);	
				}
		
				if(!ok)
				{				
					this.displayError(form, element, true);	
					check = false
				}
				else
					this.displayError(form, element, false);	
			}	
			else
				this.displayError(form, element, false);	
		}
		
		return check;		
	}
	
	this.displayError = function(form, element, display)
	{
		var e = document.getElementById(form + '_' + element + '_error');
		
		if(e == null)
			window.alert(form + '_' + element + '_error');
		
		if(display)		
			e.style.display = 'inline';
		else
			e.style.display = 'none';		
	}
		
	this.zipcodeCheck = function(value)
	{
		var mask = /^([0-9]{4})([a-zA-Z]{2})$/i
		return this.checkMask(value, mask);
	}	
	
	this.emailCheck = function(value)
	{
		var mask = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		return this.checkMask(value, mask);
	}

	this.numberCheck = function(value)
	{
		var mask = /^[0-9]*$/ 
		return this.checkMask(value, mask);
	}	
	
	this.dateCheck = function(value)
	{		
		var parts = value.split('-');
		
		if(parts.length != 3)
			return false;
			
		for(i=0; i<parts.length; i++)
		{
			if(isNaN(parts[i]))
				return false;
		}
			
		if(parts[0] > 31 || parts[0] < 1)
			return false;

		if(parts[1] > 12 || parts[1] < 1)
			return false;			
							
		if(parts[1] == 4 || parts[1] == 6 || parts[1] == 9 || parts[1] == 11)
		{
			if(parts[0] > 30)
				return false;			
		}
		
		if(parts[1] == 2 && parts[0] > 29)
			return false;
			
		return true;
	}

	this.checkGroup = function(group)
	{
		len     = group.length
		checked = false;

		for (i = 0; i <len; i++) 
		{
			if (group[i].checked) 
				checked = true;
		}	
		
		return checked;		
	}
	
	this.checkCheckBoxGroup = function(name)
	{
		var c = document.getElementsByTagName('input');
		var checked = false;
	
		for (var i = 0; i < c.length; i++)
		{
			if (c[i].type == 'checkbox' && c[i].name.substr(0, name.length) == name && c[i].checked)
				checked = true;		
		}
		
		return checked;
	}
	
	
	
	this.checkMask = function(value, mask)
	{
		if (mask.test(value)) 
			return true;
		else 
			return false;		
	}
}

function trim(value)
{
  value = value.replace(/^\s+/,'');
  value = value.replace(/\s+$/,'');
  return value;
}

formchecker = new FormChecker();


/*
Imagegroup functions
*/

ImageGroup = function()
{
	this.controls = Array;
	
	this.selectImage = function(group, id, form)
	{
		for(i=0; i<this.controls[group].length; i++)
		{
			if(id == this.controls[group][i].id)
			{
				$(group + '_' + id).src = this.controls[group][i].image + '_1.png'
				$(group + '_' + this.controls[group][i].id).className = 'image_group_selected';				
			}
			else
			{
				$(group + '_' + this.controls[group][i].id).src = this.controls[group][i].image + '.png'	
				$(group + '_' + this.controls[group][i].id).className = 'image_group';
			}
		}
		
		eval('document.'+form+'.'+group).value = id;		
	}
	
	this.addControl = function(id, group, image)
	{
		if(this.controls[group] == null)
			this.controls[group] = Array();
		
		var control = new Object();
		control.id    = id;
		control.image = image;
		
		this.controls[group].push(control);				
	}
	
	
}

var imagegroup = new ImageGroup();



/*
Password strength
*/

PasswordChecker = function()
{
	
	this.check = function(elem, passwordIndicator)
	{
		var level = 0;
		var password = elem.value;
		
		//Now consider the letters
		if (password.match(/[a-z]/i))
		{
			level++; 
			
			//Mixed case
			if (password.match(/[a-z][A-Z]|[A-Z][a-z]/)) 
				level++;
		} 
		
		
		//Now check for numbers
		count = password.match(/[0-9]/g);
	
		if (count)
			level++

		//no number, no points
		
		
		//Now check for special characters
		count = password.match(/[!,@,#,$,%,^,*,?,_,~,-]/g)
		if (count) 
			level++
		//no special characters, no points	
		
		//calculate strength out of length
		var length = password.length;
		
		if (length > 12) 
			level++;

		//Substract if not longer than 6
		else if (length < 6) 
			level = 0;
		
		this.setIndicator(passwordIndicator, level);
		
		return level;
	}
	
	this.setIndicator = function(elem, level)
	{
		div = document.getElementById(elem);
		
		if(level < 2)
			strVerdict = "heel zwak"
		else if (level == 2)
			strVerdict = "zwak"
		else if (level == 3)
			strVerdict = "redelijk"
		else 
			strVerdict = "sterk"
	
		div.innerHTML = strVerdict;		
	}	
}

PlaceCallFormExtension = function()
{
	this.selectType = function(type)
	{
		$('new_call_car_options').hide();
		$('new_call_ov_options').hide();	
		
		if(type == 1)
		{
			$('create_call_location_label').innerHTML = 'Locatie';
		}
		else if(type == 2)
		{
			$('new_call_car_options').show();
			$('create_call_location_label').innerHTML = 'Locatie';

	
		}
		else if(type == 3)
		{
			$('new_call_ov_options').show();	
			$('create_call_location_label').innerHTML = 'Eindstation';
		}
		else if(type == 4)
		{
			$('new_call_arriva_options').show();	
			$('create_call_location_label').innerHTML = 'Eindstation';
		}		
		
	}	
}

var place_call_form_extension = new PlaceCallFormExtension();
