// JavaScript Document
var iCurrentAlternative = 0;
//
// FUNCTIONS FOR FLASH INTERACTION
//
function changeSwatch(sSwatch, sBase, sAccent) {
	showPalette();
	swatch = sSwatch.toLowerCase();
	$("cntPalettesFlash").changeSwatch(sBase, sAccent);
}
//
function selectColor(sColorID, sColorName, sColorValue, sColorCollection) {
	if (swatch == "primary") {
		$("fldPrimarySwatch").value = sColorID;
	} else if (swatch == "accent1") {
		$("fldAccent1Swatch").value = sColorID;
	} else if (swatch == "accent2") {
		$("fldAccent2Swatch").value = sColorID;
	}
	$("cntSwatchesFlash").setSwatchValue(sColorID, sColorName, sColorValue, sColorCollection);
}
//
function savePalette() {
	if (($F("fldPrimarySwatch").length > 0) && ($F("fldAccent1Swatch").length > 0) && ($F("fldAccent2Swatch").length > 0)) {
		if ($F("userID") != "-1") {
			savePaletteData();
		} else {
			showForm('registration');
		}
	} else {
		writeError("Unable to save your palette", "Please ensure that you have at all three colors filled.");
	}
}
//
function saveNewUserPaletteData(s) {
	Shadowbox.close();
	var userID = parseTag("userID",s.responseText);
	var userName = parseTag("userName",s.responseText);
	$("userID").value = userID;
	try {
		$("signIn").style.display = "none";
		$("roomsNotLoggedIn").style.display = "none";
		$("signOutName").innerHTML = userName;
		$("signOut").style.display = "block";
		$("roomsLoggedIn").style.display = "block";
	} catch(e) {
		//	
	}
	savePaletteData();
}
//
function parseTag(sTag, sContent) {
	var sResponse = sContent;
	var iTemp, iStart, iStop;
	iTemp = sResponse.indexOf("<"+sTag);
	if (iTemp >= 0) {
		iStart = sResponse.indexOf(">", iTemp)+1;
		iStop = sResponse.indexOf("</"+sTag+">");
		return sResponse.substr(iStart, iStop-iStart);
	} else {
		return "";
	}
}
//
function savePaletteData() {
	var data="userID="+encodeURIComponent($F("userID"));
	data+="&projectID="+encodeURIComponent($F("projectID"));
	data+="&primary="+encodeURIComponent($F("fldPrimarySwatch"));
	data+="&accent1="+encodeURIComponent($F("fldAccent1Swatch"));
	data+="&accent2="+encodeURIComponent($F("fldAccent2Swatch"));
	data+="&r="+Math.random();
	var url = "/community/ajax/PutPrimaryPaletteAjax.html";
	var myAjax = new Ajax.Request(url,{asynchronous:true, method:'post', parameters:data, onSuccess:savePaletteDataSC, onFailure:fnErr});
}
//
function savePaletteDataSC(r) {
	if (r.responseText.indexOf("success") > 0) {
		var sProjectID, sProjectType;
		sProjectID = parseTag("projectID", r.responseText);
		sProjectType = $F("projectType")
		if (sProjectType != 'experience') {
			sProjectType = 'palette';
		}
		$("projectID").value = sProjectID;
		showForm(sProjectType);
	} else {
		writeError("Unable to save your palette", "Please try again.");
	}
}
//
function populateSavedData(iPrimaryID,iAccent1ID,iAccent2ID) {
	$("fldPrimarySwatch").value = iPrimaryID;
	$("fldAccent1Swatch").value = iAccent1ID;
	$("fldAccent2Swatch").value = iAccent2ID;
}
//
function setup() {
	if (($F('projectID') != "-1") && ($F('userID') != "-1")) {
		if ($F('projectType') == "palette") {
			showForm("palette");
		} else if ($F('projectType') == "experience") {
			showForm("experience");
		}
	} else {
		$("fldPrimarySwatch").value = "";
		$("fldAccent1Swatch").value = "";
		$("fldAccent2Swatch").value = "";
	}
}
//
function changePanel(s) {
	var url = "";
	var fnSuccess;
	switch (s) {
		case 'step1':
			url = "ajax/GetFreshPalettesAjax.html";
			fnSuccess = getPalettesSC;
			break;
		case 'step2':
			url = "ajax/GetHealthyHomeExperiencesAjax.html";
			fnSuccess = getExperiencesSC;
			break;
	}
	if (url.length > 0) {
		var data="projectID=" +$F("projectID");
		data+="&r="+Math.random();
		var myAjax = new Ajax.Request(url,{asynchronous:true, method:'post', parameters:data, onSuccess:fnSuccess, onFailure:fnErr});
	}
}
//
function getPalettesSC(r) {
	$("cntEditForm").innerHTML = r.responseText;
	calculateRemainingChars('Acheive', 1000);
	setupAlternatePalettes();
	getInspirationPhotos();
	self.location.hash = "#top";
}
//
function getInspirationPhotos() {
	var data="projectID=" +$F("projectID");
	data+="&r="+Math.random();
	var myAjax = new Ajax.Updater("cntInspirationPhotos", "ajax/GetInspirationPhotosAjax.html", {method: 'post', parameters:data});	
}
//
function getMyPhotos() {
	var data="projectID=" +$F("projectID");
	data+="&r="+Math.random();
	var myAjax = new Ajax.Updater("cntMyPhotos", "ajax/GetMyPhotosAjax.html", {method: 'post', parameters:data});	
}
//
function getExperiencesSC(r) {
	$("cntEditForm").innerHTML = r.responseText;
	calculateRemainingChars('Experience',1000);
	calculateRemainingChars('ReviewDescription',1000);
	getMyPhotos();
	self.location.hash = "#top";
}
//
function setRating(iMainRating, iCat1Rating, iCat2Rating, iCat3Rating) {
	$("fldMainRating").value = iMainRating;
	$("fldCat1Rating").value = iCat1Rating;
	$("fldCat2Rating").value = iCat2Rating;
	$("fldCat3Rating").value = iCat3Rating;
	saveReview();
}
//
function setupAlternatePalettes() {
	setupAlternatePalette(1);
	setupAlternatePalette(2);
	setupAlternatePalette(3);
}
//
function setupAlternatePalette(i) {
	try {
		$("cntAlternatePrimary_"+i).style.backgroundColor = $F("fldAlternatePrimary_"+i);
		$("cntAlternateAccent1_"+i).style.backgroundColor = $F("fldAlternateAccent1_"+i);
		$("cntAlternateAccent2_"+i).style.backgroundColor = $F("fldAlternateAccent2_"+i);
	} catch(e) {
		writeError("An error has occurred when attempting to display your alternate palette.", "");
	}
}
//
function saveAlternatePalette(sPrimary,sAccent1,sAccent2,sPrimaryID,sAccent1ID,sAccent2ID) {
	Shadowbox.close();
	try {
		$("fldAlternatePrimary_"+iCurrentAlternative).value = sPrimary;
		$("fldAlternateAccent1_"+iCurrentAlternative).value = sAccent1;
		$("fldAlternateAccent2_"+iCurrentAlternative).value = sAccent2;
		$("fldAlternatePrimaryID_"+iCurrentAlternative).value = sPrimaryID;
		$("fldAlternateAccent1ID_"+iCurrentAlternative).value = sAccent1ID;
		$("fldAlternateAccent2ID_"+iCurrentAlternative).value = sAccent2ID;
		var url = "ajax/PutAlternatePaletteAjax.html";
		var data="projectID=" +$F("projectID");
		data+="&primary="+encodeURIComponent(sPrimaryID);
		data+="&accent1="+encodeURIComponent(sAccent1ID);
		data+="&accent2="+encodeURIComponent(sAccent2ID);
		data+="&paletteIndex="+encodeURIComponent(iCurrentAlternative);
		data+="&r="+Math.random();
		var myAjax = new Ajax.Request(url,{asynchronous:true, method:'post', parameters:data, onSuccess:saveAlternatePaletteDataSC, onFailure:fnErr});
	} catch(e) {
		writeError("An error has occurred when attempting to save your alternate palette.", "Please try again.");
		iCurrentAlternative = 0;
	}
}
//
function saveAlternatePaletteDataSC(r) {
	if (r.responseText.indexOf("success") >= 0) {
		setupAlternatePalette(iCurrentAlternative);
	} else {
		writeError("An error has occurred when attempting to save your alternate palette.", "Please try again.");	
	}
	iCurrentAlternative = 0;
}
//
function editPalette(iIndex) {
	if (iIndex > 0 && iIndex < 4) {
		var sPrimary = $F("fldAlternatePrimary_"+iIndex).substring(1);
		var sAccent1 = $F("fldAlternateAccent1_"+iIndex).substring(1);
		var sAccent2 = $F("fldAlternateAccent2_"+iIndex).substring(1);
		iCurrentAlternative = iIndex;
		Shadowbox.open({
			player: 'iframe',
			content: '/community/ajax/GetAlternatePaletteAjax.html?primary='+sPrimary+'&accent1='+sAccent1+'&accent2='+sAccent2,
			height:560,
			width:870
		});
	}
}
//
function saveUploadImage(sDescription, sImage) {
	   var url="/community/ajax/PutPhotoAjax.html";
        var data="projectID=" + encodeURIComponent($F("projectID"));
        data+="&userID=" + encodeURIComponent($F("userID"));
        data+="&photoType=" + encodeURIComponent($F("photoType"));
        data+="&photoDescription=" + sDescription.substr(1000);
        data+="&r="+Math.random();
        var myAjax = new Ajax.Request(url,{asynchronous:true, method:'post', parameters:data, onSuccess:saveUploadImageSC, onFailure:fnErr});
}
//
function saveUploadImageSC(r) {
        Shadowbox.close();
        var sType = $F("photoType");
        if (sType == "inspiration") {
		   getInspirationPhotos();
        } else {
		   getMyPhotos();
        }
}
// ---------------------------------------------------------------
// FUNCTIONS FOR FORM INTERACTION
// ---------------------------------------------------------------
//
var curEditFldBase = "";
var curEditGridBase = 0;
//
function getValue(arr,sName,oDefault) {
	for (var i = 0; i < arr.length; i++) {
		if (arr[i][0] == sName) {
			return arr[i][1];
		}
	}
	return oDefault;
}
//
function ResponseObject(s) {
	try {
		s = s.trim();
		this.success = parseTag("status", s);
		this.base = parseTag("base", s);
		this.value = parseTag("val", s);
		var iPosStart, iPosEnd;
		iPosStart = 0;
		iPosEnd = this.value.length;
		if (this.value.indexOf("<![CDATA[") >= 0) {
			iPosStart = this.value.indexOf("<![CDATA[")+9;
		}
		if (this.value.indexOf("]]>") >= 0) {
			iPosEnd = this.value.indexOf("]]>") - iPosStart;
		}
		this.value = this.value.substr(iPosStart, iPosEnd);
	} catch (e) {
		this.success = "failure";
		this.base = null;
		this.value = null;
	}
}
//
function fnErr(t) {
	errFunc(t);
}
//
//
// - - - - - - - - - - - - - -
// SHOW/HIDE FUNCTIONS
// - - - - - - - - - - - - - -
//
function showForm(sDestination) {
	if (sDestination == "palette") {
		hidePalette();
		changePanel('step1');
		$("cntEditForm").style.display = "block";
	} else if (sDestination == "experience") {
		hidePalette();
		changePanel('step2');
		$("cntEditForm").style.display = "block";
	} else {
		showRegistration("palette");
	}
}
//
function hideForm() {
	$("cntEditForm").style.display = "none";
}
//
function showPalette() {
	hideForm();
	$("cntPalettes").style.display = "block";
}
//
function hidePalette() {
	$("cntPalettes").style.display = "none";
}
//
function showUploader() {
	Shadowbox.open({
        player:     'iframe',
	   content: 'ajax/GetImageUploaderAjax.html?projectID='+$F("projectID"),
        height:325,
        width:760
    });
}
//
function cancelUpload() {
	Shadowbox.close();
}
//
function hideAllEdit() {
	try { hideEditFld(); } catch(e) {};
}
//
function selectRoomType(roomTypeElem, roomTypeSelect) {
	var roomType;
	if (document.all)
		roomType = document.getElementById(roomTypeElem).innerText.toLowerCase();
	else
		roomType = document.getElementById(roomTypeElem).textContent.toLowerCase();
	
	if (roomType == '') return;
	var options = document.getElementById(roomTypeSelect).options;
	for (var i=0;i<options.length;i++) {
		if (options[i].text.toLowerCase() == roomType) {
			options[i].selected = true;
			break;
		}
	}
}
//
function showEditFld(sBase) {
	hideAllEdit();
	try {
		$("display"+sBase).style.display = "none";
		$("edit"+sBase).style.display = "none";
		$("cnt"+sBase).style.display = "block";
		$("save"+sBase).style.display = "none";
		curEditFldBase = sBase;
	} catch(e) {
		//
	}
}
//
function showSaveStatus(sBase) {
	try {
		$("display"+sBase).style.display = "none";
		$("edit"+sBase).style.display = "none";
		$("cnt"+sBase).style.display = "none";
		$("save"+sBase).style.display = "block";
	} catch(e) {
		//
	}
}
//
function hideEditFld() {
	try {
		$("display"+curEditFldBase).style.display = "block";
		$("edit"+curEditFldBase).style.display = "inline";
		$("cnt"+curEditFldBase).style.display = "none";
		$("save"+curEditFldBase).style.display = "none";
		curEditFldBase = "";
	} catch(e) {
		//
	}
}
//
function hideEditFldByName(sFldBase) {
	 hideEditFld();
	try {
		$("display"+sFldBase).style.display = "block";
		$("edit"+sFldBase).style.display = "inline";
		$("cnt"+sFldBase).style.display = "none";
		$("save"+sFldBase).style.display = "none";
	} catch(e) {
		//
	}
}
//
//
// - - - - - - - - - - - - - -
// SAVE FUNCTIONS
// - - - - - - - - - - - - - -
//
function saveEditFld(sBase,sType) {
	var bValid = true;
	if (sType == "checkbox") {
		sVal = $("fld"+sBase).checked;
	} else {
		sVal = $F("fld"+sBase);
	}
	if (bValid) { makeAJAXcall(sBase,sVal); }
}
//
function makeAJAXcall(sBase,sValue) {
	showSaveStatus(sBase);
	var url;
	switch (sBase) {
		default:
			url = "ajax/PutRoomDetailsAjax.html";
			break;
	}
	var data="projectID=" +$F("projectID");
	data+="&base="+encodeURIComponent(sBase);
	data+="&val="+encodeURIComponent(sValue);
	data+="&r="+Math.random();
	var fnSuccess;
	switch (sBase) {
		case "CommentsOptin":
			fnSuccess = saveCommentsSC;
			break;
		default:
			fnSuccess = saveEditFldSC;
			break;
	}
	var myAjax = new Ajax.Request(url,{asynchronous:true, method:'post',parameters:data, onSuccess:fnSuccess, onFailure:fnErr});	
}
//
function saveEditFldSC(r) {
	var sValue = "";
	try {
		var ro = new ResponseObject(r.responseText);
		if (ro.success == "success") {
			// if the save was successful
			try {
				// try to update as if the field is a dropdown
				$("display"+ro.base).innerHTML = $("fld"+ro.base).options[$("fld"+ro.base).selectedIndex].text;
			} catch(e) {
				// update as a textfield
				$("display"+ro.base).innerHTML = $F("fld"+ro.base);
			}
			hideEditFld();
		} else {
			// base value not found
			alert("An error occurred while trying to make your updates. Please try again.");
			showEditFld(ro.base);
			try { $("fld"+ro.base).focus(); } catch(e2) {};
		}
		return ro;
	} catch (e) {
		alert("An error occurred while trying to make your updates. Please try again.");
		return null;
	}
	checkStatus();
}
//
function saveCommentsSC(r) {
	var sValue = "";
	try {
		var ro = new ResponseObject(r.responseText);
		if (ro.success == "success") {
			// if the save was successful
			if ($("fldCommentsOptin").checked) {
				$("cntCommentsListings").style.display = "block";
				getComments();
			} else {
				$("cntCommentsListings").innerHTML = "";
			}
		} else {
			// base value not found
			alert("An error occurred while trying to make your updates. Please try again.");
		}
		return ro;
	} catch (e) {
		alert("An error occurred while trying to make your updates. Please try again.");
		return null;
	}
	checkStatus();
}
//
function calculateRemainingChars(sBase,iMax) {
	try {
		var iRemaining = (iMax - $F("fld"+sBase).length);
		if (iRemaining <= 0) {
			$("fld"+sBase).value = $F("fld"+sBase).substring(0,iMax);
			iRemaining = 0;
			$("rt"+sBase).style.color = "#FF0000";
		} else {
			$("rt"+sBase).style.color = "#999999";
		}
		$("rt"+sBase).innerHTML = "( " + iRemaining + " characters remaining )";
	} catch (e) {
	}
}
//
function optinComments() {
	$("cntCommentsListings").style.display = "none";	
	saveEditFld("CommentsOptin","checkbox");
}
//
function deleteComment(sCommentID) {
	try {
		var url = "ajax/DeleteCommentAjax.html";
		var data="projectID="+encodeURIComponent($F("projectID"));
		data+="&commentID="+encodeURIComponent($F(sCommentID));
		data+="&r="+Math.random();
		if (confirm("Are you sure you want to delete this comment?")) {
			var myAjax = new Ajax.Request(url,{asynchronous:true, method:'post',parameters:data, onSuccess:getCommentsSC, onFailure:fnErr});	
		}
	} catch(e) {
		alert("Cannot delete comment because the id is invalid.");	
	}
}
//
function getComments() {
	var url = "ajax/GetCommentsAjax.html";
	var data="projectID="+encodeURIComponent($F("projectID"));
	data+="&r="+Math.random();
	var myAjax = new Ajax.Request(url,{asynchronous:true, method:'post',parameters:data, onSuccess:getCommentsSC, onFailure:fnErr});	
}
//
function getCommentsSC(r) {
	$("cntCommentsListings").innerHTML = r.responseText;
}
//
function saveReview() {
	var url = "ajax/PutReviewAjax.html";
	var data="projectID="+encodeURIComponent($F("projectID"));
	data+="&overallRating="+encodeURIComponent($F("fldMainRating"));
	data+="&smellRating="+encodeURIComponent($F("fldCat1Rating"));
	data+="&coverageRating="+encodeURIComponent($F("fldCat2Rating"));
	data+="&valueRating="+encodeURIComponent($F("fldCat3Rating"));
	data+="&reviewTitle="+encodeURIComponent($F("fldReviewTitle"));
	data+="&reviewDescription="+encodeURIComponent($F("fldReviewDescription"));
	data+="&r="+Math.random();
	var myAjax = new Ajax.Request(url,{asynchronous:true, method:'post',parameters:data, onSuccess:saveReviewSC, onFailure:errFunc});
}
//
function saveReviewSC(r) {
	if (r.responseText.indexOf("success") > 0) {
		$("displayReviewDescription").innerHTML = $F("fldReviewDescription");
		$("displayReviewTitle").innerHTML = $F("fldReviewTitle");
		hideEditFldByName("ReviewTitle");
		hideEditFldByName("ReviewDescription");
	} else {
		writeError("An error has occurred when attempting to share this content. Please try again.");
	}
}
//
// - - - - - - - - - - - - - -
// STATUS FUNCTIONS
// - - - - - - - - - - - - - -
//
function checkStatus() {
	//
}
//
function setStatus(s) {
	//
}
//
function saveStatusSC(r) {
	//
}
//
// - - - - - - - - - - - - - -
// PHOTO FUNCTIONS
// - - - - - - - - - - - - - -
function setGalleryPhoto(iID) {
	var url = "ajax/PutGalleryPhotoAjax.html";
	var data="projectID="+encodeURIComponent($F("projectID"));
	data+="&photoID="+encodeURIComponent(iID);
	data+="&r="+Math.random();
	var myAjax = new Ajax.Request(url,{asynchronous:true, method:'post',parameters:data, onSuccess:setGalleryPhotoSC, onFailure:fnErr});
}
//
function setGalleryPhotoSC(r) {
	if (r.responseText.indexOf("success") > 0) {
		var sType = $F("photoType");
		if (sType == "inspiration") {
			getInspirationPhotos();
		} else {
			getMyPhotos();
		}
	} else {
		writeError("An error has occurred when attempting to set your gallery photo. Please try again.");
	}
}
//
function deleteInspirationPhoto(iID) {
	if (confirm("Are you sure you want to delete this photo? This cannot be undone.")) {
		var url = "ajax/DeleteInspirationPhotoAjax.html";
		var data="projectID="+encodeURIComponent($F("projectID"));
		data+="&photoID="+encodeURIComponent(iID);
		data+="&r="+Math.random();
		var myAjax = new Ajax.Request(url,{asynchronous:true, method:'post',parameters:data, onSuccess:deleteInspirationPhotoSC, onFailure:fnErr});	
	}
}
//
function deleteInspirationPhotoSC(r) {
	if (r.responseText.indexOf("success") > 0) {
		getInspirationPhotos();
	} else {
		writeError("An error has occurred when attempting to delete your photo. Please try again.");
	}
}
//
function deleteMyPhoto(iID) {
	if (confirm("Are you sure you want to delete this photo? This cannot be undone.")) {
		var url = "ajax/DeleteMyPhotoAjax.html";
		var data="projectID="+encodeURIComponent($F("projectID"));
		data+="&photoID="+encodeURIComponent(iID);
		data+="&r="+Math.random();
		var myAjax = new Ajax.Request(url,{asynchronous:true, method:'post',parameters:data, onSuccess:deleteMyPhotoSC, onFailure:fnErr});	
	}
}
//
function deleteMyPhotoSC(r) {
	if (r.responseText.indexOf("success") > 0) {
		getMyPhotos();
	} else {
		writeError("An error has occurred when attempting to delete your photo. Please try again.");
	}
}

