var personId = -1;
var dealerId = -1;

/** Show/Hide the list displaying available roles */
function showHideRoles(){
	the_block = document.getElementById('roleList');
	visible = the_block.style.visibility == 'visible';
	if(visible){
		the_block.style.visibility = 'hidden';	
	} else {
		the_block.style.visibility = 'visible';	
	}
}

// Substitute
function showAvailableRoles(){
	showHideRoles();	
}


/**
** @abstract Remove a role from the DOM and the hidden_values
*/

function removeRole(role_id, person_id){
    hidden_roles = document.getElementById('p_roles');
	roles = document.getElementById('currentRoles');
	role = document.getElementById('currentRole_'+role_id);
	// Get element and remove it from parent element
	if(roles != null && role!= null){
		roles.removeChild(role);
		var values_txt = hidden_roles.value;
		hidden_roles.value = ''; // Reset the text-field
		var values = values_txt.split(';');
		for(var i=0;i<values.length;i++){
			if(values[i] != '' && values[i] != role_id){
				hidden_roles.value += values[i]+';';
			}
		}
	} else {
		alert('Cannot remove role!');
	}
	
	// Send request to server
}
/**
** @abstract Add a role to the DOM and the hidden_values
*/
function addRole(role_id, person_id, text){
	roles = document.getElementById('currentRoles');
    hidden_roles = document.getElementById('p_roles');
	if(roles == null){
		alert('Nowhere to put role!');
	} else {
		li = document.getElementById('currentRole_'+role_id);
		if(li == null){
			var li = document.createElement('li');
			var txt = document.createTextNode(text);
			var link = document.createElement('a');
			link.href = "#";
			link.innerHTML = '<img src="images/delete.png" alt="Fjern rolle!" onclick="removeRole(\''+role_id+'\',\''+person_id+'\'); return false;"/>';
			li.id = "currentRole_"+role_id;
			li.appendChild(txt);	
			li.appendChild(link);								
			roles.appendChild(li);
			hidden_roles.value += role_id+';'
		}
	}
	
}
/** Reflects changes in baseField to otherField
 ** Put the action on e.g onkeyup 
 */
function reflectChanges(baseField, otherField){
	otherField.value = baseField.value;	
}

/** Popup dealerbox */
function assignDealer(personID){
	showHide('AutoComplete');
	personId = personID;
}
/** Removes the DOM MODEL FOR THE CRM - Deletes the entire record  */
function deleteCRM(personId){  
		removeDOM('CRM_'+personId);      
}

// Add a CRM to a person (Assign dealer to a customer)
function addCRM(personId){
	
}

function setCatalogCounter(number){
    block = document.getElementById('selectedAmount');
    block2 = document.getElementById('maxAmount');
    
    var currentValue = parseInt(block.innerHTML);
    var maxAmount = parseInt(block2.innerHTML);
    var newValue = currentValue + number
    if(newValue >= 0 && newValue <= maxAmount){
	    block.innerHTML = newValue;
	}
    
}
function removeDOM(childId){
		block = document.getElementById(childId);
		block.parentNode.removeChild(block);
}

function validateFormFieldsForPerson(){
	fieldnames = new Array("firstname", "lastname", "addressline1", "postcode", "postoffice", "telephone");
	for(i = 0; i < fieldnames.length;i++){
		name = fieldnames[i];

		field = document.getElementById(name);
		if(field == null){
			return false;
		} else if(theFieldIsEmpty(field)){
			alert("Full ut din kontaktinformation");
			field.className="error";
			field.focus();			
			return false;
		} else {
			field.className=null;
		}
	}	
	errorMessage = "";
	
}

function theFieldIsEmpty(field){
	if(field.value.length<1){
		return true;
	} else {
		if(field.value == ' '){
			return true;
		}
	return false;
	}
}
