<?php

//print_r($_REQUEST);

$mainCat = $_GET["mainCat"];
$optionFor = $_GET["optionFor"];

if(empty($mainCat) || empty($optionFor)){
	include("postPop.php");
	exit;
}

include("includes/site_info.php");
include('session_handler.inc');

$WUserID = $_SESSION['Sess_UserId'];
$WUsername = $_SESSION['Sess_UserName'];

$member_query=mysql_query("SELECT adv_countryid FROM adverts LEFT JOIN members ON (adv_userid=mem_userid) WHERE adv_userid = '$WUserID' AND adv_approved='1'") or die(mysql_error());
$sql_array = mysql_fetch_object($member_query);
?>
<link type="text/css" href="jquery-ui-1.8.11.custom.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-ui-1.8.11.custom.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-timepicker-addon.js"></script>
<script type="text/javascript" src="js/ajaxupload.js"></script>
<script type="text/javascript">
	
jQuery(document).ready(function($){

$("#tellmore").live('click', function() {
$("#extraInfo").html('<img src="images/loader1.gif" border="0" align="absmiddle" style="padding-left:100px;">');
$("#extraInfo").load("postAction.php?Mode=getExtraInfo", {forID: <?=$optionFor?> }, function(){
		$('.datetimepicker > pre').each(function(i){
			eval($(this).text());
		});
	});

$("#tellmore").die('click');
});

$("#subCat").live('change', function()
{
var id=$(this).val();

$("#msg_content_error2").fadeOut('slow');
$('#msg_content_error2').html('');

$("#subSelectsub").html('<img src="images/loader1.gif" border="0" align="absmiddle">');

$.getJSON('postAction.php?Mode=getsubSelect', { dataID: id }, function(html) {
	if (html.status=="1") {
		$("#subSelectsub").html(html.subSelect);
	} else {
		$("#subSelectsub").html(html.error);
	}
});

});

$("span.remove").live('click', function() {
	var targetid = $(this).attr('id').replace("remove", "");
	$('img#thumb'+targetid).attr('src','images/upimage.jpg');
	$('input#imagefield'+targetid).remove();
	$('input#imagefield'+targetid+'med').remove();
	$('input#imagefield'+targetid+'ori').remove();
	$('span#remove'+targetid).remove();
});

var id = 1;

$('.thumb').each(
   function() { 
     $(this).attr('id', 'thumb' + id); 
	id++;

	new AjaxUpload($(this).attr('id'), {
		action: 'postAction.php',
		name: 'image'+$(this).attr('id'), // name: 'image'+$(this).attr('id'),
		responseType: 'json',
		data: {Mode : 'uploadPic', userid : '<?=$WUserID?>', fieldname: 'image'+$(this).attr('id')},
		onSubmit: function(file, extension) {
		
			var targetid = this._settings.name.replace("imagethumb", "");
		
			if (!(extension && /^(jpg|png|jpeg|gif)$/i.test(extension))){
                        // extension is not allowed
                        alert('Error: invalid file extension');
                        // cancel upload
                        return false;
                }
			else {
				
				$('img#thumb'+targetid).closest('div.preview').addClass('loading');
			}
		},
		onComplete: function(file, response) {
//			alert(response);
			
	  		if(response.status == '1') { 

			var targetid = this._settings.name.replace("imagethumb", "");
			
			$('img#thumb'+targetid).load(function(){
				$('img#thumb'+targetid).closest('div.preview').removeClass('loading');
				$('img#thumb'+targetid).unbind();
				$('img#thumb'+targetid).closest('div.preview').append('<span class="remove" id="remove'+targetid+'"></span>');
			});
			$('img#thumb'+targetid).attr('src', response.thumb);
			$('input#imagefield'+targetid).remove();
			$('input#imagefield'+targetid+'med').remove();
			$('input#imagefield'+targetid+'ori').remove();
			$('img#thumb'+targetid).after('<input type="hidden" name="imagefield'+targetid+'" id="imagefield'+targetid+'" value="'+response.thumb+'" /><input type="hidden" name="imagefield'+targetid+'med" id="imagefield'+targetid+'med" value="'+response.med+'" /><input type="hidden" name="imagefield'+targetid+'ori" id="imagefield'+targetid+'ori" value="'+response.ori+'" />');
			
			} else {
		$("#msg_content_error2").fadeIn('slow');
	 	$('#msg_content_error2').html('Ops! Error, please try again!');
		if ($.isArray(response.error)==true){
			$.each(response.error, function(key, value) { 
 			$('#msg_content_error2').append('<br>'+value);
				});
		} else {
 			$('#msg_content_error2').append('<br>'+response.error);
		}
			}
			
		}
	});
});	

var active_color = '#000'; // Colour of user provided text
var inactive_color = '#999'; // Colour of default text

  var default_values = new Array();
  $(":input.lightgrey2").focus(function() {
    if (!default_values[this.id]) {
      default_values[this.id] = this.value;
    }
    if (this.value == default_values[this.id]) {
      this.value = '';
      this.style.color = active_color;
    }
    $(this).blur(function() {
      if (this.value == '') {
        this.style.color = inactive_color;
        this.value = default_values[this.id];
      }
    });
  });
  
  $(":input.sub").live('click', function() {
    if (!default_values[this.id]) {
      default_values[this.id] = this.value;
    }
    if (this.value == default_values[this.id]) {
      this.value = '';
      this.style.color = active_color;
    }
    $(this).blur(function() {
      if (this.value == '') {
        this.style.color = inactive_color;
        this.value = default_values[this.id];
      }
    });
  });

    $("#addPost2").bind('submit', function() {

	 	$('#msg_content_error2').html('');

 	if ($(":input.required").val().length > 0) {
      var isValid;
		$(":input.required").each(function (index) {
    if (this.value.match(/(What|Describe it)/i)) {
		$("#msg_content_error2").fadeIn('slow');
	 	$('#msg_content_error2').append('Ops! Please fill in ' + $(this).attr('rel') + '!<br>');
      isValid = false;
    }

	if ($(":input.required").val().length < 1)
    {
		$("#msg_content_error2").fadeIn('slow');
	 	$('#msg_content_error2').append('Ops! Please fill in ' + $(this).attr('rel') + ' with correct data!<br>');
      isValid = false;
    }
	 });
	if (isValid == false) {
      return isValid;
	}
   }

 	if ($(":input.required:checked").length == 0) {
	 
		$("#msg_content_error2").fadeIn('slow');
	 	$('#msg_content_error2').append('Ops! Please choose Option for Category!<br>');
      return false;

    }

 	if ($("select.required")) {
      var isValid;
		$("select.required").each(function (index) {
//	 	$('#msg_content_error2').append('Ops! Please select ' + $(this).val() + '!<br>');
		if ($(this).val()== 0) {
		$("#msg_content_error2").fadeIn('slow');
	 	$('#msg_content_error2').append('Ops! Please select ' + $(this).attr('rel') + '!<br>');
      isValid = false;
		}
	 });
	if (isValid == false) {
      return isValid;
	}
    }


 	if ($(":input.price").val().match(/[^0-9.]/)) {
	 
		$("#msg_content_error2").fadeIn('slow');
	 	$('#msg_content_error2').append('Ops! Limit to digits only.<br>');
      return false;

    }

	 $('#msg_content_error2').hide();
	$('input[type=submit]').attr("disabled","disabled");

	var str = 'Mode=putInfo&' + $(this).serialize();
	var that = this;

   		$.ajax({
   		type: "POST",
   		url: "postAction.php",
   		data: str,
			dataType: "json",
   		success: function(result){
		$('input[type=submit]').removeAttr("disabled");

 		if(result.status == '1') { 
		$(':input','#addPost2')
		 .not(':button, :submit, :reset, :hidden')
		 .val('')
		 .removeAttr('checked')
		 .removeAttr('selected');

// 		$("#msg_content_error2").fadeIn('slow');
//	 	$('#msg_content_error2').html('Ops! Done!');
//			$('#msg_content_error2').append('<br>'+result.success);
		parent.jQuery.facebox.close();
		window.location.href='<?=$CONST_LINK_ROOT?>/home.php#cshowPost?tab=active&postID='+result.postID;
		} else {
		$("#msg_content_error2").fadeIn('slow');
	 	$('#msg_content_error2').html('Ops! Error, please try again!');
		if ($.isArray(result.error)==true){
			$.each(result.error, function(key, value) { 
 			$('#msg_content_error2').append('<br>'+value);
				});
		} else {
// 			$('#msg_content_error2').append('<br>'+result);
 			$('#msg_content_error2').append('<br>'+result.error);
		}
		}
		}

 });
return false;

    });
	 
});

</script>
<table width="650" border="0" cellpadding="0" cellspacing="0">
	<tr>
		<td valign="top"><div id="msg_content_error2" style="display:none;" class="msg_content_error"></div>
			<div id="sub_content">
				<form id="addPost2" name="addPost2" method="post" style="padding:0; margin:0; border:0;">
					<input name="userid" type="hidden" value="<?=$WUserID?>" />
					<input name="mainCat" type="hidden" value="<?=$mainCat?>" />
					<input name="optionFor" type="hidden" value="<?=$optionFor?>" />
					<table width="100%" border="0" cellspacing="0" cellpadding="0">
						<tr>
							<td><table width="100%" border="0" cellspacing="0" cellpadding="5" class="white_back_t_xb">
									<tr>
										<td valign="top"><div class="preview"><img class="thumb" width="100" height="100" src="images/upimage.jpg" /></div>
											<div class="preview"><img class="thumb" width="100" height="100" src="images/upimage.jpg" /></div>
											<div class="preview"><img class="thumb" width="100" height="100" src="images/upimage.jpg" /></div></td>
										<td width="100%" valign="top"><div id="markPost">
												<p>
													<label for="subCat" class="label"> Subcategory:* </label>
													<span id="subSelect">
													<select name="subCat" id="subCat" class="select required" rel="Subcategory">
														<option value="0">Select a Subcategory</option>
														<?php
																			$sql=mysql_query("select dataID, dataName from market_category_data where catID=$mainCat and parent=0 order by dataName asc");
																			while($row=mysql_fetch_array($sql))
																			{
																			$id=$row['dataID'];
																			$data=$row['dataName'];
																			echo '<option value="'.$id.'">'.$data.'</option>';
																			 } ?>
													</select>
													</span> <span id="subSelectsub"> </span> </p>
												<p>
													<label for="title" class="label">Title:* </label>
													<input name="title" type="text" class="lightgrey2 input required" id="title" value="What you provide/request?" maxlength="255" rel="Title" />
												</p>
<!--												<p>
													<label for="why" class="label">Why: </label>
													<input name="why" type="text" id="why" class="lightgrey2 input" value="What purpose?" />
												</p>
-->												<p>
													<label for="location" class="label">Service Location: </label>
													<textarea name="location" class="lightgrey2 textarea" id="location">Details on your service location, exp:- by City & State or Zip or Country</textarea>
												</p>
												<p>
													<label class="label">&nbsp;</label>
													<select name="Country" id="Country" size="1" class="select required" rel="Country">
														<option value="0">Select a Country</option>
														<?php
												$countryr=mysql_query("select * from Geo_Countries order by Country");
												while($rst= mysql_fetch_array($countryr))
												{
												?>
														<option value="<?php echo $rst["CountryId"];?>""<?php if($rst["CountryId"]==$sql_array->adv_countryid){ echo " selected='selected'";}?>" ><?php echo $rst["Country"];?></option>
														<?php
												}
												?>
													</select>
												</p>
												<p>
													<label for="description" class="label"> Description:* </label>
													<textarea name="description" id="description" class="lightgrey2 textarea required" rel="Description" >Describe it!</textarea>
												</p>
												<div id='oanda_ecc'> <!-- IF YOU ALTER OR REMOVE THE FOLLOWING CODE, THE EMBEDDED WIDGET WILL NOT WORK. --><span style='color:#000; text-decoration:none; font-size:9px;'><a href='http://www.oanda.com/currency/converter/' id='oanda_cc_link'>Currency Converter</a> by OANDA</span><script src='http://www.oanda.com/embedded/converter/get/b2thenVraW4vL3dhdWhhaGE=/?lang=en'></script></div> 
												<div id="extraInfo">
													<?php
													switch ($optionFor) {
	case 1; 
	?>
													<p>
														<label for="price" class="label">Price: </label>
														<input name="price" type="text" id="price" maxlength="100" class="price" /><span class="inputPrice">US$</span>
													</p>
													<?php
		break;
	case ($optionFor <= 4); 
		break;
	case ($optionFor <= 6); 
	?>
													<p>
														<label for="description" class="label">Extra Info</label>
														<input name="tellmore" type="text" readonly class="input" id="tellmore" value="Tells more" maxlength="255" />
													</p>
													<?php
		break;
	case 7; 
		break;
	case 10; 
	?>
													<p>
														<label for="price" class="label">Price: </label>
														<input name="price" type="text" id="price" maxlength="100" class="price" /><span class="inputPrice">US$</span>
													</p>
													<?php
		break;
	case 11; 
	?>
													<p>
														<label for="description" class="label">Extra Info</label>
														<input name="tellmore" type="text" readonly class="input" id="tellmore" value="Tells more" maxlength="255" />
													</p>
													<?php
		break;
	case 12; 
		break;
	case 13; 
	?>
													<p>
														<label for="price" class="label">Price: </label>
														<input name="price" type="text" id="price" maxlength="100" class="price" /><span class="inputPrice">US$</span>
													</p>
													<?php
		break;
	case 14; 
		break;
	case ($optionFor <= 16); 
	?>
													<p>
														<label for="description" class="label">Extra Info</label>
														<input name="tellmore" type="text" readonly class="input" id="tellmore" value="Tells more" maxlength="255" />
													</p>
													<?php
		break;
	case 17; 
		break;
	case ($optionFor <= 19); 
	?>
													<p>
														<label for="description" class="label">Extra Info</label>
														<input name="tellmore" type="text" readonly class="input" id="tellmore" value="Tells more" maxlength="255" />
													</p>
													<?php
		break;
	case ($optionFor <= 20); 
		break;
	}

												?>
												</div>
											</div></td>
									</tr>
								</table></td>
						</tr>
						<tr>
							<td align="center" class="left solid_line"><input name="cancel" type="button" class="button" id="cancel" value="Cancel" onclick="javascript: jQuery.facebox.close('newPost2');"/>
								<input name="reset" type="reset" class="button" id="reset" value="Reset" />
								<input name="save" type="submit" class="submit" id="save" value="Proceed"/></td>
						</tr>
					</table>
				</form>
			</div></td>
	</tr>
</table>
