// JavaScript Document

function chkValidate(){
	$("#requestcat").validationEngine();
}

function chkPassword(){
	
	var password = $("#password").val();
	var confirmPass = $("#confirm").val();
	
	if (password != confirmPass){
		
			$("#chkPassword").html('<img src="images/delete.png" /> Passwrod miss match');
			$("#chkPass").val('0');
			return 0;
		
	}else{
			$("#chkPass").val('1');
			$("#chkPassword").empty();
			return 1;
			
	}
	
	
	
}

function showFillter(filterIn) {
		
		var filter = '<ul class="fillterIn">TYPE<br /><form id="typeFiller"><input name="types" type="checkbox" value="type=1" onclick="typeFilter();"/>&nbsp;upper-body clothes<br /><input name="types" type="checkbox" value="type=2" onclick="typeFilter();"/>&nbsp;lower-body clothes<br /></form></ul><ul class="fillterIn">LENGTH<br /><form id="length"><input name="length" type="checkbox" value="lengths=1" onclick="lengthFilter();"/>&nbsp;long&nbsp;&nbsp;&nbsp;&nbsp;<input name="length" type="checkbox" value="lengths=2" onclick="lengthFilter();"/>&nbsp;short<br /></form></ul><ul class="fillterIn">PRICE<br /><form id="price"><input name="price" type="checkbox" value="price<20" onclick="priceFilter();"/>&nbsp;< $20.00<br /><input name="price" type="checkbox" value="price between 20.01 and 50.00" onclick="priceFilter();"/>&nbsp;&nbsp;&nbsp;&nbsp;$20.01 - $50.00<br /><input name="price" type="checkbox" value="price between 50.01 and 100.00" onclick="priceFilter();"/>&nbsp;&nbsp;&nbsp;&nbsp;$50.01 - $100.00<br /><input name="price" type="checkbox" value="price>100"  onclick="priceFilter();"/>&nbsp;> $100<br /><input name="filterIn" id="filterIn" type="hidden" value=""/></form><br /></ul>';
			
			$("#man").empty();
			$("#women").empty();
			$("#youth").empty();
			//$(filterIn).html(filter);		
			$("#"+filterIn).html(filter);
			$("#gender").val(filterIn);
			viewCatalog()
			pageProduct(1);			
					
}

function priceFilter(){
	
		$("#curPage").val(1);
		viewCatalog()
		pageProduct(1);
	
}

function priceReset(){
	
	var priceForm = '<input name="price" type="checkbox" value="price<20" onclick="priceFilter();"/>&nbsp;< $20.00<br /><input name="price" type="checkbox" value="price between 20.01 and 50.00" onclick="priceFilter();"/>&nbsp;&nbsp;&nbsp;&nbsp;$20.01 - $50.00<br /><input name="price" type="checkbox" value="price between 50.01 and 100.00" onclick="priceFilter();"/>&nbsp;&nbsp;&nbsp;&nbsp;$50.01 - $100.00<br /><input name="price" type="checkbox" value="price>100"  onclick="priceFilter();"/>&nbsp;> $100<br />';
	
	$("#price").html(priceForm);
	

}

function typeFilter(){


			$("#curPage").val(1);
			viewCatalog()
			pageProduct(1);

}

function typeReset(){
	
	var typeForm = '<input name="types" type="checkbox" value="type=1" onclick="typeFilter();"/>&nbsp;upper-body clothes<br /><input name="types" type="checkbox" value="type=2" onclick="typeFilter();"/>&nbsp;lower-body clothes<br />';
	
	$("#typeFiller").html(typeForm);
	

}


function lengthFilter(){
	
		$("#curPage").val(1);
		viewCatalog()
		pageProduct(1);
	
}

function lengthReset(){
	
	var lengthForm = '<input name="length" type="checkbox" value="lengths=1" onclick="lengthFilter();"/>&nbsp;long&nbsp;&nbsp;&nbsp;&nbsp;<input name="length" type="checkbox" value="lengths=2" onclick="lengthFilter();"/>&nbsp;short<br />';
	
	$("#length").html(lengthForm);
	

}

function viewCatalog() {
	
		
		
		var condition = '';
		var price = '';
		var types = '';
		var length = '';
		var group = $("#next").val();
		var gender = $("#gender").val();
		var selectCate = $("#selectCate").val();
		var page = $("#curPage").val();
		
		if(gender=='man'){
			
			gender=1;
			
		}else if(gender=='women'){
			
			gender=2;
		
		}else if(gender=='youth'){
			
			gender=3;
			
		}


		var price=$("#price").val();	
		
	    var types = $("#types").val();
		
		var length=$("#length").val();	

				
		if(price!=''){
			condition += price;	
		}
		
		if(types != '' && condition != ''){
		
			condition += ' and ' + types
			
		}else if(types != '' && condition ==''){
		
			condition = types;
			
		}
		
		if(length != '' && condition != ''){
		
			condition += ' and ' + length;
		
		}else if(length != '' && condition ==''){
		
			condition = length;
			
		}
		
		if(gender!=''){
			if(condition !=''){
			
				condition += ' and gender = ' + gender;
				
			}else{
				
				condition = 'gender = ' + gender;
			}
		}else{
		
			condition = '';
		
		}

		$("#notificationsLoader").html('<img src="images/loader.gif" />');	
		
		$("#sqlCon").val(condition);
		//var page = $("#curPage").val();
		
		$(this).remove();
	  		$.ajax({  
				type: "POST",  
				url: "libs/viewCatalog.php",
				data: {sqlCon: condition, category: selectCate, curPage: page, next: group},  
				success: function(theResponse) {
							
						
						$(".column").html(theResponse);
						
						$(window).resize(function () {
            					smartColumnsAds();
								smartColumns();
            
        				}); 
						smartColumns();	
						
						$("#notificationsLoader").empty();		

							
	
				}
			});  

}



function pagePre(){
	
	//var nextPage = Number($("#curPage").val());
	//nextPage = nextPage+1;
	var curNext = Number($("#next").val());
	var curPage = Number($("#curPage").val());
	$("#next").val(curNext-1);
	$("#curPage").val(curPage-3);
	viewCatalog(curPage-3);
	//pageProduct(nextPage);
	
	
}

function pageNext(){
	
	//var nextPage = Number($("#curPage").val());
	//nextPage = nextPage+1;
	var curNext = Number($("#next").val());
	$("#next").val(curNext+1);
	$("#curPage").val((curNext*3)+1);
	viewCatalog((curNext*3)+1);
	//pageProduct(nextPage);
	
	
}

function pageProduct(){

	
	var curPage = Number($("#curPage").val());	
	var group = Number($("#next").val());	
	var countData = '';

	countData = Number($("#countData").val());
	
	var pages = countData/12 ;
	if ((pages*12) < countData){ 
		pages++; 
	}
	
	
		$("#prePage").hide();
		$("#nextPage").hide();
		
	
	if(group<pages){
		
		$("#nextPage").show();
		//$("#prePage").show();
		
	}	
	
	if(group<pages && group>1){
		
		$("#prePage").show();
		$("#nextPage").show();
		
	}		
	
//	if (group > 1 && group!=0){
//		
//		$("#prePage").show();
//		$("#nextPage").show();
//		
//	}
//	
//	if (group < pages && group > 1){
//		
//		$("#prePage").show();
//		$("#nextPage").hide();
//		
//	}	
	
	var writePage='';
	var start = Number(group)-1;
		start = start*3;
		start = start+1;
	//var end = Number(curPage)+2;
	
	
	//if(curPage>0 && curPage<=3){
		
	var chkNext=0;
		
		if(pages>1){
	
			for(i=0;i<3 && i<=pages;i++){
				
				
					if ((start+i)==curPage){
							
						writePage +='<a onclick="viewCatalog('+(start+i)+');"><div class="currentPage">'+(start+i)+'</div>';
					
					}else{
						
						writePage +='<a onclick="viewCatalog('+(start+i)+');"><div class="pageNav">'+(start+i)+'</div>';
						
					}
					
				chkNext=i;
					
			}
		}
	
	if (Number(chkNext)>1){
		
		$("#nextPage").hide();
		$("#prePage").show();
	
	}
		
//		if ((start+1)==curPage){
//				
//			writePage +='<a onclick="viewCatalog('+(start+1)+');"><div class="currentPage">'+(start+1)+'</div>';
//		
//		}else{
//			
//			writePage +='<a onclick="viewCatalog('+(start+1)+');"><div class="pageNav">'+(start+1)+'</div>';
//			
//		}		
//		
//		
//		if ((start+2)==curPage){
//				
//			writePage +='<a onclick="viewCatalog('+(start+2)+');"><div class="currentPage">'+(start+2)+'</div>';
//		
//		}else{
//			
//			writePage +='<a onclick="viewCatalog('+(start+2)+');"><div class="pageNav">'+(start+2)+'</div>';
//			
//		}	
	
	
	$(".pager").html(writePage);

}

function checkUser() {
		
		
		
	var emailUser = $("#email").val();
		$(this).remove();
		$("#notificationsLoader").html('<img src="images/checkUser.gif" />');	
	  		$.ajax({  
				type: "POST",  
				url: "libs/checkUser.php",  
				data: {email: emailUser},  
				success: function(theResponse) {
					
					$("#notificationsLoader").empty();	
					
					if(theResponse!="sucess"){						
						
						$("#notificationsLoader").html('<img src="images/delete.png" /> This e-mail is already taken');	
						$("#chkMail").val('0');
						
							
					}else{
						
						$("#chkMail").val('1');
					
					}
					
					
	
				}
			});  

}

function userLogin() {
		
	var uname = $("#email").val();
	var upws = $("#password").val();
 			
		$(this).remove();
	  		$.ajax({  
				type: "POST",  
				url: "libs/checkLogin.php",  
				data: { email: uname, password: upws},  
				success: function(theResponse) {
							
					if(theResponse=="sucess"){
	
						goto('Profile.php');
						//hideLogin();
							
					}else{
						
						
						alert('Login fail. Please checking  your username or password');
							
					}
	
				}
			});  

}

	
function hideLogin() {
	
	$("#login").hide();
	
	
}



function getColor(id) {
		

		
		$(this).remove();
	  		$.ajax({  
				type: "POST",  
				url: "libs/getColor.php",
				data: { id: id, typeName: $("#type option:selected").val()},  
				success: function(theResponse) {
							
						
						$("#listColor").html(theResponse);
							
	
				}
			});  

}


function setColor(html, id, icon) {
		
	$("#polo").css({'background-color' : html});
		
	if($("#position").val()=="0"){
		
		$("#collarC").val(id);
		$("#sleeveC").val(id);
		$("#bodyC").val(id);
		$("#collarColor").html('<img id = "collarColor" src="images/color/'+ icon + '" alt="" width="25" height="10">');
		$("#sleeveColor").html('<img id = "collarColor" src="images/color/'+ icon + '" alt="" width="25" height="10">');
		$("#bodyColor").html('<img id = "collarColor" src="images/color/'+ icon + '" alt="" width="25" height="10">');

	}
	
	if($("#position").val()=="1"){
		
		$("#collarC").val(id);
		$("#collarColor").html('<img id = "collarColor" src="images/color/'+ icon + '" alt="" width="25" height="10">');

	}
	
	if($("#position").val()=="2"){
		
		$("#sleeveC").val(id);
		$("#sleeveColor").html('<img id = "collarColor" src="images/color/'+ icon + '" alt="" width="25" height="10">');

	}
	
	if($("#position").val()=="3"){
		
		$("#bodyC").val(id);
		$("#bodyColor").html('<img id = "collarColor" src="images/color/'+ icon + '" alt="" width="25" height="10">');

	}	

	
}

function chooseProduct(product_color, codecolor, image, product_id){
	
	var color = codecolor.split("_");
	var lengthColor = color.length;
	var i=0;
	var positionText = "";
	var type="";
	
//		for(i=0;i<lengthColor;i++){
//			
//			var type = color[i].split("=");
//			positionText += '<tr><td width="74">Position ' + (i+1) + ' :</td><td width="35" valign="middle"><div style="display:block;border:1px solid #999999; width:25px; height:10px; position:relative; float:left;"><img src="images/color/'+color[i]+'.jpg" alt="" width="25" height="10"></div></td><td width="45">Type :</td><td width="156">' + type[1] + '</td></tr>'; 
//		
//		}                                                 	   
		
	//$("#pattren").html(positionText);
	
	$("#productColor").val(product_color);
	$("#codeColor").val(codecolor);	
	

		
}

function setType() {
		
		
	if($("#position option:selected").val()=="0"){
		
		$("#collarT").val($("#type option:selected").val());
		$("#sleeveT").val($("#type option:selected").val());
		$("#bodyT").val($("#type option:selected").val());
		$("#collarType").text('Type : ' + $("#type option:selected").text());
		$("#sleeveType").text('Type : ' + $("#type option:selected").text());
		$("#bodyType").text('Type : ' + $("#type option:selected").text());

	}
	
	if($("#position option:selected").val()=="1"){
		
		$("#collarT").val($("#type option:selected").val());
		$("#collarType").text('Type : ' + $("#type option:selected").text());

	}
	
	if($("#position option:selected").val()=="2"){
		
		$("#sleeveT").val($("#type option:selected").val());
		$("#sleeveType").text('Type : ' + $("#type option:selected").text());

	}
	
	if($("#position option:selected").val()=="3"){
		
		$("#bodyT").val($("#type option:selected").val());
		$("#bodyType").text('Type : ' + $("#type option:selected").text());

	}	

	
}


function setSize() {		
			
		$("#sizeText").text('Size : ' + $("#size option:selected").text());
	
}

function setStyle() {		
			
		$("#styleText").text('Style : ' + $("#style option:selected").text());
	
}

function alertVal(id){
	
		//alert('a');
		alert(id);

	
}





function submitForm(id){
	
		
		$(id).submit();
	
}

function goto(url){

		window.location.href=url;
}

function yourStyle(html) {
		

		$("#polo").css({'background-color' : html});
	
}


function checkOut(id){
	
	$("#action").val('checkout');
	$(id).submit();
		
}

function getZone() {
		
		$(this).remove();
		
	  		$.ajax({  
				type: "POST",  
				url: "libs/getZone.php",
				data: {countryID: $("#country_id option:selected").val()},  
				success: function(theResponse) {
							
						
						$("#zone_id").html(theResponse);
							
	
				}
			});  

}

function selectZone(id) {
		
		$(this).remove();
		
	  		$.ajax({  
				type: "POST",  
				url: "libs/getZone.php",
				data: {countryID: $("#country_id option:selected").val(), zoneID: id},  
				success: function(theResponse) {
							
						
						$("#zone_id").html(theResponse);
							
	
				}
			});  

}

function smartColumns() { //Create a function that calculates the smart columns

        //Reset column size to a 100% once view port has been adjusted
	$("ul.column").css({ 'width' : "100%"});

	var colWrap = $("ul.column").width(); //Get the width of row
	var colNum = Math.floor(colWrap / 180); //Find how many columns of 200px can fit per row / then round it down to a whole number
	var colFixed = Math.floor(colWrap / colNum); //Get the width of the row and divide it by the number of columns it can fit / then round it down to a whole number. This value will be the exact width of the re-adjusted column

	$("ul.column").css({ 'width' : colWrap}); //Set exact width of row in pixels instead of using % - Prevents cross-browser bugs that appear in certain view port resolutions.
	$("ul.column li").css({ 'width' : colFixed}); //Set exact width of the re-adjusted column	

}	


function printpr() { 
	var OLECMDID = 7; 
	/* OLECMDID values: 
	* 6 - print 
	* 7 - print preview 
	* 1 - open window 
	* 4 - Save As 
	*/
	var PROMPT = 1; // 2 DONTPROMPTUSER 
	var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
	document.body.insertAdjacentHTML('beforeEnd', WebBrowser); 
	WebBrowser1.ExecWB(OLECMDID, PROMPT); 
	WebBrowser1.outerHTML = ""; 


} 



function smartColumnsAds() { //Create a function that calculates the smart columns

        //Reset column size to a 100% once view port has been adjusted
	$("ul.column").css({ 'width' : "100%"});

	var colWrap = $("ul.column").width(); //Get the width of row
	var colNum = Math.floor(colWrap / 170); //Find how many columns of 200px can fit per row / then round it down to a whole number
	var colFixed = Math.floor(colWrap / colNum); //Get the width of the row and divide it by the number of columns it can fit / then round it down to a whole number. This value will be the exact width of the re-adjusted column

	$("ul.column").css({ 'width' : colWrap}); //Set exact width of row in pixels instead of using % - Prevents cross-browser bugs that appear in certain view port resolutions.
	$("ul.column li").css({ 'width' : colFixed}); //Set exact width of the re-adjusted column	

}


$(function(){   
    var objKey="#search"; // id text search 
    if($(objKey).val()==""){   
            $(objKey).addClass("watermark");   
    }      
    $(objKey).focus(function(){     
        $(objKey).removeClass("watermark");             
    }).blur(function(){   
        if($(objKey).val()==""){   
            $(objKey).addClass("watermark");   
        }      
    });    
});

//$(function(){
//	$("ul#acc > li").click(function(){
//
//			$(this).children("ul").slideToggle();
//			
//			//$("ul#acc > li > ul").slideUp();
//			//$(this).children("ul").slideDown();
//			
//			var width = $(this).find("img:first").css('width');
//			
//			if(width == '250px'){
//				
//				
//				$(this).find("img:first").css({'width':'150px', 'height':'80'});
//
//			
//			}else{
//				
//				$(this).find("img:first").css({'width':'250px', 'height':'180'});
//
//				
//			}
//			
//			
//			$(this).siblings().children("ul").slideUp();
//			$(this).siblings().find("img:first").css({'width':'150px', 'height':'80'});
//
//			
//	});
//});

$(function(){
	$("ul#ads >  li").click(function(){

			$(this).children("ul").slideToggle();
			
			var width = $(this).find("img:first").css('width');
			
			if(width == '250px'){
				
				
				$(this).find("img:first").css({'width':'150px', 'height':'80'});

			
			}else{
				
				$(this).find("img:first").css({'width':'250px', 'height':'350'});

				
			}
			
			
			$(this).siblings().children("ul").slideUp();
			$(this).siblings().find("img:first").css({'width':'150px', 'height':'80'});

			
	});
});






function showToolTip(e, imageFileName) {
	var largeTooltip= document.getElementById("largeIcon");
	var tooltipCon = document.getElementById("tooltipContainer");
	
	tooltipCon.style.display = "block";
	largeTooltip.src = "images/color/" + imageFileName;
	
	var newX = e.offsetLeft+370;
	var newY = e.offsetTop+50;

	$("#tooltipContainer").css({'top' : newY, 'left' : newX});

}

function hideToolTip() {

	var tooltipCon = document.getElementById("tooltipContainer");
	tooltipCon.style.display = "none";
}

