/* Author: Rebekah Ocampo
   Company: Eckx Media Group
   Revised: 11/21/08 */
function countLines2(numRegularlnes, priceRegularLnes, priceAdditionalLnes, charsPerline){
		//var cost = 2;
		var charsLine = parseInt(charsPerline);
		var numRegularlines = parseInt(numRegularlnes);
		var priceRegularLines = parseFloat(priceRegularLnes);
		var priceAdditionalLines = parseFloat(priceAdditionalLnes);
		var adDesc = $('addesc').value;
		var adDescLinesArray = adDesc.split('\n');
		
		for (var k = 0; k < adDescLinesArray.length; k++){// loop through all lines
			if(adDescLinesArray[k].length > charsLine){
				var characters = adDescLinesArray[k].toArray();// this is a single line as an array of characters
				var appendStr = '';
				for (var j = 0; j < characters.length; j++){// must loop through all the characters
					if(j > charsLine){
						if(characters[j]==' '){//wait for a space then put the remaining characters to a next line
							if(j != characters.length - 1){// if there is more characters after
								for (var m = j+1; m < characters.length; m++){
									appendStr += characters[m];
								}
								if(k == (adDescLinesArray.length - 1)){// if it is the last line
									adDescLinesArray[k+1] = appendStr;
								}
								else{
									adDescLinesArray[k+1] = appendStr + ' ' + adDescLinesArray[k+1];
								}
								adDescLinesArray[k] = adDescLinesArray[k].substring(0, j);
								break;
							}
						}
					}
				}
			}
		}
		var newTestStr = adDescLinesArray.join('\n');
		$('num_of_lines').value = adDescLinesArray.length;
		
		var numExtraLines = adDescLinesArray.length - numRegularlines;
		
		var costOfAdditinalLines = 0;
		if(numExtraLines > 0){
			costOfAdditinalLines = priceAdditionalLines * numExtraLines;
		}
		
		//$('ad_cost').value = '$'+(cost*adDescLinesArray.length);
		
		var adTotCost = priceRegularLines + costOfAdditinalLines;
		$('ad_cost').value = adTotCost.toFixed(2);
		$('addesc').value = newTestStr;
		$('addesc').scrollTop = $('addesc').scrollHeight;
		/*if(submitForm==1){
			$('car_ad_form').submit();
		}*/
	
}

function refreshPictures(){
	$('picture').value = '';
	var url = window.CLIENTROOT+"index.php?/place/show_uploaded_pictures_ro";
	new Ajax.Request(url, { method: 'get',  onSuccess: function(refreshAttachment2) {
			$('attach-file-container').innerHTML = refreshAttachment2.responseText;
		}
	}); 
}
function removePicture(filename){
	var url = window.CLIENTROOT+"index.php?/place/removePictureRO/"+filename;
	new Ajax.Request(url, { method: 'get',  onSuccess: function(removePicture2) {
			//refreshPictures();
			var f = document.getElementById('pic_fram');
			f.src = f.src;
		}
	});
	
}
function changeModelsList(makeID){
	var url = window.CLIENTROOT+"index.php?/place/show_models_ro/"+makeID;
	new Ajax.Request(url, { method: 'get',  onSuccess: function(changeModelsList2) {
			$('models-container').innerHTML = changeModelsList2.responseText;
		}
	}); 
}
function changeModelsListCB(makeID){
	var url = window.CLIENTROOT+"index.php?/place/show_models_cb_ro/"+makeID;
	new Ajax.Request(url, { method: 'get',  onSuccess: function(changeModelsList2) {
			$('models-container').innerHTML = changeModelsList2.responseText;
		}
	}); 
}
function submitAdForm(){
	countLines2(1);
}
function checkSelectModel(){
	var numOfModels = $('numOfModels').value;
	var checkedOne=false;
	for(var i=0; i < numOfModels; i++){
		if($('model_'+i).checked){
			checkedOne=true;
			break;
		}
	}
	if(checkedOne==false){
		$('modelMsg').innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;At least one model is required.';
		return false;
	}
	else{
		$('search_ad_form').submit();
	}
}
function submitSearchAdForm(cat){
	$('ad_search_form').action=window.CLIENTROOT+"index.php?/search/results/"+cat+"/"+$('make').value+"/"+$('model').value+"/";
	$('ad_search_form').submit();
}
//not using
function pop_image(picID){
	var url = window.CLIENTROOT+"index.php?/search/pop_image/"+picID;
	curtain.load();
	
	new Ajax.Request(url, { method: 'get', onSuccess: function(pop_image2) {
			curtain.content(pop_image2.responseText);
			setTimeout("customTitle.init('curtain_contentLayer"+curtain.index+"')" , 100);	//set input, css, js, events handling 
		} 
	}); 
}

function showState(){
	if($('place-ad-country').value != 'US'){
		$('state-label').innerHTML = 'State / Province:';
		//$('state-label').style.backgroundColor  = "#3CF";
		$('state-container').innerHTML = '<input name="state" id="state" type="text" size="30" maxlength="50" />';
	}
	else{
		//var url = window.CLIENTROOT+"index.php?/place/show_states/"+$('state').value;
		var url = window.CLIENTROOT+"index.php?/place/show_states";
		new Ajax.Request(url, { method: 'get',  onSuccess: function(showState2) {
				$('state-label').innerHTML = 'State:';
				$('state-container').innerHTML = showState2.responseText;
			}
		}); 	
	}
}



