// JavaScript Document

// post a listing page flow  
function nextScreen2() {
	// Update posting address
	var add;
	if(document.postlisting.postLocation.value != ""){
		add = document.postlisting.postLocation.value;
	}else{
		add = document.postlisting.address.value;
	}
	add = "<span class='address-header'>@ " + add + "</span>";
	
	// configure span style for and expiration per category
	var span;
	var expire = "";
	var showDate;
	var id = document.postlisting.postCategory.value;
	if(id == 56){
		span = "event";
		showDate = true;
	}
	if(id == 59){
		span = "job";
		expire = "&nbsp; <i class='exp'>(Expires after 30 days)</i>";
		showDate = false;
		document.postlisting.postStartDate.value = "";
		document.postlisting.postEndDate.value = "";
	}
	if(id == 61){
		span = "classified";
		expire = "&nbsp; <i class='exp'>(Expires after 60 days)</i>";
		showDate = false;
		document.postlisting.postStartDate.value = "";
		document.postlisting.postEndDate.value = "";
	}
	if(id == 62){
		span = "group";
		expire = "&nbsp; <i class='exp'>(Expires after 2 years)</i>";
		showDate = false;
		document.postlisting.postStartDate.value = "";
		document.postlisting.postEndDate.value = "";
	}
	if(id == 63){
		span = "real-estate";
		expire = "&nbsp; <i class='exp'>(Expires after 60 days)</i>";
		showDate = false;
		document.postlisting.postStartDate.value = "";
		document.postlisting.postEndDate.value = "";
	}
	
	// show a date field if post is an event
	if(showDate) {
		document.getElementById("post-date").style.display="block";
	}else{
		document.getElementById("post-date").style.display="none";
	}
	
	// set the category  variable
	var w = document.postlisting.postCategory.selectedIndex;
	var cat = document.postlisting.postCategory.options[w].text;
	cat = "&nbsp;&nbsp;<span class='"+span+"-span' >"+cat+"</span>";
	
	// toggle screens
	document.getElementById("map-layer").style.display="none";
	document.getElementById("photo-layer").style.display="block";
	document.getElementById("post-screen-1").style.display="none";
	document.getElementById("post-screen-2").style.display="block";
	
	// concatenate vars for posting header in screen 2
	document.getElementById("section-title").innerHTML += cat + add + expire;
}
//
function lastScreen1()  {
	// toggle screens
	document.getElementById("map-layer").style.display="block";
	document.getElementById("photo-layer").style.display="none";
	document.getElementById("post-screen-2").style.display="none";
	document.getElementById("post-screen-1").style.display="block";	
	
	// reset posting header
	document.getElementById("section-title").innerHTML = "Post A Listing";
	// reset error message
	document.getElementById("text-error").innerHTML = "";
	document.getElementById("title-error").innerHTML = "";
}
//
function nextScreen3() {
	//
	document.getElementById("post-screen-3").style.display="block";
	document.getElementById("post-screen-2").style.display="none";
	document.getElementById("map-layer").style.display="block";
	document.getElementById("account-right").style.borderLeft="none";
	document.getElementById("photo-layer").style.display="none";
	// remove push pin  add html box
	removeMarker();
	addPreview();
	addPin();
	scrollMe();
}
//
function addPreview() {
	//
	var title = document.postlisting.postTitle.value;
	var text =  document.postlisting.postText.value;
	var textArray = new Array();
	var textSplit = text.split("\n");
	textArray = textSplit;
	var newText = "";	
	for(var i = 0; i < textArray.length; i++){
			newText += textArray[i] + "<br />";
	}
	var address = document.postlisting.postLocation.value;

	var date = "";
    if ($('#recur_type').val() == "" || $('#recur_type').val() == "annually") {
		date = $('#postStartDate').val() +' - '+$('#postEndDate').val();
    } else if ($('#recur_type').val() == "weekly") {
        var start_date = $('#postStartDate').val();
        var end_date = $('#postEndDate').val();
        start_date = start_date.replace(/\./g, "/"); 
        end_date = end_date.replace(/\./g, "/"); 
        start_date = new Date(start_date);
        end_date = new Date(end_date);
        var days= end_date.getTime() - start_date.getTime();
        var days = parseInt(days / (1000 * 60 * 60 * 24));
        if (days>365)
            date = "every " + $('#byday').val().split(',').join(', ') + " year 'round ";
        else
		    date = "every " + $('#byday').val().split(',').join(', ') + " from " + $('#postStartDate').val() +' to '+$('#postEndDate').val();
    } else if ($('#recur_type').val() == "monthly") {		
		var weeks = $('#byweek').val().split(',');;
		var tagMap = {'first': "1<sup>st</sup>", 'second': '2<sup>nd</sup>', 'third': '3<sup>rd</sup>', 'fourth': '4<sup>th</sup>'};
        var start_date = $('#postStartDate').val();
        var end_date = $('#postEndDate').val();
        start_date = start_date.replace(/\./g, "/"); 
        end_date = end_date.replace(/\./g, "/"); 
        start_date = new Date(start_date);
        end_date = new Date(end_date);
        var days= end_date.getTime() - start_date.getTime();
        var days = parseInt(days / (1000 * 60 * 60 * 24));
		if (weeks.length == 1){
            if (days>365)
                date = "every " + tagMap[$('#byweek').val()] + " " + $('#byday').val().split(',').join(', ') + " of the month year 'round ";
            else
			    date = "every " + tagMap[$('#byweek').val()] + " " + $('#byday').val().split(',').join(', ') + " of the month from " + $('#postStartDate').val() +' to '+$('#postEndDate').val();
		}else if (weeks.length > 1)  {
			var tmp = [];
			for(var i=0;i<weeks.length;i++) {
				tmp[tmp.length] =tagMap[weeks[i]] +  $('#byday').val().split(',').join(', ');
			}
            if (days>365)
                date = "every " + tmp.join(' and ') + " of the month year 'round ";
            else
			    date = "every " + tmp.join(' and ') + " of the month from " + $('#postStartDate').val() +' to '+$('#postEndDate').val();
		}
    } else if ($('#recur_type').val() == "specific") {
		date = $('#specific_dates').val().split(',').join(', ');
    }

    date = "<b style='font-size:12px'>Date</b> " + date;

	//
	$("#pop-up-title").html(title);
	$("#pop-up-text").html(newText);
	$("#overlay-title").html(title);
	$("#pop-up-address").html(address);
	$("#pop-up-date").html(date);
	var t = $("#postTime").val();
	if( t !=  "" ){
		$("#pop-up-date").append(" | <b>Time</b> " + t);	
	}
	
}
//
function lastScreen2()  {
	//
	document.getElementById("post-screen-3").style.display="none";
	document.getElementById("post-screen-2").style.display="block";
	document.getElementById("map-layer").style.display="none";
	document.getElementById("account-right").style.borderLeft="1px solid #666";
	document.getElementById("photo-layer").style.display="block";
	// remove push pin  add html box
	addMarker();
	removePin();
	
	//
}
//
function valdatePost() {
	var title = document.postlisting.postTitle.value;
	var text = document.postlisting.postText.value;
	var cat = document.postlisting.postTitle.value;
	//
	var error = new Array();
	//
	if(title == "") {
		error.push("error");
		document.getElementById("title-error").innerHTML = "enter title";
	}
	if(text.length > 5000) {
		var amountOver = text.length - 5000;
		error.push("error");
		document.getElementById("text-error").innerHTML = "please reduce by " + amountOver + " characters";
	}

	//validate recur BEGIN
	var recur_type = $('#recur_type').val();
	if (recur_type == 'weekly' && $('#byday').val() == "") {
		$('#weekly_error').html("the day of week is required.");
		error.push("error");
	} else {
		$('#weekly_error').html("");
	}

	if (recur_type == 'monthly') {
		if ($('#byday').val() == "") {
			$('#monthly_day_error').text("the day of week is required.");
			error.push("error");
		} else {									
			$('#monthly_day_error').text("");
		}

		if ($('#byweek').val() == "") {
			$('#monthly_week_error').text("the week of the month is required.");
			error.push("error");
		} else {
			$('#monthly_week_error').text("");
		}
	}
	//validate recur END

	if(error.length != 0) {
		return;
	}else{
		nextScreen3();	
		document.getElementById("text-error").innerHTML = "";
		document.getElementById("title-error").innerHTML = "";
	}
}
//
function addTags(id) {
	var str;
	str = document.getElementById(id).innerHTML;
	str = str.replace("&amp;","&");
	document.postlisting.postTags.value += str + ", ";
}
//
function clearMe() {
	if(document.postlisting.address.value == "(general or specific)"){
		document.postlisting.address.value = "";
	}
}
//