var xmlHttp;

var tab="";

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		//Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

if(document.images){
	popup=new Image();
	popup.src="images/popup.jpg";

	button = new Image();
	button.src= "images/button_raised.png";
	button.src = "images/button_depressed.png";
	button.src="images/button_raised_small.png";
	button.src = "images/button_depressed_small.png";
	button.src = "images/message_button_raised.png";
	button.src = "images/message_button_depressed.png";
	button.src = "images/square_button_raised.png";
	button.src = "images/square_button_depressed.png";
	button.src = "images/square_button_raised_small.png";
	button.src = "images/square_button_depressed_small.png";
}

function submitComment(){
	var comment = encodeURIComponent(document.getElementById('comment').value);
	var wine = document.getElementById('hidden_comment_value').value;
	var isPublic = document.getElementById('public').checked;
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		return true;
	}
	
	var url = "addComment.php";
	var params = "comment="+comment+"&wine="+wine+"&public="+isPublic;
	
	xmlHttp.open("POST", url, true);
	
	xmlHttp.onreadystatechange=add_comment_stateChanged ;
	
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	
	xmlHttp.send(params);
	
	return false;
}

function add_comment_stateChanged(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		//        alert(xmlHttp.responseText);
		document.getElementById("comment").value="";
		getComments(document.getElementById('hidden_comment_value').value);
	}
}

function commentStateChanged(){
	if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
		document.getElementById('comments').innerHTML = xmlHttp.responseText;
	}
}

function getComments(wine){
	document.getElementById('comments').innerHtml="";
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		return false;
	}
	var url = "getComments.php";
	url=url+'?wineValue='+wine;
	xmlHttp.onreadystatechange=commentStateChanged ;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return true;
}

function deleteComment(id){
	xmlHttp = GetXmlHttpObject();
	if(xmlHttp==null){
		return false;
	}
	var url = "deleteComments.php";
	url = url+"?id="+id;
	xmlHttp.onreadystatechange=delete_comment_stateChanged ;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return true;
}

function delete_comment_stateChanged(){
	if (xmlHttp.readyState==4 && xmlHttp.status==200){
		getComments(document.getElementById('hidden_comment_value').value);
	}
}

function editComment(comment_id, id){
	var comment = document.getElementById(id);
	var current = comment.innerHTML;
	comment.innerHTML="<textarea cols='50' rows='2' id='changed comment'>"+current+"</textarea><br/><input type='checkbox' id='changed_public'/>Public?<br/><input type='button' onclick='updateComment("+comment_id+")' value='submit'/>";
}

function updateComment(id){
	var comment = document.getElementById("changed comment").value;
	var status = document.getElementById("changed_public");
	if(status.checked){
		status = "true";
	}else{
		status = "false";
	}
	var url = "updateComment.php";
	url = url+"?id="+id+"&comment="+comment+"&public="+status;

	if(!xmlHttp) xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		return false;
	}

	xmlHttp.onreadystatechange=updateComment_stateChanged ;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
	return true;
}

function updateComment_stateChanged(){
	if(xmlHttp.readyState == 4 && xmlHttp.status==200){
		getComments(document.getElementById("hidden_comment_value").value);
	}
}

function showAlert(text){
	document.getElementById("faded").style.display="block";
	var alert = document.getElementById("alert");
	alert.innerHTML="<div class='alert-message'>"+text+"</div><center><a href='#' style='text-decoration:none;' onclick='javascript:hideAlert();'><div class='message_button'>close</div></a></center>";
	alert.style.display="block";
}

function hideAlert(){
	document.getElementById("alert").style.display="none";
	document.getElementById("faded").style.display="none";
}

function showFaq(text){
	document.getElementById("faded").style.display="block";
	var faq = document.getElementById("faq");
	faq.innerHTML = "<div class='faq-message'>"+text+"</div><center><a href='#' style='text-decoration:none;' onclick='javascript:hideFaq();'><div class='message_button'>close</div></a></center>";
	faq.style.display="block";
}

function hideFaq(){
	document.getElementById("faq").style.display="none";
	document.getElementById("faded").style.display="none";
}

function setTab(id){
	document.getElementById(id).style.fontWeight = 'bold'
}

function verifyWine(id){
	var url="getWineForm.php";
	url = url+"?id="+id;

	if(!xmlHttp) xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		return false;
	}

	xmlHttp.onreadystatechange=verifyWine_stateChanged ;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
	return true;
}

function verifyWine_stateChanged(){
	if(xmlHttp.readyState == 4 && xmlHttp.status==200){
		var form = xmlHttp.responseText;
		document.getElementById('wine-form').innerHTML = form;
		document.getElementById('popup_wine_info').style.display="block";
	}
}

function saveWine(id){
	var vineyard = document.wine_info_form.vineyard.value;
	var label = document.wine_info_form.label.value;
	var type = document.wine_info_form.type.value;
	var vintage = document.wine_info_form.vintage.value;

	if(vineyard == "" || label == "" || type == "" || vintage == ""){
		alert("Vineyard, label, type and vintage are required.");
		return false;
	}

	var price = document.wine_info_form.price.value;
	var state = document.wine_info_form.state.value;
	var country = document.wine_info_form.country.value;
	var website = document.wine_info_form.website.value;

	if(!xmlHttp) xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		return false;
	}

	var url = "updateWine.php";
	var params = "id="+id+"&vineyard="+vineyard+"&label="+label+"&type="+type+"&vintage="+
	vintage+"&price="+price+"&state="+state+"&country="+country+
	"&website="+website;

	xmlHttp.open("POST", url, true);

	xmlHttp.onreadystatechange=saveWine_stateChanged ;

	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.send(params);

	return true;
}

function saveWine_stateChanged(){
	if(xmlHttp.readyState == 4 && xmlHttp.status==200){
		var response = xmlHttp.responseText;
		if(response == "saved"){
			closePopupWineInfo();
			showAlert("Wine was saved successfully");
		}else{
			showAlert("Wine was not saved. Please try again.");
		}
	}
}

function closePopupWineInfo(){
	document.getElementById('wine-form').innerHTML = "";
	document.getElementById("popup_wine_info").style.display="none";
}

function updateVendorVisit(website){
	var url = "updateVendorVisits.php";
	var params = "website="+website;

	if(!xmlHttp) xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		return false;
	}

	xmlHttp.open("POST", url, true);

	xmlHttp.onreadystatechange= updateVendorVisit_stateChanged ;

	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.send(params);

	return true;
}

function updateVendorVisit_stateChanged(){
	if(xmlHttp.readyState == 4 && xmlHttp.status==200){
}
}

function showRecommendWindow(wine, id){
	//    var unsafeWine = wine.replace(/@/g, "&");
	//    unsafeWine = unsafeWine.replace(/\^/g, "'");
	//    unsafeWine = unsafeWine.replace(/~/g, "#");
	//    var tmp = unsafeWine.split(",");
	//    var vineyard = tmp[0];
	//    var label = tmp[1];
	//    var vintage = tmp[3];
	var vineyard = document.getElementById(wine+",vineyard").innerHTML;
	var label = document.getElementById(wine+",label").innerHTML;
    var vintage = document.getElementById(wine+",vintage").childNodes[0].innerHTML;
	var unsafeWine = vineyard+", "+label+", "+vintage;
	var fb_rec = document.getElementById('fb_wine_rec');
	var iframe = fb_rec.childNodes[0].childNodes[0];
	var src = iframe.getAttribute('src');
	src = src.replace(/www\.tasteroom\.com%2FpublicWine\.php.*&layout/, 'www.tasteroom.com%2FpublicWine.php%3Fid%3D'+id+'%26idw%3D'+wine+"&layout");
	iframe.setAttribute('src', src);
    document.getElementById("recommended_wine").innerHTML = "<b><center>"+unsafeWine+"</center></b>";
	document.getElementById("hidden_recommended_wine").value=wine;
	document.getElementById("recommend_area").style.display="block";
}

function hideRecommendWindow(){
	document.getElementById("recommend_area").style.display="none";
}

function recommendWine(){
	var wine = document.getElementById("hidden_recommended_wine").value;
	var recommend_list = "";
	var list = document.recommend_form.recommend_list

	for (var i=0; i < list.length; i++){
		if(list[i].checked){
			recommend_list += list[i].value+",";
		}
	}

	var url = "recommendWine.php";
	var params = "wine="+wine+"&recommend_list="+recommend_list;

	if(!xmlHttp) xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		return false;
	}

	xmlHttp.open("POST", url, true);

	xmlHttp.onreadystatechange=recommendWine_stateChanged ;

	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.send(params);

	return true;
}

function recommendWine_stateChanged(){
	if(xmlHttp.readyState == 4 && xmlHttp.status==200){
		var result = xmlHttp.responseText;
		if(result == "true"){
			showAlert("Your wine recommendation has been sent.");
		}else{
			showAlert("There was an error recommending your wine");
		}
		hideRecommendWindow();
	}
}

function followVendor(vendor, user){
	if(user == ""){
		showFaq("You must be logged in to follow "+vendor);
		return false;
	}
	var reply = confirm("Are you sure you want to follow "+vendor+"?");
	if(reply){
		if(!xmlHttp) xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null){
			return false;
		}

		var url = "followVendor.php";
		var param = "vendor="+vendor;

		xmlHttp.open("POST", url, true);
		xmlHttp.onreadystatechange=followVendor_stateChanged ;

		//Send the proper header information along with the request
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", param.length);
		xmlHttp.setRequestHeader("Connection", "close");

		xmlHttp.send(param);
		return true;
	}
	return false;
}

function followVendor_stateChanged(){
	if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
		var response = xmlHttp.responseText;
		if(response == "success"){
			//showFaq("You are now following "+response+". You will now receive updates on your profile with any news from "+response+".");
			var location = window.location.href;
            location = location.replace("#", "");
            window.location = location;
		}else if(reponse == "login"){
			showFaq("You must login to follow this vendor.");
		}else{
			showFaq("There was an error following this vendor. Please try again or <a style='text-decoration:underline;' href='contact.php'>contact us</a>.");
		}
	}
}

function unFollowVendor(vendor, user){
	if(user == ""){
		showFaq("You must be logged in to un-follow "+vendor);
		return false;
	}
	var reply = confirm("Are you sure you want to un-follow "+vendor+"?");
	if(reply){
		if(!xmlHttp) xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null){
			return false;
		}

		var url = "followVendor.php";
		var param = "vendor="+vendor+"&action=remove";

		xmlHttp.open("POST", url, true);
		xmlHttp.onreadystatechange=unFollowVendor_stateChanged ;

		//Send the proper header information along with the request
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", param.length);
		xmlHttp.setRequestHeader("Connection", "close");

		xmlHttp.send(param);
		return true;
	}
	return false;
}

function unFollowVendor_stateChanged(){
	if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
		var response = xmlHttp.responseText;
		if(response == "success"){
			//showFaq("You are now following "+response+". You will now receive updates on your profile with any news from "+response+".");
			var location = window.location.href;
            location = location.replace("#", "");
            window.location = location;
		}else{
			showFaq("There was an error un-following this vendor. Please try again or <a style='text-decoration:underline;' href='contact.php'>contact us</a>.");
		}
	}
}

function showUploading(){
	document.getElementById("faded").style.display="block";
	document.getElementById("uploading").style.display="block";
}

function hideUploading(){
	document.getElementById("faded").style.display="none";
	document.getElementById("uploading").style.display="none";
}

function optOutMailingList(vendorid){
	var reply = confirm("Are you sure you want to opt out?");
	if(reply){
		if(!xmlHttp) xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null){
			return false;
		}

		var url = "removeVendorMailingList.php";
		var param = "vendor="+vendorid;

		xmlHttp.open("POST", url, true);
		xmlHttp.onreadystatechange=optOutMailingList_stateChanged ;

		//Send the proper header information along with the request
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", param.length);
		xmlHttp.setRequestHeader("Connection", "close");

		xmlHttp.send(param);
		return true;
	}
	return false;
}

function optOutMailingList_stateChanged(){
	if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
		var response = xmlHttp.responseText;
		if(response == "success"){
			showFaq("You have successfully opted out of the mailing list.");
		}else{
			showFaq("There was an error opting you out. Please try again or <a style='text-decoration:underline;' href='contact.php'>contact us</a>.");
		}
	}
}
