//General js functions

function check_openhouse_times(){

	var start = parseInt(document.getElementById('Start Hour').value + document.getElementById('Start Minute').value);
	var end = parseInt(document.getElementById('End Hour').value + document.getElementById('End Minute').value);
	if(end <= start){
		alert('Your End time must be later than your Start time');
		return false;
	}
	else if(!document.getElementById('date').value){
		alert('You must provide a date');
		return false;
	}
	else{
		return true;
	}

}

function check_search(){

	if(parseInt(document.getElementById('Price Minimum').value) > parseInt(document.getElementById('Price Maximum').value)){
		alert('Minimum Price cannot be greater than Maximum Price');
		return false;
	}
	else{
		return true;
	}

}

//ajax helpers
function updating(elementname){
        document.getElementById(elementname).innerHTML = "<img src='/images/ajax-loader.gif' style='margin-left:2px;'>";
}
function done_message(elementname,message){
        document.getElementById(elementname).innerHTML = "<span class='tabletextMED' style='color:#9E0B0E;'>" + message  + "</span>";
}
function error_message(elementname,message){
        document.getElementById(elementname).innerHTML = "<span class='tabletextMED' style='color:#9E0B0E;'>" + message  + "</span>";
}


//limit chars on a textarea field
function limitText(limitField, limitCount, limitNum) {
        if (limitField.value.length > limitNum) {
            limitField.value = limitField.value.substring(0, limitNum);
        }
    else {
        limitCount.value = limitNum - limitField.value.length;
    }
}

//
function validate_terms_and_conditions(element){
	if(document.myform.agree.checked == false){
	        alert ( "Please check the Terms and Conditions box." );
		document.getElementById(element).style.border = '2px solid #9E0B0E';
		return false;
	}
	else{
		return true;
	}
}


///PARSE FORM FIELDS
/*
usage:
<script language="javascript" src="/lib/parse_form_fields.js"></script>
<form name="myform" action="#" method="post" ONSUBMIT="return parse_form_fields('field1,field2,field3,..');">
hint: make sure all required fields have "id='fieldname' for the getelementbyid attribute to work right
hint: a field's 'id' does NOT have to match the field's 'name'
*/
function parse_form_fields(mycheckfields,extra){
        var checkfields = mycheckfields.split(",");
        var error_message = "";

        for (i=0;i<checkfields.length;i++){
                if(!document.getElementById(checkfields[i]).value){
                        error_message = error_message + "\n- " + checkfields[i];
			document.getElementById(checkfields[i]).style.border = '2px solid #9E0B0E';
                }
        }
        if(error_message==""){
		if(extra == 'signup'){ //validate password and email 
		        if(document.myform.password.value != document.myform.repassword.value){
		                alert ('Your password was not re-typed correctly.');
				document.getElementById('Re-entered Password').style.border = '2px solid #9E0B0E';
				document.getElementById('Password').style.border = '2px solid #9E0B0E';
		                return false;
		        }
		        else{
				var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				if(reg.test(document.myform.email.value) == false) {
					alert('Your email address is not valid');
					document.getElementById('Email Address').style.border = '2px solid #9E0B0E';
					return false;
				}
				else{
			                return true;
				}
		        }
		}
		else if(extra == 'seller_view'){ //validate password and email 
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(reg.test(document.myform.sender_email.value) == false) {
				alert('Your email address is not valid');
				document.getElementById('Your Email Address').style.border = '2px solid #9E0B0E';
				return false;
			}
			else{
		                return true;
			}
		}
		else if(extra == 'email'){ //validate password and email 
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(reg.test(document.myform.email.value) == false) {
				alert('Your email address is not valid');
				document.getElementById('Your Email Address').style.border = '2px solid #9E0B0E';
				return false;
			}
			else{
		                return true;
			}
		}
		else if(extra == 'email_login'){ //validate password and email 
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(reg.test(document.myform.email.value) == false) {
				alert('Your email address is not valid');
				document.getElementById('Email Address').style.border = '2px solid #9E0B0E';
				return false;
			}
			else{
		                return true;
			}
		}
		else if(extra == 'getpass'){ //validate email 
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(reg.test(document.getpass.email.value) == false) {
				alert('Your email address is not valid');
				document.getElementById('Your Email Address').style.border = '2px solid #9E0B0E';
				return false;
			}
			else{
		                return true;
			}
		}
		else if(extra == 'email_listing'){ //validate email addresses
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if(reg.test(document.myform.email_to.value) == false) {
				alert('The email address you\'re sending to is not valid');
				document.getElementById('Email this listing to').style.border = '2px solid #9E0B0E';
				return false;
			}
			else{
		                return true;
			}
		}
		else if(extra == 'ads'){
			if(/\d/.test(document.myform.price.value) == false) {
				alert('The Price must be in number format');
				document.getElementById('Price').style.border = '2px solid #9E0B0E';
				return false;
			}
			else if(document.myform.video_embed_code.value){
	        		if(/\<embed/.test(document.myform.video_embed_code.value) == false) {
					alert('Your Video Embed Code is not valid');
					document.getElementById('Video Embed Code').style.border = '2px solid #9E0B0E';
					return false;
				}
				else{
			                return true;
				}
			}
			else{
				return true;
			}
		}
                else if(extra == 'terms_and_conditions'){ //make sure they agree to terms & conditions
			if(document.myform.agree.checked == false){
			        alert ( "Please check the Terms and Conditions box." );
				document.getElementById('I Agree').style.border = '2px solid #9E0B0E';
				return false;
			}
			else{
				return true;
			}
		}
		else{
                	return true;
		}
        }
        else{
                alert ('Please make sure you filled out the following required fields: ' + error_message);
                return false;
        }
}

