/* */

var strCargando = '<center><br><br><img src="engines/classifieds/img/ajax-loader.gif" alt="Loading..."></center>';

function saveSessionVars() {
	var xY = "&name="+GetId('name').value;
	xY += "&email="+GetId('email').value;
	xY += "&address="+GetId('address').value;
	xY += "&city="+GetId('city').value;
	xY += "&state="+GetId('state').value;
	xY += "&zip="+GetId('zip').value;
	xY += "&phone="+GetId('phone').value;
	xY += "&title_hidden="+GetId('title_hidden').value;
	xY += "&text_hidden="+GetId('text_hidden').value;
	xY += "&category="+GetId('category').value;
	goAJAX('class_content','engines/classifieds/ajax/saveSession.php',xY,'','','1','1');
}

function saveSessionVars2() {
	var xY = "&start_date="+GetId('start_date').value;
	xY += "&duration="+GetId('duration').value;
	xY += "&end_date="+GetId('end_date').value;

	goAJAX('class_content','engines/classifieds/ajax/saveSession.php',xY,'','','1','1');
}

function searchAnAd() {
	goAJAX('class_content', 'engines/classifieds/ajax/searchAnAd.php', '', '', strCargando);
	return false;
}

function searchAds() {
	word = GetId('word').value;
	word1 = escape(word);
	cat = GetId('category').value;
	cat1 = escape(cat);
	location.href = "classifieds-1000-"+word1+"-"+cat1+".html"; 
}

function placeAnAd() {
	goAJAX('class_content', 'engines/classifieds/ajax/placeAnAd.php', '', '', strCargando);
	return false;
}

function submitStep1() {
	var xY = "step=2";
	xY += "&category="+GetId('category').value;
	parent.goAJAX('class_content', 'engines/classifieds/ajax/placeAnAd.php', xY, '', strCargando);
	return false;
}

function submitStep2(returning) {
	if (GetId('title_hidden').value == "Insert title here") {
		alert("Please, complete the title of this ad.");
		return false;
		exit();
	}
	if (GetId('text_hidden').value == "This is an example text for the ad text. Please explain here what are you selling in this ad. Your contact information will be above this text, do not write here.") {
		alert("Please, complete the text of this ad.");
		return false;
		exit();
	}
	if (returning == "" || returning == null) {
		if (GetId('name').value != "" || GetId('email').value != "" || GetId('address').value != "" || GetId('city').value != "" || GetId('state').value != "" || GetId('zip').value != "" || GetId('phone').value != "" ) {
			
			saveSessionVars();
			
			var xY = "step=3";
			parent.goAJAX('class_content', 'engines/classifieds/ajax/placeAnAd.php', xY, '', strCargando);
			return false;
		} else {
			alert("Please, complete all fields from the Contact Information");
			return false;
		}
	} else {
		var xY = "step=3";
		parent.goAJAX('class_content', 'engines/classifieds/ajax/placeAnAd.php', xY, '', strCargando);
		return false;
	}
}

function submitStep3(returning) {
	if (returning == "" || returning == null) {
		if (GetId('start_date').value != "" || GetId('duration').value != "0" || GetId('end_date').value != "") {
			
			saveSessionVars2()
			
			var xY = "step=4";
			parent.goAJAX('class_content', 'engines/classifieds/ajax/placeAnAd.php', xY, '', strCargando);
			return false;
		} else {
			alert("Please, complete all fields from the Schedule Information");
			return false;
		}
	} else {
		var xY = "step=4";
		parent.goAJAX('class_content', 'engines/classifieds/ajax/placeAnAd.php', xY, '', strCargando);
		return false;
	}
}

function submitStep4() {
		var xY = "step=5";
		parent.goAJAX('class_content', 'engines/classifieds/ajax/placeAnAd.php', xY, '', strCargando);
		return false;
}

function submitStep5() {
		var xY = "step=6";
		//parent.goAJAX('class_content', 'engines/classifieds/ajax/placeAnAd.php', xY, 'redirectHTTPS(respuesta);', strCargando, '1', '1');
		parent.goAJAX('class_content', 'engines/classifieds/ajax/placeAnAd.php', xY);
		return false;
}

function redirectHTTPS(respuesta) {
	//location.href = "https://www.expressmilwaukee.com/index.php?"+respuesta; 
	location.href = "index.php?"+respuesta; 
	//alert("This is a test ONLY!");
}

function editTitle() {
	currentValue = document.getElementById("title_hidden").value;
	element = document.getElementById("class_title");
	element.innerHTML = "<input type=\"text\" value=\""+currentValue+"\" size=\"30\" class=\"inputText\" maxlength=\"70\" onblur=\"editTitleOnBlur(this);\" />";
	element.firstChild.focus();
}

function editTitleOnBlur(field) {
	if (field.value != ""){
		
		elem = document.getElementById("class_title");
		elem.innerHTML = "&nbsp;&nbsp;&nbsp;"+field.value;
		document.getElementById("title_hidden").value = field.value;
		
		saveSessionVars();
		
		return false;
	}
}

function editBody() {
	currentValue = document.getElementById("text_hidden").value;
	element = document.getElementById("class_text");
	element.innerHTML = "<textarea rows=\"4\" id=\"bodyTextArea\" cols=\"40\" onblur=\"editBodyOnBlur(this);\" class=\"inputText\">"+currentValue+"</textarea>";
	element.firstChild.focus();
}

function editBodyOnBlur(field) {
	if (field.value != ""){		
		
		elem = document.getElementById("class_text");
		elem.innerHTML = field.value;
		document.getElementById("text_hidden").value = field.value;
		
		saveSessionVars();
		
		return false;
	}
}

function uploadImgClass() {
	
	saveSessionVars();
	
	putIn('winTit350','Upload Image')
	showWindow('win350');	
	goAJAX('winCont350','engines/classifieds/ajax/uploadImage.php','', 'centerDivWindow(\'win350\');');
	return false;
}

function updateImg(img, imgt) {
	GetId('imageUpdate').innerHTML = '<div id="imageUpdate"><a id="Hthumb" href="/imgs/classifieds/'+img+'" onclick="uploadImgClass(); return false;" class="img_thumb"><img src="imgs/classifieds/'+imgt+'" border="0" /></a></div>';	
}

var aFinMes = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 

function finMes(nMes, nAno){
   return aFinMes[nMes - 1] + (((nMes == 2) && (nAno % 4) == 0)? 1: 0);
} 

function padNmb(nStr, nLen, sChr){
    var sRes = String(nStr);
    for (var i = 0; i < nLen - String(nStr).length; i++)
     sRes = sChr + sRes;
    return sRes;
}

function makeDateFormat(nDay, nMonth, nYear){
    var sRes;
    sRes =  padNmb(nYear, 4, "0") + "-" + padNmb(nMonth, 2, "0") + "-" + padNmb(nDay, 2, "0");
    return sRes;
}

function incDate(sFec0){
   var nDia = parseInt(sFec0.substr(8, 2), 10);
   var nMes = parseInt(sFec0.substr(5, 2), 10);
   var nAno = parseInt(sFec0.substr(0, 4), 10);
   nDia += 1;
   if (nDia > finMes(nMes, nAno)){
    nDia = 1;
    nMes += 1;
    if (nMes == 13){
     nMes = 1;
     nAno += 1;
    }
   }
   return makeDateFormat(nDia, nMes, nAno);
} 

function addToDate(sFec0, sInc){
   var nInc = Math.abs(parseInt(sInc));
   var sRes = sFec0;
   for (var i = 0; i < nInc; i++) {
	   sRes = incDate(sRes);
   }
   return sRes;
} 

function setEndDate() {
	var duration = GetId('duration').value;
	var startDate = GetId('start_date').value;
	var Hoy = new Date();
	var Month = Hoy.getMonth()+1;
	if (Month == 1 || Month == 2 || Month == 3 || Month == 4 || Month == 5 || Month == 6 || Month == 7 || Month == 8 || Month == 9) {
		Month = "0"+Month;
	}
	var Day = Hoy.getDate();
	if (Day == 1 || Day == 2 || Day == 3 || Day == 4 || Day == 5 || Day == 6 || Day == 7 || Day == 8 || Day == 9) {
		Day = "0"+Day;
	}
	var fechaHoy = Hoy.getFullYear()+"-"+Month+"-"+Day;
	if (fechaHoy <= startDate) {
		if (startDate != "") {
			if(duration != 0) {			
				var endDate = addToDate(startDate, parseInt(duration));
				GetId('end_date').value = endDate;
			} else {
				GetId('end_date').value = "";
				GetId('duration').value = 0;
			}
		} else {
			alert("Please, first complete the start date.");
		}
	} else {
		GetId('end_date').value = "";
		GetId('duration').value = 0;
		alert(fechaHoy+"\n"+startDate+"\n"+"The start date cannot be lower to the date of today");
	}
}
















