function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function validateLogin() {
	var username = document.login_form.username.value;
	var password = document.login_form.password.value;
	
	if (username == "") {
		alert("You must enter a username.");
		document.login_form.username.focus();
		return false;
	}
	if (password == "") {
		alert("You must enter a password.");
		document.login_form.password.focus();
		return false;
	}
	
	encodePassword(password);
}

function validateEmail(email) {
	// E-mail Validation by Henrik Petersen / NetKontoret
	// Explained at www.echoecho.com/jsforms.htm
	// Please do not remove this line and the two lines above.
	apos=email.indexOf("@"); 
	dotpos=email.lastIndexOf(".");
	lastpos=email.length-1;
	if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
		return false;
	} else {
		return true;
	}
}

function encodePassword(password) {
    var hash = hex_md5(password);
	document.login_form.password.value = hash;
	return true;
}

function cancelAdmin(section) {
	document.forms[0].action = baseUrl+"/"+section;
	document.forms[0].submit();	
}

function replaceChars(entry) {
	out = "\\"; // replace this
	add = "/";  // with this
	temp = "" + entry; // temporary holder

	while (temp.indexOf(out)>-1) {
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + 
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function isNumeric(strString) {
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}

function isPhone(strString) {
	var strValidChars = "0123456789()+- ";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}

function confirmDelete(path) {
	if (confirm("Are you sure want to delete this entry from the database?")) {
		window.location = baseUrl+path;
	}
}


function addLeadNotes(type) {
	new Ajax.Updater('notes_div', baseUrl+'/lead/ajaxAddNotes', { 
        method:'post', postBody:'lead_id='+$F('lead_id')+'&lead_notes='+$F('lead_notes'), 
        onComplete: function() {
            document.getElementById('lead_notes').value = "";
        }
    });
}

function addAction(type) {
	document.edit_form.action = baseUrl+"/"+type+"/addAction";
	document.edit_form.submit();	
}

function resetLeadFilter() {
    document.filter_form.product_id.value = -1;
    document.filter_form.status_id.value = -1;
    document.filter_form.owner_id.value = -1;
    document.filter_form.source_id.value = -1;
    document.filter_form.priority_id.value = -1;
    document.filter_form.submit();
}

function showNewSource(sourceId) {
    if (sourceId == '0') {
        document.getElementById('new_source_div').style.display = 'block';
        document.getElementById('new_source').focus();
    } else {
        document.getElementById('new_source_div').style.display = 'none';
    }
}

function toggleContactDate() {
    toggleField = document.getElementById('toggle_contact_date');

    if (toggleField.checked) {
        showBlock('contact_date_div');
    } else {
        hideBlock('contact_date_div');
    }
}

function toggleLastContactDate() {
    toggleField = document.getElementById('toggle_last_contact_date');

    if (toggleField.checked) {
        showBlock('last_contact_date_div');
    } else {
        hideBlock('last_contact_date_div');
    }
}

function toggleFollowupDate() {
    toggleField = document.getElementById('toggle_followup_date');

    if (toggleField.checked) {
        showBlock('followup_date_div');
    } else {
        hideBlock('followup_date_div');
    }
}

function hideBlock(divId) {
	document.getElementById(divId).style.display = "none";
}

function showBlock(divId) {
	document.getElementById(divId).style.display = "block";
}

function validateProductForm() {
    productName = document.getElementById('product_name');
    if (productName.value == "") {
    	alert ('You must provide a product name.');
    	productName.focus();
    	return false;
    }
    
    var priceType = document.edit_form.price_type;
    for (i=0; i<priceType.length; i++) {
    	if (priceType[i].checked) {
    		priceTypeSelection = priceType[i].value;
    	}
    }
    price = document.getElementById('price');
    if (priceTypeSelection == 1 && price.value == "") {
    	alert ('You must provide a fixed price.');
    	return false;
    }
    
    return true;
}

function finishUpdate() {
    document.edit_form.complete.value = "true";
    document.edit_form.submit();
}

function toggleNewContact(state) {
    if (state == 1) {
        showBlock('hide_new_contact_div');
        hideBlock('show_new_contact_div');
        
        showBlock('new_contact_div');
        document.getElementById('contact_name').focus();
        
        document.getElementById('show_new_contact').value = "Y";
    } else {
        showBlock('show_new_contact_div');
        hideBlock('hide_new_contact_div');
        
        hideBlock('new_contact_div');
        
        document.getElementById('show_new_contact').value = "N";
    }
}

function toggleContacts(state) {
    if (state == 1) {
        showBlock('hide_contacts_div');
        hideBlock('show_contacts_div');
        
        showBlock('contacts_div');
        
        document.getElementById('show_contacts').value = "Y";
    } else {
        showBlock('show_contacts_div');
        hideBlock('hide_contacts_div');
        
        hideBlock('contacts_div');
        
        document.getElementById('show_contacts').value = "N";
    }
}

function addNewContact() {
    var contactName = document.getElementById('contact_name').value;
	var phone = document.getElementById('contact_phone').value;
	var mobile = document.getElementById('contact_mobile').value;
	var email = document.getElementById('contact_email').value;
	if (contactName.value == "") {
		alert("You must enter a contact name.");
		document.getElementById('contact_name').focus();
		return;
	}
	if (phone != "" && !isPhone(phone)) {
		alert("Your phone number contains invalid characters");
		document.getElementById('contact_phone').focus();
		return;
	}
	if (mobile != "" && !isPhone(mobile)) {
		alert("Your mobile number contains invalid characters");
		document.getElementById('contact_mobile').focus();
		return;
	}
	if (email != "" && !validateEmail(email)) {	    
		alert("You must enter a valid email address");
		document.getElementById('contact_email').focus();
		return;
	}
	
	document.getElementById('add_contact').value = "Y";
	document.edit_form.submit();
}

function validateUserForm() {
	var username = document.getElementById('username');
	var password = document.getElementById('password');
	var verifyPassword = document.getElementById('verify_password');
	
	if (username.value == "") {
		alert("You must enter either a username.");
		username.focus();
		return false;
	}
	if (password.value == "") {
	    alert("You must provide a password.");
	    password.focus();
		return false;
	}
	if (verifyPassword.value == "") {
	    alert("You must retype the password.");
	    verifyPassword.focus();
		return false;
	}
	if (verifyPassword.value != password.value) {
	    alert("Your passwords do not match - please retype.");
	    verifyPassword.select();
		return false;
	}
	
	return true;
}

function validateUpdateUserForm() {
	var password = document.getElementById('password');
	var verifyPassword = document.getElementById('verify_password');
	
	if (password.value != "") {
    	if (verifyPassword.value == "") {
    	    alert("You must retype the password.");
    	    verifyPassword.focus();
    		return false;
    	}
    	if (verifyPassword.value != password.value) {
    	    alert("Your passwords do not match - please retype.");
    	    verifyPassword.select();
    		return false;
    	}
	}
	return true;
}

function adminForm(type) {
    document.filter_form.action = baseUrl+"/broker/add"+type;
	document.filter_form.submit();	
}

function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) {
		return btn[cnt].value;
	}  else {
		return null;
	}
}

function showNewCategory(categoryId) {
    if (categoryId == '0') {
        showBlock('new_category_div');
        document.getElementById('new_category').focus();
    } else {
        hideBlock('new_category_div');
    }
}

function downloadBrochure(downloadId) {
	  newWindow = window.open(baseUrl+"/home/download/"+downloadId, 'newWindow', 'toolbar=no, location=no, scrollbars=no, resizable=yes, width=420, height=300, left=150, top=150');
}

function validateDownloadForm() {
	firstName = document.getElementById('first_name');
	surname = document.getElementById('surname');
	email = document.getElementById('email');
	
	if (email.value == "") {
		alert("You must enter your email address");
		email.focus();
		return false;
	}

	if (!validateEmail(email.value)) {
		alert("You must enter a valid email address");
	    email.focus();
		return false;
	}
	return true;
}

function validateContactForm() {
	var fullName = document.getElementById('full_name');
	var categorySelection = document.getElementById('category_id').selectedIndex;
    var categoryId = document.getElementById('category_id').options[categorySelection].value;
	var email = document.getElementById('email');
	var phone = document.getElementById('phone');
	
	if (fullName.value == "") {
		alert("You must enter your full name.");
		fullName.focus();
		return false;
	}
	if (categoryId == 0) {
	    var newCategory = document.getElementById('new_category');
	    if (newCategory.value == "") {
	        alert("You must enter the new sector name.");
    		newCategory.focus();
    		return false;
	    }
	}
	if (email.value == "" && phone.value == "") {
		alert("You must enter either a phone number or email address.");
		email.focus();
		return false;
	}
	if (email.value != "") {
		if (!validateEmail(email.value)) {
    		alert("You must enter a valid email address");
    	    email.focus();
    		return false;
    	}
	}
	if (phone.value != "" && !isPhone(phone.value)) {
		alert("The phone number contains invalid characters.");
		email.focus();
		return false;
	}
	return true;
}

function validateContactForm() {
	var fullName = document.getElementById('full_name');
	var email = document.getElementById('email');
	var phone = document.getElementById('phone');
	
	if (fullName.value == "") {
		alert("You must enter your full name.");
		fullName.focus();
		return false;
	}
	if (email.value == "" && phone.value == "") {
		alert("You must enter either a phone number or email address.");
		email.focus();
		return false;
	}
	if (email.value != "") {
		if (!validateEmail(email.value)) {
    		alert("You must enter a valid email address");
    	    email.focus();
    		return false;
    	}
	}
	if (phone.value != "" && !isPhone(phone.value)) {
		alert("The phone number contains invalid characters.");
		email.focus();
		return false;
	}
	return true;
}
