<?php

/**

* @version		1.1

* @package		DJ Classifieds

* @subpackage	DJ Classifieds Component

* @copyright	Copyright (C) 2010 Blue Constant Media LTD, All rights reserved.

* @license		http://www.gnu.org/licenses GNU/GPL

* @autor url    http://design-joomla.eu

* @autor email  contact@design-joomla.eu

* @Developer    Lukasz Ciastek - lukasz.ciastek@design-joomla.eu

* 

* 

* DJ Classifieds is free software: you can redistribute it and/or modify

* it under the terms of the GNU General Public License as published by

* the Free Software Foundation, either version 3 of the License, or

* (at your option) any later version.

*

* DJ Classifieds is distributed in the hope that it will be useful,

* but WITHOUT ANY WARRANTY; without even the implied warranty of

* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

* GNU General Public License for more details.

*

* You should have received a copy of the GNU General Public License

* along with DJ Classifieds. If not, see <http://www.gnu.org/licenses/>.

* 

*/

defined ('_JEXEC') or die('Restricted access');



//$getTexts = $editor->getContent('description');



jimport('joomla.media.images');



$document=& JFactory::getDocument();

//$js = JPATH_BASE.'/components/com_djclassidields/images/Moo.Form.js';

//$document->addScript($js);

JHTML::_( 'behavior.Mootools' );

require_once(dirname(__FILE__).DS.'helper.php');

$sort_list=$this->list;

$_list = new TreeNodeHelper();

$par = &JComponentHelper::getParams( 'com_djclassifieds' );

$imglimit = (int)$par->get('img_limit');



?>

<div id="dj-classifieds" class="clearfix">

<div class="dj-additem clearfix" >

<div class="title_top"><?php 

	if(JRequest::getVar('id', 0, '', 'int' )>0){

		echo JText::_('Edit Ad');

	}else{

		echo JText::_('New Ad');	

	}

?></div>

<form action="index.php" method="post" name="djForm" id="djForm"  enctype='multipart/form-data'>

    	<center><img src='<?php echo JURI::base() ?>/components/com_djclassifieds/images/long_loader.gif' alt='LOADING' style='display: none;' id='upload_loading' /><div id="alercik"></div></center>

        <table class="admintable">

            <tr>

                <td width="100" align="right" class="label" id="name_l">

                    <?php echo JText::_('Title');?>

                </td>

                <td>

                    <input class="text_area" type="text" name="name" id="name" size="50" maxlength="250" value="<?php echo $this->nl->name; ?>" />

                </td>

            </tr>

            <tr>

                <td width="100" align="right" class="label" id="category_l">

                    <?php echo JText::_('Category'); ?>					

                </td>

                <td class="category">

                    <?php

				//	if(JRequest::getVar('id', 0, '', 'int' )>0){

				//		echo $this->nl->c_name;

				//		echo '<input type="hidden" id="cat_id" option="cat_id" value="'.$this->nl->cat_id.'">'; 	

				//	}else{

							$optionss = array();

							$optionss=$_list->getSortList($sort_list,$sort_list,$this->par->get('unit_price'));

							$main_tab = array();

							$main_tab[0]= JHTML::_('select.option', '0', JText::_('Select Category'));

							$options = array();

							$options = array_merge_recursive ($main_tab, $optionss);							

							echo JHTML::_('select.genericlist', $options, 'cat_id', null, 'value', 'text', $this->nl->cat_id);					

				//	}

					?>

                </td> 

            </tr>

			<tr>

                <td width="100" align="right" class="label" id="intro_desc_l">

                    <?php 

					echo JText::_('Intro description');?>

 					<div id="ile">(<?php echo $par->get('introdesc_char_limit');?>)</div>					

                </td>

                <td>

		            <textarea id="intro_desc" name="intro_desc" rows="5" cols="55" class="inputbox" onkeyup="checkt(<?php echo $par->get('introdesc_char_limit');?>);" onkeydown="checkt(<?php echo $par->get('introdesc_char_limit');?>);"><?php echo $this->nl->intro_desc; ?></textarea>

                </td>

            </tr>



            <tr>

                <td width="100" align="right" class="label" id="description_l">

                    <?php echo JText::_('Description');?>

                </td>

                <td>

		            <textarea id="description" name="description" rows="20" cols="55" class="inputbox"><?php echo $this->nl->description; ?></textarea>                  

                </td>

            </tr>

			<?php if((int)$par->get('showcontact','1')){?>

			<tr>

                <td width="100" align="right" class="label" id="contact_l">

                    <?php echo JText::_('Contact');?>

                </td>

                <td>

		            <textarea id="contact" name="contact" rows="4" cols="55" class="inputbox"><?php echo $this->nl->contact; ?></textarea>                  

                </td>

            </tr>

			<?php }

			if((int)$par->get('showprice','1')){

			?>

			<tr>

                <td width="100" align="right" class="label" >

                    <?php echo JText::_('Price');?>

                </td>

                <td>

                    <input class="text_area" type="text" name="price" id="price" size="50" maxlength="250" value="<?php echo $this->nl->price; ?>" />

                </td>

            </tr>

			<?php } ?>

			<?php

			if(JRequest::getVar('id', 0, '', 'int' )>0){

			?>		

				<tr>

                <td width="100" align="right" class="label">

                    <?php echo JText::_('Images');?>

                </td>

                <td>

			<?php

				$images_count = 0;

				$images = array();

				if(!$image = $this->nl->image_url){

					echo JText::_('No images included');

				}else{

					echo '<input type="hidden" name="image_url" value="'.$this->nl->image_url.'" />';

					$images=explode(';', substr($image,0,-1));

					for($i=0; $i<count($images); $i++){

						?>

						<?php $sciezka = str_replace('/administrator','',JURI::base());

					      $sciezka .= '/components/com_djclassifieds/images/';

						  $sciezka .= $images[$i];

						  ?>

						  <img src="<?php echo $sciezka;?>.th.jpg"/>

						  <input type="checkbox" name="usun[]" id="usun[]" value="<?php echo $images[$i];?>"/>

						  <?php echo JText::_('Check to delete'); ?>

						  <br/>

						<?php

					}

					echo '<input type="hidden" id="count_images" value="'.count($images).'">';

				}

				?>

				</td></tr>

				<?php

			}

			

			?>

 			<tr>

                <td width="100" align="right" class="label">

                    <?php echo JText::_('Add image');

					$img_maxsize = $par->get('img_maxsize',0);	

					if($img_maxsize>0){

						echo '<br />'.JText::_('Max image size').': '.$img_maxsize.' MB';

					}

					?>

                </td>

                <td>

                    <?php $image_urls = ""?>

					<div id="uploader">

					<input type="file"  name="image[]" class="inputbox" />

					<?php if($imglimit>1){ ?>

						</div><a href="#" onclick="addImage(<?php echo $imglimit;?>); return false;" ><?php echo JText::_('Add next image')?></a>

					<?php }?>

                </td>

            </tr>

 		 <?php

			if($this->par->get('promotion')=='1' || $this->nl->special==1){ ?>				

			<tr>

                <td width="100" align="right" class="label">

                	<?php $prom_price = $this->par->get('prom_price');

                    echo JText::_('Promotion first').'<br />'.JText::_('price').'&nbsp;'.$prom_price.'&nbsp; '.$this->par->get('unit_price');;?>

					

                </td>

				<td>

					<input type="radio" name="special" value="1" <?php  if($this->nl->special==1){echo "checked";}?> /><?php echo JText::_('Yes'); ?>

					<input type="radio" name="special" value="0" <?php  if($this->nl->special==0){echo "checked";}?> /><?php echo JText::_('No'); ?>	

				</td>	

			</tr>

		 <?php } ?>			

        </table>

		<label id="verification_alert" class="invalid" style="display:none; text-align:center;" /><br /></label>

    <button class="button2" type="button" id="submit_b" onclick="submit_checkt();"><?php echo JText::_('Save'); ?></button>

	<input type="hidden" name="option" value="<?php echo $option;?>" />

	<?php

	if(JRequest::getVar('id', 0, '', 'int' )>0){

		echo '<input type="hidden" name="view" value="saveEditItem" />';

		echo '<input type="hidden" name="id" value="'.$this->nl->id.'" />';

		//echo '<input type="hidden" name="cat_id" value="'.$this->nl->cat_id.'" />';

	}else{

		echo '<input type="hidden" name="view" value="saveItem" />';

	} 

	?>

	<input type="hidden" name="boxchecked" value="0" />

</form>

</div>

</div>

<script type="text/javascript">	

	

function addImage(imglimit){



	lim=document.djForm['image[]'].length;

	if(!lim){

		lim=1;

	}

	

	if(document.djForm['usun[]']){

		lim_old=document.djForm['usun[]'].length;

		if(!lim_old){

			lim_old=1;

		}

		lim = lim + lim_old;	

	}

	

	

	if(lim==imglimit){

		alert('<?php echo JText::_('Maximum number of images is');?> '+imglimit);

	}else{

		var inputdiv = document.createElement('input');

		inputdiv.setAttribute('name','image[]');

		inputdiv.setAttribute('type','file');



		var ni = $('uploader');

	

		ni.appendChild(document.createElement('br'))

		ni.appendChild(inputdiv);		

	}



}



function submit_checkt_old(){



	if(document.getElementById("name").value==''){

		alert("<?php echo JText::_('Please enter title');?>");

	}else if(document.getElementById("cat_id").value=='0'){

		alert("<?php echo JText::_('Please select category');?>");

	}else if(document.getElementById("intro_desc").value==''){

		alert("<?php echo JText::_('Please enter description intro');?>");

	}else if(document.getElementById("description").value==''){

		alert("<?php echo JText::_('Please enter description');?>");

	}else if(document.getElementById("contact").value==''){

		alert("<?php echo JText::_('Please enter contact');?>");

	}else{

	document.djForm.submit();		

	}



}



function submit_checkt(){

	

	var pass = 1;





	if (document.getElementById("name").value == '') {

	//	alert("<?php echo JText::_('Please enter title');?>");

		$("name_l").className = "invalid";

		pass=0;

	}else{

		$("name_l").className = "";

	}



		

	if (document.getElementById("cat_id").value == 0) {

		//alert("<?php echo JText::_('Please select category');?>");

		$("category_l").className = "invalid";

		pass=0;

	}else {

		$("category_l").className = "";

	}

	if (document.getElementById("intro_desc").value == '') {

		//alert("<?php echo JText::_('Please enter description intro');?>");

		$("intro_desc_l").className = "invalid";

		pass=0;

	}else {

		$("intro_desc_l").className = "";

	}

	

	if (document.getElementById("description").value == '') {

		//alert("<?php echo JText::_('Please enter description intro');?>");

		$("description_l").className = "invalid";

		pass=0;

	}else {

		$("description_l").className = "";

	}

	<?php if((int)$par->get('showcontact','1')){?>

	if (document.getElementById("contact").value == '') {		

		$("contact_l").className = "invalid";

		pass=0;

	}else {

		$("contact_l").className = "";

	}

	<?php } ?>

		

	if(pass==0){

		$('verification_alert').innerHTML = "<?php echo JText::_('Please enter all values'); ?>";

		$('verification_alert').setStyle('display', 'block');

		setTimeout("$('verification_alert').setStyle('display', 'none');",2000);

	}else{

		$('upload_loading').setStyle('display', 'block');

		$('alercik').innerHTML = "<?php echo JText::_('Saving, please wait');?>";

		document.djForm.submit();

	}



}



	

function checkt(limit){

if(document.djForm.intro_desc.value.length<=limit)

{

	a=document.djForm.intro_desc.value.length;

	b=limit;

	c=b-a;

	document.getElementById('ile').innerHTML= '('+c+')';

}

else

{

	document.djForm.intro_desc.value = document.djForm.intro_desc.value.substring(0, limit);

}

}

checkt(<?php echo $par->get('introdesc_char_limit');?>);



</script>