// JavaScript Document
function changePhoto(str, alt, id, count) {
	// change all to gray
	for($i=1; $i<=count*3; $i++) {
		if(document.getElementById('squarePhoto'+$i)) {
			document.getElementById('squarePhoto'+$i).src = 'images/gray18x18.jpg';
		}		
	}	
	// set current is blue
	document.getElementById(id).src = 'images/yellow18x18.jpg';
	document.getElementById('photo').src = 'images/web/enlarged/'+str;	
	document.getElementById('photo').alt = alt;	
	document.getElementById('photo').title = alt;	
}



function isValidEmail(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);  
	return pattern.test(emailAddress);
  
}


$(document).ready(function(){
	
	//validate request-a-conversation.php	
	$("#btnSendWarranty").click(function() {
		
		var txt = '';							
			if($('#name').val() == ''){
                txt += 'Please input Your Name. \n';
            }
			if($('#address').val() == ''){
                txt += 'Please input Street Address. \n';
            }	
			if($('#city').val() == ''){
                txt += 'Please input City. \n';
            }	
			if($('#state').val() == ''){
                txt += 'Please input State. \n';
            }
			if($('#zip').val() == ''){
                txt += 'Please input Zip Code. \n';
            }
			if($('#homePhone').val() == ''){
                txt += 'Please input Home Phone. \n';            } 
			
			if($('#email').val() == '' || !isValidEmail($('#email').val())){
                txt += 'Please input Email Address. \n';
            }				
			if($('#recaptcha_response_field').val() == ''){
                txt += 'Please input Security Code. \n';
            } 
			
			if(txt != '') {
                alert(txt);
                return false;
            }
            
            return true;				
    });	// end validate
						   
						   
						   
	
	//validate request-a-conversation.php	
	$("#btnSendConversation").click(function() {
		
		var txt = '';							
			if($('#firstName').val() == ''){
                txt += 'Please input First Name. \n';
            } 
			if($('#lastName').val() == ''){
                txt += 'Please input Last Name. \n';
            } 			
			
			if($('#email').val() == '' || !isValidEmail($('#email').val())){
                txt += 'Please input Email Address. \n';
            }
			if($('#phone').val() == ''){
                txt += 'Please input Phone Number. \n';
            } 
			
			if ($("input[@name='commercialORresidential']:checked").val() == 'Commercial') {
				// Code for handling value 'Commercial'
				if($('#companyName').val() == ''){
                	txt += 'Please input Company Name. \n';
            	} 
				if($('#title').val() == ''){
               	 	txt += 'Please input Title. \n';
            	} 				
			}

			
			//if(!$('input[type="checkbox"]').is(':checked')){
				//txt += 'Please check at least one \"Type of Project \". \n';			 	
			//}			
				
			if($('#recaptcha_response_field').val() == ''){
                txt += 'Please input Security Code. \n';
            } 
			
			if(txt != '') {
                alert(txt);
                return false;
            }
            
            return true;				
    });	// end validate
	
	
	
	//validate quick-contact	
	$("#btnSUBMITcontact").click(function() {		
		var txt = '';							
			if($('#contactNAME').val() == 'Your name:'){
                txt += 'Please input you name. \n';
            } 
			if($('#contactPHONE').val() == 'Phone number:'){
                txt += 'Please input your phone. \n';
            } 			
			
			if($('#contactEMAIL').val() == 'Email address:' || !isValidEmail($('#contactEMAIL').val())){
                txt += 'Please input email address. \n';
            }
			if($('#contactZIP').val() == 'Zip Code:'){
                txt += 'Please input zip code. \n';
            } 
			if($('#contactBRANCH').val() == 'none'){
                txt += 'Please select closest branch. \n';
            } 		
				
			if($('#contactCAPTCHA').val() == 'Write the following word:'){
                txt += 'Please enter the CAPTCHA code as pictured \n';
            } 
			
			if(txt != '') {
                alert(txt);
                return false;
            } 
            
            return true;				
    });	// end validate
	
	
});


//quick contact validation -- matt
function formValidator(){

	var name1 = document.getElementById('contactNAME');
	var phone = document.getElementById('contactPHONE');
	var email = document.getElementById('contactEMAIL');
	var zip = document.getElementById('contactZIP');
	var branch = document.getElementById('contactBRANCH');
	var message = document.getElementById('contactMESSAGE');
	var captcha = document.getElementById('captcha-form');

	
	

						if(notEmpty(name1, "Please enter your name")){
								if(notEmpty(phone,"Please enter your phone number")){
									if(isNumeric(phone, "Please enter only numbers for your phone number")){
										if(notEmpty(email, "Please enter your email address")){
											if(emailValidator(email, "Please enter a valid email address")){
												if(notEmpty(zip, "Please enter a zip code")){
													if(isNumeric(zip, "Please enter only numbers for your zip code")){
														if(madeSelection(branch, "none",  "Please select closest branch")){
																if(notEmptyCaptcha(captcha, "Write the following word:", "Please enter the CAPTCHA code as pictured")){

																	return true;
														}
													}
												}
											}
										}
									}
								}
							}
						}
					
					
					
		
	
	
	return false;
	
}

function notEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function notEmptyCaptcha(elem, value, helperMsg){
	if(elem.value.length == 0 || elem.value == value){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9-]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function madeSelection(elem, value, helperMsg){
	if(elem.value == value){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
}
