// marrymt.js
var clicked = false;
var pclicked = false;

function validateForm(){
	if (addVendor.name.value == undefined || trim(addVendor.name.value) == ''){
		alert('Your Name is a required field');
		addVendor.name.focus();
		return false;
	}
	
	if (addVendor.email.value == undefined || trim(addVendor.email.value) == ''){
		alert('Your Email is a required field');
		addVendor.email.focus();
		return false;
	} else {
		if (addVendor.email.value.indexOf('@') == -1) {
			alert('You entered an invalid email address');
			addVendor.email.focus();
			return false;
		}
		if (addVendor.email.value.indexOf('.') == -1) {
			alert('You entered an invalid email address');
			addVendor.email.focus();
			return false;
		}
	}
		
	if (addVendor.vwebsite.value != undefined && trim(addVendor.vwebsite.value) != '') {
		var websitevar = addVendor.vwebsite.value;
		if (websitevar.indexOf('http://') < 0) {
			alert('The website field must begin with http:// or be left blank');
			addVendor.vwebsite.focus();
			return false;
		}
	}
	
	if (addVendor.vname.value == undefined || trim(addVendor.vname.value) == ''){
		alert('The vendor name is a required field');
		addVendor.vname.focus();
		return false;
	}
	
	if (addVendor.capacity.value == undefined || trim(addVendor.capacity.value) == '') {
		alert('The capacity field is a required field.  If you are not a venue enter 0');
		addVendor.capacity.focus();
		return false;
	}
	
	return true;
} 

function validateComments(){
	if (commentsForm.name.value == undefined || trim(commentsForm.name.value) == ''){
		alert('Your Name is a required field');
		commentsForm.name.focus();
		return false;
	}
	
	if (commentsForm.email.value == undefined || trim(commentsForm.email.value) == ''){
		alert('Your Email is a required field');
		commentsForm.email.focus();
		return false;
	} else {
		if (commentsForm.email.value.indexOf('@') == -1) {
			alert('You entered an invalid email address');
			commentsForm.email.focus();
			return false;
		}
		if (commentsForm.email.value.indexOf('.') == -1) {
			alert('You entered an invalid email address');
			commentsForm.email.focus();
			return false;
		}
	}
	
	var qRating = document.getElementById('qualityRating');
		
	if (qRating.value == undefined || qRating.value == '') {
		alert('You must select a quality rating');
		return false;
	}
	
	var pRating = document.getElementById('priceRating');
	if (qRating.value == undefined || pRating.value == '') {
		alert('Please enter a price rating');
		return false;
	}

	if (qRating != undefined && qRating.value < 3) {
		if (commentsForm.comments.value == undefined || commentsForm.comments.value == ''){
			alert('When leaving this quality rating you MUST also enter some comments');
			return false;
		}
	} 
	
	if (commentsForm.comments.value == undefined || trim(commentsForm.comments.value) == ''){
			var answer = confirm('Are you sure you want to submit your comment with no comment details?');
			return answer;
	}
	
	return true;
}

function showAddVendorInfo(query){
   	var strURL = 'addVendor-venue.jsp?' + query;
    var xmlHttpReq = false;
    
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
;
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
        	var divElement = document.getElementById('venueInfo');
            divElement.innerHTML = self.xmlHttpReq.responseText;
            displayLoadingMsg(false,loadingMsgId);
        }
    }
    
    self.xmlHttpReq.send('');
}

function showValueClicked(value, element, star){

	if (value == 1){
		if (star) {
		document.star0.src='images/starFill.gif';
		document.star1.src='images/star.gif';
		document.star2.src='images/star.gif';
		document.star3.src='images/star.gif';
		document.star4.src='images/star.gif';
		clicked = true;
		} else {
		document.money0.src='images/moneyFill.gif';
		document.money1.src='images/money.gif';
		document.money2.src='images/money.gif';
		document.money3.src='images/money.gif';
		document.money4.src='images/money.gif';
		document.money5.src='images/money.gif';
		pclicked = true;
		}
	} else if (value == 2) {
		if (star) {
		document.star0.src='images/starFill.gif';
		document.star1.src='images/starFill.gif';
		document.star2.src='images/star.gif';
		document.star3.src='images/star.gif';
		document.star4.src='images/star.gif';
		clicked = true;
		} else {
			document.money0.src='images/moneyFill.gif';
			document.money1.src='images/moneyFill.gif';
			document.money2.src='images/money.gif';
			document.money3.src='images/money.gif';
			document.money4.src='images/money.gif';
			document.money5.src='images/money.gif';
			pclicked = true;
		}
	} else if (value == 3) {
		if (star) {
		document.star0.src='images/starFill.gif';
		document.star1.src='images/starFill.gif';
		document.star2.src='images/starFill.gif';
		document.star3.src='images/star.gif';
		document.star4.src='images/star.gif';
		clicked = true;
		} else {
			document.money0.src='images/moneyFill.gif';
			document.money1.src='images/moneyFill.gif';
			document.money2.src='images/moneyFill.gif';
			document.money3.src='images/money.gif';
			document.money4.src='images/money.gif';
			document.money5.src='images/money.gif';
			pclicked = true;
		}
	} else if (value == 4) {
		if (star) {
		document.star0.src='images/starFill.gif';
		document.star1.src='images/starFill.gif';
		document.star2.src='images/starFill.gif';
		document.star3.src='images/starFill.gif';
		document.star4.src='images/star.gif';
		clicked = true;
		} else {
			document.money0.src='images/moneyFill.gif';
			document.money1.src='images/moneyFill.gif';
			document.money2.src='images/moneyFill.gif';
			document.money3.src='images/moneyFill.gif';
			document.money4.src='images/money.gif';
			document.money5.src='images/money.gif';
			pclicked = true;
		}
	}else if (value == 5) {
		if (star) {
		document.star0.src='images/starFill.gif';
		document.star1.src='images/starFill.gif';
		document.star2.src='images/starFill.gif';
		document.star3.src='images/starFill.gif';
		document.star4.src='images/starFill.gif';
		clicked = true;
		} else {
			document.money0.src='images/moneyFill.gif';
			document.money1.src='images/moneyFill.gif';
			document.money2.src='images/moneyFill.gif';
			document.money3.src='images/moneyFill.gif';
			document.money4.src='images/moneyFill.gif';
			document.money5.src='images/money.gif';
			pclicked = true;
		}
	} else if (value == 0) {
		if (!star) {
			document.money0.src='images/money.gif';
			document.money1.src='images/money.gif';
			document.money2.src='images/money.gif';
			document.money3.src='images/money.gif';
			document.money4.src='images/money.gif';
			document.money5.src='images/moneyFill.gif';
			pclicked = true;
		}
	}
	
	var divElement = document.getElementById(element);
	divElement.value = value;
}

//Change the stars
function changeStars(vendorId, blue){
	var star0 = document.getElementById('star0' + vendorId);
	var star1 = document.getElementById('star1' + vendorId);
	var star2 = document.getElementById('star2' + vendorId);
	var star3 = document.getElementById('star3' + vendorId);
	var star4 = document.getElementById('star4' + vendorId);
	 		
	var old0Src = star0.src;
	var old1Src = star1.src;
	var old2Src = star2.src;
	var old3Src = star3.src;
	var old4Src = star4.src;
	
	var star2use;
	var old2use;
	
	for (i = 0; i<5; i++){
		if (i==0) {
			star2use = star0;
			old2use = old0Src;
		} else if (i==1) {
			star2use = star1;
			old2use = old1Src;
		} else if (i==2) {
			star2use = star2;
			old2use = old2Src;
		} else if (i==3) {
			star2use = star3;
			old2use = old3Src;
		} else if (i==4) {
			star2use = star4;
			old2use = old4Src;
		}
		if (old2use != undefined) {
			if (old2use.indexOf('half') > -1) {
				//we have half a star
				if (blue) {
					star2use.src = 'images/halfstarblue.gif';
				} else {
					star2use.src = 'images/halfstar.gif';
				}
			} else if (old2use.indexOf('Fill') > -1) {
				//we have a filled star
				if (blue) {
					star2use.src = 'images/starFillblue.gif';
				} else {
					star2use.src = 'images/starFill.gif';
				}
			} else {
				//we have an empty star
				if (blue) {
					star2use.src = 'images/starblue.gif';
				} else {
					star2use.src = 'images/star.gif';
				}
			}
		} 
	}
	
}

//methods uses AJAX to add information about MarryMontana Vendors
function addMarryMTVendors(show){
	var strURL = 'engagement-marrymt.jsp?mt=' + show;
    var xmlHttpReq = false;
    
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
;
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
        	var divElement = document.getElementById('marrymt');
            divElement.innerHTML = self.xmlHttpReq.responseText;
           // displayLoadingMsg(false,loadingMsgId);
        }
    }
    
    self.xmlHttpReq.send('');
}

function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

// ************************* 
// Method to validate engagement announcement
// ***********************************************
function validateEngagement(){

	if (anForm.bride.value == undefined || trim(anForm.bride.value) == ''){
		alert('The brides Name is a required field');
		anForm.bride.focus();
		return false;
	}
	
	if (anForm.groom.value == undefined || trim(anForm.groom.value) == ''){
		alert('The grooms Name is a required field');
		anForm.groom.focus();
		return false;
	}
	
	if (anForm.bparents.value == undefined || trim(anForm.bparents.value) == ''){
		alert('The brides parents are a required field');
		anForm.bparents.focus();
		return false;
	}
	
	if (anForm.gparents.value == undefined || trim(anForm.gparents.value) == ''){
		alert('The grooms parents are a required field');
		anForm.gparents.focus();
		return false;
	}
	
	if (anForm.email.value == undefined || trim(anForm.email.value) == ''){
		alert('Your Email is a required field');
		anForm.email.focus();
		return false;
	} else {
		if (anForm.email.value.indexOf('@') == -1) {
			alert('You entered an invalid email address');
			anForm.email.focus();
			return false;
		}
		if (anForm.email.value.indexOf('.') == -1) {
			alert('You entered an invalid email address');
			anForm.email.focus();
			return false;
		}
	}
		
	if (anForm.website.value != undefined && trim(anForm.website.value) != '') {
		var websitevar = anForm.website.value;
		if (websitevar.indexOf('http://') < 0) {
			alert('The website field must begin with http:// or be left blank');
			anForm.website.focus();
			return false;
		}
	}
	
	if (anForm.city.value == undefined || trim(anForm.city.value) == ''){
		alert('City is a required field');
		anForm.city.focus();
		return false;
	}
	
	/*var dateVal = anForm.date.value;
	if (anForm.date == undefined || trim(anForm.date.value) == '') {
		alert('Date is a required field.  If you are unsure enter TBD');
		anForm.date.value = 'TBD';
		anForm.date.focus();
		return false;
	} else if (dateVal != 'TBD' && dateVal.indexOf('-') == -1) {
		alert('Date is a required field and must be formatted like this: MM-DD-YYYY.  If you are unsure of your wedding date enter TBD');
		anForm.date.value = 'TBD';
		anForm.date.focus();
		return false;
	} else if (dateVal.indexOf('Y') != -1){
		alert('Date is a required field.  If you are unsure enter TBD');
		anForm.date.value = 'TBD';
		anForm.date.focus();
		return false;
	}*/

	
	return true;
} 

function clearvalue(){
	var modifiedAfter = anForm.date.value;
	
	if (modifiedAfter != undefined && modifiedAfter == 'MM-DD-YYYY') {
		anForm.date.value = '';
	}
}

function setDate() {
	anForm.date.value = 'MM-DD-YYYY';
}