$(document).ready(function(){
	$.each($("span[rel='out_stock']"), function (){

		$(this).click(function(e){
			//1.change the os_req_pcode
			var a = $(this).attr('id').split('^');
			var productid=a[0];
			var productcode = a[1];
			var productname = a[2];
		
			//2.track the position of the out of stock image
			//turn out the light
			$('.g_pname').html(productname);
			$('#os_req_pcode').val(productcode);
			$('#shadow').css({'width':$(document).width(), 'height':$(document).height()}).show();
			$('#shadow').click(function(e){
				$(this).hide();
				$('#popup_layer').css({'visibility':'hidden', 'display':'none'});
			});

			$('#popup_layer').css({'left':parseInt(e.pageX) - 500, 'top':e.pageY,'visibility':'visible','display':'block'});
			//3.show the form

			//4.check if everything's filled in.
			$('.out_stock_btn').click(function(e){

				e.preventDefault();

				var request_name	= $('#os_req_name').val();
				var os_req_contact	= $('#os_req_contact').val();
				var os_req_email	= $('#os_req_email').val();
				var os_req_pcode	= $('#os_req_pcode').val();
				if(request_name==''){
					alert('Please key in your name.');
					$('#os_req_name').focus();
					return false;
				}else{
					if(os_req_contact == ''){
						alert('Please key in your contact.');
						$('#os_req_contact').focus();
						return false;
					}else{
						if(os_req_email ==''){
							alert('Please key in your e-mail.');
							$('#os_req_email').focus();
							return false;
						}
					}
				}

				if(os_req_contact || request_name || os_req_email){
					$.post('ajax/out-stock-request.php', {pcode:os_req_pcode, email:os_req_email, contact: os_req_contact, name: request_name} , function(d){
						$('#request_message').html('');
						$('#request_message').html(d);
					});
				}
			});

			$('#close').click(function(e){
				e.preventDefault();
				$('#shadow').hide();
				$('#popup_layer').css({'visibility':'hidden', 'display':'none'});
				$('#request_message').html(
'<form name="request_form"><span style="font-size:16px;font-weight:bold;color:green;">Inform me, please</span><br/><p>Dear customer, kindly fill in your name, contact number and email, so we can inform you when the new stock of this product arrive.</p><table class="boxtable" border="0" cellspacing="1" cellpadding="0" width="100%" ><tr><td>Product : </td><td><span class="g_pname"><?=$pname?></span><input type="hidden" id="os_req_pcode" name="os_req_pcode" value="<?=$pcode?>" class="inputbox"></td></tr>  <tr><td>Your name : </td><td><input type="text" name="os_req_name" id="os_req_name" value="" class="inputbox"></td></tr><tr><td>Your contact : </td><td><input type="text" name="os_req_contact" id="os_req_contact" value="" class="inputbox"></td></tr><tr><td>Your email address : </td><td><input type="text" name="os_req_email" id="os_req_email" value="" class="inputbox"></td></tr><tr><td colspan="2" align="center"><input type="button" name="submit" value="submit" class="button out_stock_btn" ></td></tr></table></form>'
				);
			});



		});//end click of each out of stock object

	});
});
