////////
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = '#EEEEEE';
        error = "Please enter your email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#EEEEEE';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#EEEEEE';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "Please enter your phone number.\n";
        fld.style.background = '#EEEEEE';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = '#EEEEEE';
    } 
    return error;
}

function validateName(fld) {
    var error = "";
 
    if (fld.value.length == 0 || fld.value =='Name...' || fld.value ==' ') {
        fld.style.background = '#EEEEEE'; 
        error = "Please enter your full name.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateService(fld) {
    var error = "";
 
    if (fld.value.length == 0 || fld.value =='') {
        fld.style.background = '#EEEEEE'; 
        error = "Please enter your service.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateNotes(fld) {
    var error = "";
 
    if (fld.value.length == 0 || fld.value =='Notes...'  || fld.value ==' ') {
        fld.style.background = '#EEEEEE'; 
        error = "Please enter some notes.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
////////
function validate_qe_form(theForm) {
var reason = "";

  reason += validateName(theForm.name);
  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.tel);
  reason += validateService(theForm.service);  
  reason += validateNotes(theForm.notes);
      
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }

  return true;
}


$(document).ready(function(){


/////////////////////////////////////////////////// 
/////////////////////////////////////////////////// 
/////////////////////////////////////////////////// DDACORDIAN FIX! 

$('ul.categoryitems>li>a').click(function() { 
	$.cookie("expandableMenu", 'yes' , { path: '/' });

});
$('.arrowlistmenu>ul>li>a').click(function() { 
	$.cookie("expandableMenu", 'no' , { path: '/' });

});

/////////////////////////////////////////////////// 
/////////////////////////////////////////////////// 
/////////////////////////////////////////////////// 


/*
//quickEnquiry

$("#enquiryForm input[name='name']").click(function(){
											
	if($("#enquiryForm input[name='name']").val() == 'Name...'){
	$("#enquiryForm input[name='name']").val('');	
	}
});

$("#enquiryForm input[name='name']").blur(function(){
											
	if($("#enquiryForm input[name='name']").val() == ''){
	$("#enquiryForm input[name='name']").val('Name...');	
	}
});
///// email
$("#enquiryForm input[name='email']").click(function(){
											
	if($("#enquiryForm input[name='email']").val() == 'Email...'){
	$("#enquiryForm input[name='email']").val('');	
	}
});

$("#enquiryForm input[name='email']").blur(function(){
											
	if($("#enquiryForm input[name='email']").val() == ''){
	$("#enquiryForm input[name='email']").val('Email...');	
	}
});
///// tel
$("#enquiryForm input[name='tel']").click(function(){
											
	if($("#enquiryForm input[name='tel']").val() == 'Tel...'){
	$("#enquiryForm input[name='tel']").val('');	
	}
});

$("#enquiryForm input[name='tel']").blur(function(){
											
	if($("#enquiryForm input[name='tel']").val() == ''){
	$("#enquiryForm input[name='tel']").val('Tel...');	
	}
});

///// notes
$("#enquiryForm textarea[name='notes']").click(function(){
											
	if($("#enquiryForm textarea[name='notes']").val() == 'Notes...'){
	$("#enquiryForm textarea[name='notes']").val('');	
	}
});

$("#enquiryForm textarea[name='notes']").blur(function(){
											
	if($("#enquiryForm textarea[name='notes']").val() == ''){
	$("#enquiryForm textarea[name='notes']").val('Notes...');	
	}
});

*/
	
});



ddaccordion.init({
	headerclass: "expandable", //Shared CSS class name of headers group that are expandable
	contentclass: "categoryitems", //Shared CSS class name of contents group
	revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click" or "mouseover
	collapseprev: true, //Collapse previous content (so only one open at any time)? true/false 
	defaultexpanded: [], //index of content(s) open by default [index1, index2, etc]. [] denotes no content
	animatedefault: false, //Should contents open by default be animated into view?
	persiststate: true, //persist state of opened contents within browser session?
	toggleclass: ["", "openheader"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
	togglehtml: ["prefix", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
	animatespeed: "normal", //speed of animation: "fast", "normal", or "slow"
	oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
		//do nothing
	},
	onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
		//do nothing
	}
})