/* Created and Maintained By:- Priyank Vaish | Email:- priyank.vaish@sapple.co.in */

var pid1 = 2;
var pid2 = 6;
var cpush = 0;
var keychaincheck = 0;
var wrappedcheck = 0;
var prodval = 0;
var shipval = 0;
var pselect = 0;
var sselect = 0;
var totProof = 0;
var totaldefault = 0;
var extraPrice = 0;
var extraPriceCounterA = 0;
var extraPriceCounterY = 0;
var extracolorCost = 0;
var yeCol = new Array();
var aeCol = new Array();
var totqty = 0;
var lenArr = 0;
var flag = 1;
var colorAStr = '';
var colorYStr = '';
var adultStr = '';
var youthStr = '';
var AQStr = '';
var YQStr = '';
var postTotal = 0;
var totalamt = 0;
var min50Alert = 0;

var finalFixShipProd = 0;
/* Cliparts variable */
var fscid = '';
var fecid = '';
var bscid = '';
var becid = '';

/* NEw price variables */

var logoprice = 0;
var frontmsgprice = 0;
var backmsgprice = 0;
var internalmsgprice = 0;
var getOnce = 1;

var youthArr = new Array();
var adultArr = new Array();

/* new changes 10 8 8 ect */

var diffArr = new Array();
var diffCount = 0;

/* Qty array for debbosed and solid case if in any style qty is over 100 then min qty clasue will be 20 per style */
var yeArr = new Array();
var aeArr = new Array();


$(document).ready(function(){
  $.post('getdatapv2s.php', {'dataval' : 20}, getPostData, "json");
  $('.choose1').click(function(){
      tempid = $(this).attr('id');
      trackid = $('.yesselect1').attr('id');
      $('#'+trackid).removeClass('yesselect1');
      $('#'+trackid).addClass('noselect1');
      $('#'+tempid).removeClass('yesselect1');
      $('#'+tempid).addClass('noselect1');
      $('#'+tempid).addClass('yesselect1');
      v1 = $(this).attr('id').slice(6);
      //console.log(v1);
      if(v1 == 11){
        pid1 = 1;
      }else if(v1 == 12){
        pid1 = 2;
      }else if(v1 == 13){
        pid1 = 4;
      }else if(v1 == 14){
        pid1 = 3;
      }else if(v1 == 15){
        pid1 = 9;
      }
      if(pid1 == 9){
		blockForBlank();
	  }else{
		unblockForOtherStyle();
	  }
      $('#pid2data').html('');
      $('#loadingpid2').show();
	  if(pid1 == 1 && pid2 == 6){
	  	debSolidHide();
	  }else{
		debSolidShow();
	  }
      $.post('pid2GetData.php', {'pid2':pid2, 'pid1':pid1}, postPid2getData, 'html');
      getShipProdData();
  });

  $('.tt').hover(function(){
    $(this).next('div').show();
  },
  function(){
    $(this).next('div').hide();

  });

  




  $('.choose2').click(function(){
    tempid = $(this).attr('id');
    trackid = $('.yesselect2').attr('id');
    $('#'+trackid).removeClass('yesselect2');
    $('#'+trackid).addClass('noselect2');
    $('#'+tempid).removeClass('yesselect2');
    $('#'+tempid).addClass('noselect2');
    $('#'+tempid).addClass('yesselect2');
    type2 = $(this).attr('id').slice(6);
    v1 = type2;
    
    if(v1 == 21){
      pid2 = 6;
    }else if(v1 == 22){
      pid2 = 7;
    }else if(v1 == 23){
      pid2 = 8;
    }else if(v1 == 24){
      pid2 = 5;
    }
    $('#pid2data').html('');
    $('#loadingpid2').show();
	if(pid1 == 1 && pid2 == 6){
	  debSolidHide();
    }else{
	  debSolidShow();
	}
    $.post('pid2GetData.php', {'pid2':pid2, 'pid1':pid1}, postPid2getData, 'html');
    getShipProdData();
  });

  $('input#f_message').keyup(function(){
    temtext1 = $("#front_text2").html();
    temtext2 = $("#f_message").val();
    temlen1 = temtext1.length;
    temlen2 = temtext2.length;
    if(temlen2 >= 22){
      $('#ccpush').fadeIn('1000');
    }
    if(!$('#push').attr('checked')){
      tdata = temtext2.substr(0,22);
      $("#front_text2").html(tdata);
      $("#f_message").val(tdata);
      cpush = 0;
      textboxamtdefault();
    }else{
      cpush = 1;
      textboxamtdefault();
    }
     //if((temlen1 <22) || (temlen2 < 22)){
      $("#front_text2").html($(this).val());
    //}
  });

  $('input#f_message').blur(function(){
    temtext1 = $("#front_text2").html();
    temtext2 = $("#f_message").val();
    temlen1 = temtext1.length;
    temlen2 = temtext2.length;
    if(temlen2 >= 22){
      $('#ccpush').fadeIn('1000');
    }
    if(!$('#push').attr('checked')){
      tdata = temtext2.substr(0,22);
      $("#f_message").val(tdata);
      cpush = 0;
      textboxamtdefault();
    }else{
      cpush = 1;
      textboxamtdefault();
    }
			//if((temlen1 <22) || (temlen2 < 22)){
				$("#front_text2").html($(this).val());
			//}
	});

  $("#f_change").change(function (){ 
    fontchange();
  }).change();

  $('input#b_message').keyup(function(){
    temtext1 = $("#back_text2").html();
    temtext2 = $("#b_message").val();
    temlen1 = temtext1.length;
    temlen2 = temtext2.length;
    if(temlen1 <=38 || temlen2<= 38){
      $("#back_text2").html($(this).val());
    }
    textboxamtdefault();
  });

  $('input#i_message').keyup(function(){
    $("#internal_text").html($(this).val());
    textboxamtdefault()
  });
  
  $('#keychain').click(function(){
    if($(this).attr('checked')){
      keychaincheck = 1;
    }else{
      keychaincheck = 0;
    }
    textboxamtdefault();
  });

  $('#wrapped').click(function(){
    if($(this).attr('checked')){
      wrappedcheck = 1;
    }else{
      wrappedcheck = 0;
    }
    textboxamtdefault();
  });
  
  $('#loadingpid2').hide();
  $.post('pid2GetData.php', {'pid2':pid2, 'pid1':pid1}, postPid2getData, 'html');
  //$.post('getOtherPrices.php', {'qty': 100}, getPostOtherPrices, "json");
});

function getShipProdData(){
  if(totqty > -1 && totqty < 20){
    tempval = 20;
  }else{
    tempval = totqty;
  }
  $.post('getdatapv2s.php', {'dataval' : tempval}, getPostData, "json");
}


function getPostData(d, s){
  if(d.success == 1){
    $('#production_time').html('');
     m0 = '<option rel="0" value="0" ';
    if(pselect == 0){
      m0 = m0 + ' selected="selected">Please select<\/option>';
    }else{
      m0 = m0 + '>Please select<\/option>';
    }

    k=1;
    tmp1 = m0;
    $.each(d.prod, function(i,n){
      tmp = '<option rel="'+k+'" value="'+n.price+'" '
       if(pselect == k){
         tmp = tmp + ' selected="selected">'+n.des+'<\/option>';
       }else{
        tmp = tmp + ' >'+n.des+'<\/option>';
       }
       tmp1 = tmp1 + tmp;
       k++;
    });
   
    $('#production_time').append(tmp1);
   
    $('#shipping_time').html('');
     m0 = '<option rel="0" value="0" ';
    if(sselect == 0){
      m0 = m0 + ' selected="selected">Please select<\/option>';
    }else{
      m0 = m0 + '>Please select<\/option>';
    }

    k=1;
    tmp1 = m0;
    $.each(d.ship, function(i,n){
      tmp = '<option rel="'+k+'" value="'+n.price+'" '
       if(sselect == k){
         tmp = tmp + ' selected="selected">'+n.des+'<\/option>';
       }else{
        tmp = tmp + ' >'+n.des+'<\/option>';
       }
       tmp1 = tmp1 + tmp;
       k++;
    });
     
     $('#shipping_time').append(tmp1);
    

  }
  textboxamtdefault();
  if(finalFixShipProd == 1){
    $.post('getpids.php', {'pid1' : pid1, 'pid2' : pid2, 'qty': totqty}, getPostPidFinal, "json");
    finalFixShipProd = 0;
  }
}




function shippingOnChange(){
  sselect = $('#shipping_time').find('option:selected').attr('rel');
  textboxamtdefault();
}

function productionOnChange(){
  pselect = $('#production_time').find('option:selected').attr('rel');
  textboxamtdefault();
}

function postPid2getData(d, s){
  $('#loadingpid2').hide();
  $('#pid2data').html(d);
  displayPriceAndQty('0', '0')
  totqty = 0;
  totaldefault = 0;
}

function pushCheck(){
  temtext2 = $("#f_message").val();
  temlen2 = temtext2.length;
  if(!$('#push').attr('checked')){
    tdata = temtext2.substr(0,22);
    $("#f_message").val(tdata);
    $("#front_text2").html(tdata);
    cpush = 0;
  }else{
    cpush = 1;
  }
  textboxamtdefault();
}


function fontColorChange(dd){
  temp = $(dd).find('option:selected').attr('ref');
  $('#front_text2').css({'color' : temp});
  $('#back_text').css({'color' : temp});
  $('#internal_text').css({'color' : temp});
  dd.style.border = 'none';

}

function fontchange(){
  temp = $('#f_change').find('option:selected').attr('value');
  if(temp == "Other"){
    $('#spanotherfont').show();
  }else{
    $('#spanotherfont').hide();
  }

  temp = $('#f_change').find('option:selected').attr('value');
  $('#front_text2').css({'font-family' : temp});
  $('#back_text2').css({'font-family' : temp});
  $('#internal_text').css({'font-family' : temp});
}

function addProof(dd){
  tempval = $('#digitalProof').find('option:selected').attr('value');
  if(tempval == 'No'){
    totProof = 0;
    $('#proofmsg').hide();
  }else if(tempval == 'Yes'){
    totProof = 10;
    $('#proofmsg').show();
  }
  textboxamtdefault();
}

function customColorPopUp(){
  window.open('customcolor.html','','scrollbars=yes,menubar=no,width=660,height=580,resizable=yes,toolbar=no,location=no,status=no, top=10px,left=150px');
}

function calculateQty(){
  totqty = 0;
  extraPriceCounterY = 0;
  yeCol = new Array();
  $('.youthInbox').each(function(i){
    temp = $.trim(this.value);
    tempc = $(this).attr('refc');
    if(temp.indexOf('.') == -1){
      if(temp != '' && !isNaN(temp)){
        if(temp > 0){
          totqty = totqty + parseInt(temp, 10);
		      yeCol[extraPriceCounterY] = tempc
		      extraPriceCounterY++;
        }
      }
    }
  });
  extraPriceCounterA = 0;
  aeCol = new Array();
  $('.adultInbox').each(function(i){
    temp = $.trim(this.value);
	  tempc = $(this).attr('refc');
    if(temp.indexOf('.') == -1){
      if(temp != '' && !isNaN(temp)){
        if(temp > 0){
          totqty = totqty + parseInt(temp, 10);
		      aeCol[extraPriceCounterA] = tempc
		      extraPriceCounterA++;
        }
      }
    }
  });
  if(pid1 == 1 && pid2 == 6){
	if(totqty <= 25){
    	debSolidHide();
	}else if(totqty > 25){
		debSolidShow();
	}
  }else{
    debSolidShow();
  }
  //return totqty;
}

function calculatePriceForQty(){
  if(totqty > -1 && totqty < 20){
    if(pid1 == 1 && pid2 == 6){
      $.post('getpids.php', {'pid1' : pid1, 'pid2' : pid2, 'qty': totqty}, getPostPid, "json");
    }else{
      displayPriceAndQty(totqty, '0');
      textboxamtdefault();
    }
  }else{
    $.post('getpids.php', {'pid1' : pid1, 'pid2' : pid2, 'qty': totqty}, getPostPid, "json");
  }
}

function getPostPid(d, s){
  if(d.success == 1){
    //displayPriceAndQty(d.qty, d.price);
    totaldefault = d.price;
    textboxamtdefault();
  }
}

function getPostOtherPrices(d,s){
    logoprice = parseFloat(d.logoPrice,10);
    //frontmsgprice = parseFloat(d.frontPrice,10);
    backmsgprice = parseFloat(d.backPrice,10);
    internalmsgprice = parseFloat(d.internalPrice,10);
    $('#logoMsgText').html('(+'+logoprice.toFixed(2)+'/each)');
    //$('#frontMsgText').html('(+'+frontmsgprice.toFixed(2)+'/each)');
    $('#backMsgText').html('(+'+backmsgprice.toFixed(2)+'/each)');
    $('#internalMsgText').html('(+'+internalmsgprice.toFixed(2)+'/each)');
}

function qtyKeyPess(){
  calculateQty();
  
  
  $.post('getOtherPrices.php', {'qty': totqty}, getPostOtherPricesWorking, "json");
}

function getPostOtherPricesWorking(d,s){
    logoprice = parseFloat(d.logoPrice,10);
    frontmsgprice = parseFloat(d.frontPrice,10);
    backmsgprice = parseFloat(d.backPrice,10);
    internalmsgprice = parseFloat(d.internalPrice,10);
    $('#logoMsgText').html('(+'+logoprice.toFixed(2)+'/each)');
    $('#frontMsgText').html('(+'+frontmsgprice.toFixed(2)+'/each)');
    $('#backMsgText').html('(+'+backmsgprice.toFixed(2)+'/each)');
    $('#internalMsgText').html('(+'+internalmsgprice.toFixed(2)+'/each)');
    calculatePriceForQty();
    getShipProdData();
}


function displayPriceAndQty(tot, pr){
  pr = parseFloat(pr,10);
  tot = parseInt(tot,10);
  $('#showPartQty').text(tot);
  $('#showPartPrice').text(pr.toFixed(2));
  $('#qtyShow11').text(tot);
  $('#total_right').text(pr.toFixed(2));
  $('#qtyShow22').text(tot);
  $('#total_amount').text(pr.toFixed(2));
}

/* Clipart management */
	


	function resetclips(){
		$('#beimg > img').attr('src', 'images2/1x1.gif');
		$('#bsimg > img').attr('src', 'images2/1x1.gif');
		$('#feimg > img').attr('src', 'images2/1x1.gif');
		$('#fsimg > img').attr('src', 'images2/1x1.gif');
		document.getElementById('bscselect').value = "";
		document.getElementById('fscselect').value = "";
		document.getElementById('fecselect').value = "";
		document.getElementById('becselect').value = "";
		fscid = '';
		fecid = '';
		bscid = '';
		becid = '';
		$('#upload1Status').text('');
		$('#upload2Status').text('');
		$('#upload3Status').text('');
		$('#upload4Status').text('');
		$('#fecselect').removeAttr('disabled');
		$('#becselect').removeAttr('disabled');
		$('#fscselect').removeAttr('disabled');
		$('#bscselect').removeAttr('disabled');
	}
	
	function logoSelectBox(){
		countryval = $('#country_pick').find('option:selected').attr('value');
		if(countryval != 'china'){
			$('#clipartdiv').fadeIn("slow");
		}else{
			$('#clipartdiv').fadeOut("slow");
			resetclips();	
		}
		textboxamtdefault();
	}

	function logoSelectBoxBlankReset(){
		$('#push').removeAttr('checked');
		pushCheck();
		document.logoForm.country_select.value = "china";
		$('#clipartdiv').slideUp("slow");
		resetclips();
		textboxamtdefault();
	}

	function blockForBlank(){
		logoSelectBoxBlankReset();
		$('#f_message').val('');
		$('#b_message').val('');
		$('#i_message').val('');
		$('.blockstyle').block();
	}

	function unblockForOtherStyle(){
		$('.blockstyle').unblock();
	}
	
	function ajaxFileUpload(){
		/*$("#upload1Status").ajaxStart(function(){
			$('#upload1Status').text('Uploading file...');
		})
		.ajaxComplete(function(){

		});*/

		$.ajaxFileUpload
		(
			{	
				url:'doajaxfileupload.php',
				secureuri:false,
				fileElementId:'fileToUpload',
				dataType: 'json',
				success: function (data, status)
				{
					if(typeof(data.error) != 'undefined')
					{
						if(data.error != '')
						{
							alert(data.error);
							$('#upload1Status').text('');
							$('#fstartspan').hide();
							$('#upload1').hide();
						}else
						{
							fscid = 'clipartImages/upload/'+data.filename;
							$('#upload1Status').text('Front start clipart ('+
							data.msg+') uploaded successfully.');
							$('#upload1Status').show();
							$('#fscselect').attr('disabled','disabled');
							$('#fstartspan').hide();
							$('#upload1').hide();
						}
					}
				},
				error: function (data, status, e)
				{	
					$('#upload1Status').text('');
					alert(e);
				}
			}
		)
		
		return false;

	}


	function ajaxFileUpload2(){
		/*$("#upload2Status").ajaxStart(function(){
			$('#upload2Status').text('Uploading file...');
		})
		.ajaxComplete(function(){

		});*/

		$.ajaxFileUpload
		(
			{	
				url:'doajaxfileupload2.php',
				secureuri:false,
				fileElementId:'fileToUpload2',
				dataType: 'json',
				success: function (data, status)
				{
					if(typeof(data.error) != 'undefined')
					{
						if(data.error != '')
						{
							alert(data.error);
							$('#upload2Status').text('');
							$('#upload2').hide();
						}else
						{
							fecid = 'clipartImages/upload/'+data.filename;
							$('#upload2Status').text('Front end clipart ('+
							data.msg+') uploaded successfully.');
							$('#upload2Status').show();
							$('#fecselect').attr('disabled','disabled');
							$('#upload2').hide();
						}
					}
				},
				error: function (data, status, e)
				{
					$('#upload2Status').text('');
					alert(e);
					
				}
			}
		)
		
		return false;

	}

	function ajaxFileUpload3(){
		/*$("#upload2Status").ajaxStart(function(){
			$('#upload2Status').text('Uploading file...');
		})
		.ajaxComplete(function(){

		});*/

		$.ajaxFileUpload
		(
			{	
				url:'doajaxfileupload3.php',
				secureuri:false,
				fileElementId:'fileToUpload3',
				dataType: 'json',
				success: function (data, status)
				{
					if(typeof(data.error) != 'undefined')
					{
						if(data.error != '')
						{
							alert(data.error);
							$('#upload3Status').text('');
							$('#upload3').hide();
						}else
						{
							bscid = 'clipartImages/upload/'+data.filename;
							$('#upload3Status').text('Back start clipart ('+
							data.msg+') uploaded successfully.');
							$('#upload3Status').show();
							$('#bscselect').attr('disabled','disabled');
							$('#upload3').hide();
						}
					}
				},
				error: function (data, status, e)
				{
					$('#upload3Status').text('');
					alert(e);
					
				}
			}
		)
		
		return false;

	}

	function ajaxFileUpload4(){
		/*$("#upload2Status").ajaxStart(function(){
			$('#upload2Status').text('Uploading file...');
		})
		.ajaxComplete(function(){

		});*/

		$.ajaxFileUpload
		(
			{	
				url:'doajaxfileupload4.php',
				secureuri:false,
				fileElementId:'fileToUpload4',
				dataType: 'json',
				success: function (data, status)
				{
					if(typeof(data.error) != 'undefined')
					{
						if(data.error != '')
						{
							alert(data.error);
							$('#upload4Status').text('');
							$('#upload4').hide();
						}else
						{
							becid = 'clipartImages/upload/'+data.filename;
							$('#upload4Status').text('Back end clipart ('+
							data.msg+') uploaded successfully.');
							$('#upload4Status').show();
							$('#becselect').attr('disabled','disabled');
							$('#upload4').hide();
						}
					}
				},
				error: function (data, status, e)
				{
					$('#upload4Status').text('');
					alert(e);
					
				}
			}
		)
		
		return false;

	}

	function fscchange(){
		tv = $('#fscselect').find('option:selected').attr('value');
		if(tv == "upload"){
			$('#fsimg > img').attr('src', 'images2/1x1.gif');
			$('#upload1').show();
		}else if(tv == ""){
			$('#fsimg > img').attr('src', 'images2/1x1.gif');
			$('#upload1').hide();
			fscid= '';
		}else{
			$('#fsimg > img').attr('src',tv);
			fscid= tv;
			$('#upload1').hide();
		}
	}
 
	function fecchange(){
		tv = $('#fecselect').find('option:selected').attr('value');
		if(tv == "upload"){
			$('#feimg > img').attr('src', 'images2/1x1.gif');
			$('#upload2').show();
		}else if(tv == ""){
			$('#feimg > img').attr('src', 'images2/1x1.gif');
			fscid= '';
			$('#upload2').hide();
		}else{
			$('#feimg > img').attr('src',tv);
			fecid= tv;
			$('#upload2').hide();
		}
	}

	function bscchange(){
		tv = $('#bscselect').find('option:selected').attr('value');
		if(tv == "upload"){
			$('#bsimg > img').attr('src', 'images2/1x1.gif');
			$('#upload3').show();
		}else if(tv == ""){
			$('#bsimg > img').attr('src', 'images2/1x1.gif');
			bscid= '';
			$('#upload3').hide();
		}else{
			$('#bsimg > img').attr('src',tv);
			bscid= tv;
			$('#upload3').hide();
		}
	}
 
	function becchange(){
		tv = $('#becselect').find('option:selected').attr('value');
		if(tv == "upload"){
			$('#beimg > img').attr('src', 'images2/1x1.gif');
			$('#upload4').show();
		}else if(tv == ""){
			$('#beimg > img').attr('src', 'images2/1x1.gif');
			$('#upload4').hide();
			becid= '';
		}else{
			$('#beimg > img').attr('src',tv);
			becid= tv;
			$('#upload4').hide();
		}
	}
	
/* Clipart Management ends */

function textboxamtdefault(){
  calculateQty();
  totalamt = 0;
  /*if(totqty < 100){
    alert('Minimum Quantity must be 100.');
    return false;
  }*/
  backvalue = $('#b_message').val();
  internalvalue = $('#i_message').val();
  //frontvalue = $('#f_message').val();
  if(keychaincheck == 1){
    totalamt = totalamt + (totqty * 0.15);
  }

  if(wrappedcheck == 1){
    totalamt = totalamt + (totqty * 0.08);
  }

  if(cpush == 1){
    totalamt = totalamt + (totqty * frontmsgprice);
  }

  /*if($.trim(frontvalue) != ""){
    totalamt = totalamt + (totqty * frontmsgprice)
  }*/

  if($.trim(backvalue) != ""){
    totalamt = totalamt + (totqty * backmsgprice);
  }

  if($.trim(internalvalue) != ""){
    totalamt = totalamt + (totqty * internalmsgprice)
  }
  
  if(extraPriceCounterA > 0 && extraPriceCounterY > 0){
	  extraPrice = 40;
  }else{
    extraPrice = 0;
  }
  
  getColorExtraPrice();
  //console.log(extracolorCost);
  
  /*if(extraPriceCounterA > 1 || extraPriceCounterY > 1){
    extracolorCost = 10;
  }else{
    if(aeCol.length == 1 && yeCol.length == 1){
      if(aeCol[0] != yeCol[0]){
        extracolorCost = 10;
      }else{
        extracolorCost = 0;
      }
    }else{
      extracolorCost = 0;
    }
  }*/
  
  if(totqty > -1 && totqty < 20){
    if(pid1 == 1 && pid2 == 6){
      if(totqty > 0){
        //totaldefault = totqty;
      }else{
        totaldefault = 0;
      }
    }else{
      totaldefault = 0;
    }
  }

  //totaldefault = parseFloat($('#showPartPrice').text(),10);
  prodval = $('#production_time').find('option:selected').attr('value');
  shipval = $('#shipping_time').find('option:selected').attr('value');
  countryval = $('#country_pick').find('option:selected').attr('value');
  prodval = parseFloat(prodval,10);
  shipval = parseFloat(shipval, 10);
  if(countryval == 'china'){
    countryval = 0;
  }else{
    countryval = (totqty * logoprice);
  }
  //alert(totalamt + '--- ' + prodval + '--- ' + shipval + '--- ' + countryval + '--- ' + totaldefault + ' --- ' + extraPrice + '--- ' + extracolorCost + '--- ' + totProof)
  totalamt = totalamt + prodval + shipval + countryval + totaldefault + extraPrice + extracolorCost + totProof;
	totalamt = Math.round(totalamt*Math.pow(10,2))/Math.pow(10,2);
  displayPriceAndQty(totqty, totalamt)
   getInHandDate();
}

var prodvalT = "";
var shipvalT = '';
function getInHandDate(){
		
	var dayAdd = 0;	
    if(pselect != 0){
    	prodvalT = $('#production_time').find('option:selected').text();
		prodvalT = parseInt(prodvalT,10);
		if(isNaN(prodvalT)){
			prodvalT = 7;
		}
	}
	if(sselect != 0){
    	shipvalT = $('#shipping_time').find('option:selected').text(); 
		shipvalT = parseInt(shipvalT,10);
		if(isNaN(shipvalT)){
			shipvalT = 7;
		}
	}
	
	dayAdd = shipvalT + prodvalT;
	var now = new Date();
	var tnow = new Date();
	now.getYear();
	now.getMonth();
	now.getDate();
	//tnow.setDate(tnow.getDate()  + 1);
	for(var i=1;i<=dayAdd;i++){
		tnow.setDate(tnow.getDate()  + 1);
		if(tnow.getDay() == 6){
			tnow.setDate(tnow.getDate()  + 1);
		}
		if(tnow.getDay() == 0){
			tnow.setDate(tnow.getDate()  + 1);
		}
		
	}
	/* lying on border conditions */
		if(tnow.getDay() == 6){
			tnow.setDate(tnow.getDate()  + 1);
		}
		if(tnow.getDay() == 0){
			tnow.setDate(tnow.getDate()  + 1);
		}
	/* lying on border conditions ends*/
	
	var Months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	ttm = tnow.getMonth();
	ttm = Months[ttm];
	//alert(tnow.getDate()+'-'+ttm+'-'+tnow.getFullYear());
	if(pselect == 0 && sselect == 0){
		$('#inhanddate').html("Products Delivered On:- ");
		$('#inhanddt').val("");
	}else{
		$('#inhanddate').html("Products Delivered On:- "+ttm+' '+tnow.getDate()+', '+tnow.getFullYear());
		$('#inhanddt').val("Products Delivered On:- "+ttm+' '+tnow.getDate()+', '+tnow.getFullYear());
	}	
}



function getColorExtraPrice(){
	extracolorCost = 0;
	if(totqty > 99){
		if(extraPriceCounterA > 1 || extraPriceCounterY > 1){
		  extracolorCost = 10;
		}
		
		if(aeCol.length == 0 && yeCol.length == 0){
			extracolorCost = 0;
		}
		
		if(aeCol.length == 1 && yeCol.length == 1){
		  if(aeCol[0] != yeCol[0]){
			extracolorCost = 10;
		  }else{
			extracolorCost = 0;
		  }
		}
	}
	
	if(totqty <= 99){
		var diffArr = new Array();
		diffArr = yeCol;
		diffCount = yeCol.length;
		for(i=0;i<aeCol.length;i++){
			ff = 0;
			for(j=0;j<yeCol.length;j++){
				if(ff == 0){
					if(yeCol[j] == aeCol[i]){
						ff = 1;
					}
				}
			}
			if(ff == 0){
			  diffArr[diffCount] = aeCol[i];
			  diffCount++;       
			}
		}
		if(diffArr.length == 0 || diffArr.length == 1){
			extracolorCost = 0;
		}
		if(diffArr.length == 2){
			extracolorCost = 10;
		}
		if(diffArr.length > 1){
			extracolorCost = 10 + ((diffArr.length - 2) * 8);
		}
	}
}


function debSolidHide(){
	$('#bMesHid').hide();
	$('#bTxtHid').hide();
	$('#iMesHid').hide();
	$('#iTxtHid').hide();
	$('#kChainHid').hide();
	$('#step5a').hide();
	$('#step5b').hide();
	$('#step6a').html('<strong>Step 5: Delivery &amp; Production </strong>');
	$('#iWrapHid').hide();
	$('#b_message').val('');
	$('#i_message').val('');
	$('#keychain').removeAttr('checked');
	$('#wrapped').removeAttr('checked');
	keychaincheck = 0;
	wrappedcheck = 0;
}

function debSolidShow(){
	$('#bMesHid').show();
	$('#bTxtHid').show();
	$('#iMesHid').show();
	$('#iTxtHid').show();
	$('#kChainHid').show();
	$('#iWrapHid').show();
	$('#step5a').show();
	$('#step5b').show();
	$('#step6a').html('<strong>Step 6: Delivery &amp; Production </strong>');
}

function newValidation(){
	ff = 0;
	flag = 1;
	min50Alert = 0;
	for(i=1;i<youthArr.length;i++){
		if(youthArr[i]['value'] != ""){
			tempV = parseInt(youthArr[i]['value'], 10);
			if(ff == 0){
			  if(tempV >= 100){
				ff = 1;
			  }
			}
		}
	}
	for(i=1;i<adultArr.length;i++){
		if(adultArr[i]['value'] != ""){
			tempV = parseInt(adultArr[i]['value'], 10);
			if(ff == 0){
			  if(tempV >= 100){
				ff = 1;
			  }
			}
		}
	}
	if(ff == 1){
		len = youthArr.length;
		for(i=1;i<len;i++){
			temp = $.trim(youthArr[i]['value']);
			if(temp == "0"){
			$('#youthQbox'+adultArr[i]['ref']).css({'border':'3px solid red'});
			  flag = 0;
			}
			if(temp != '' && isNaN(temp)){
			  $('#youthQbox'+youthArr[i]['ref']).css({'border':'3px solid red'});
			  flag = 0;
			}else{
			  if(!isNaN(temp)){
				if(temp.indexOf('.') != -1){
				  $('#youthQbox'+youthArr[i]['ref']).css({'border':'3px solid red'});
				  flag = 0;
				}
				if(temp < 0){
				  $('#youthQbox'+youthArr[i]['ref']).css({'border':'3px solid red'});
				  flag = 0;
				}
				if(temp < 20 && temp != ''){
				  $('#youthQbox'+youthArr[i]['ref']).css({'border':'3px solid red'});
				  flag = 0;
				  min50Alert = 1;
				}
				if(youthArr[i]['refc'] == 'Custom' && temp > 0){
				  pmsId = youthArr[i]['ref'];
				  pmsTemp = $.trim($('#pmsBox'+pmsId).val());
				  if(pmsTemp == ''){
					$('#pmsBox'+pmsId).css({'border':'3px solid red'});
					flag = 0;
				  }
				  if(pid1 == 3 || pid1 == 4){
					if($('#fontYbox'+pmsId).find('option:selected').attr('value') == ''){
					  $('#fontYbox'+pmsId).css({'border':'3px solid red'});
					  flag = 0;
					}
				  }
				}
				if(youthArr[i]['refc'] != 'Custom' && temp > 0 && temp.indexOf('.') == -1){
				  pmsId = youthArr[i]['ref'];
				  if(pid1 == 3 || pid1 == 4){
					if($('#fontYbox'+pmsId).find('option:selected').attr('value') == ''){
					  $('#fontYbox'+pmsId).css({'border':'3px solid red'});
					  flag = 0;
					}
				  }
				}
			  }
			}
		}

		len = adultArr.length;
		for(i=1;i<len;i++){
			temp = $.trim(adultArr[i]['value']);
			if(temp == "0"){
      			$('#adultQbox'+adultArr[i]['ref']).css({'border':'3px solid red'});
      			flag = 0;
    		}
			if(temp != '' && isNaN(temp)){
			  $('#adultQbox'+adultArr[i]['ref']).css({'border':'3px solid red'});
			  flag = 0;
			}else{
			  if(!isNaN(temp)){
				if(temp.indexOf('.') != -1){
				  $('#adultQbox'+adultArr[i]['ref']).css({'border':'3px solid red'});
				  flag = 0;
				}
				if(temp < 0){
				  $('#adultQbox'+adultArr[i]['ref']).css({'border':'3px solid red'});
				  flag = 0;
				}
				if(temp < 20 && temp != ''){
					$('#adultQbox'+adultArr[i]['ref']).css({'border':'3px solid red'});
					flag = 0;
					min50Alert = 1;
				}
				if(adultArr[i]['refc'] == 'Custom' && temp > 0){
				  pmsId = adultArr[i]['ref'];
				  pmsTemp = $.trim($('#pmsBox'+pmsId).val());
				  if(pmsTemp == ''){
					$('#pmsBox'+pmsId).css({'border':'3px solid red'});
					flag = 0;
				  }
				  if(pid1 == 3 || pid1 == 4){
					if($('#fontAbox'+pmsId).find('option:selected').attr('value') == ''){
					  $('#fontAbox'+pmsId).css({'border':'3px solid red'});
					  flag = 0;
					}
				  }
				}
				if(adultArr[i]['refc'] != 'Custom' && temp > 0 && temp.indexOf('.') == -1){
				  pmsId = adultArr[i]['ref'];
				  if(pid1 == 3 || pid1 == 4){
					if($('#fontAbox'+pmsId).find('option:selected').attr('value') == ''){
					  $('#fontAbox'+pmsId).css({'border':'3px solid red'});
					  flag = 0;
					}
				  }
				}
			  }
			}
		}
	}



}


function selectpost(){
  if(totqty < 20){
    if(pid1 == 1 && pid2 == 6){
      if(totqty < 1){
        alert('Please select Quantity.');
        return false;
      }
    }else{
      alert('Minimum Quantity must be 20.');
      return false;
    }
  }

  if(pselect == 0){
    alert('Please select Production Time');
    return false;
  }

  if(sselect == 0){
    alert('Please select Shipping Time');
    return false;
  }
  
  lenArr = 1;
  youthArr = new Array();
  $('.youthInbox').each(function(i){
    temp = $.trim(this.value);
    youthArr[lenArr] = new Array(3)
    youthArr[lenArr]['value'] = temp;
    youthArr[lenArr]['refc'] = $(this).attr('refc');
    youthArr[lenArr]['ref'] = $(this).attr('ref');
    lenArr++;
  });
  lenArr = 1;
  adultArr = new Array();
  $('.adultInbox').each(function(i){
    temp = $.trim(this.value);
    adultArr[lenArr] = new Array(3)
    adultArr[lenArr]['value'] = temp;
    adultArr[lenArr]['refc'] = $(this).attr('refc');
    adultArr[lenArr]['ref'] = $(this).attr('ref');
    lenArr++;
  });
  validateColors();
}

function validateColors(){
  len = adultArr.length;
  flag = 1;
  min50Alert = 0;
  for(i=1;i<len;i++){
    temp = $.trim(adultArr[i]['value']);
	if(temp == "0"){
      $('#adultQbox'+adultArr[i]['ref']).css({'border':'3px solid red'});
      flag = 0;
    }
    if(temp != '' && isNaN(temp)){
      $('#adultQbox'+adultArr[i]['ref']).css({'border':'3px solid red'});
      flag = 0;
    }else{
      if(!isNaN(temp)){
        if(temp.indexOf('.') != -1){
          $('#adultQbox'+adultArr[i]['ref']).css({'border':'3px solid red'});
          flag = 0;
        }
        if(temp < 0){
          $('#adultQbox'+adultArr[i]['ref']).css({'border':'3px solid red'});
          flag = 0;
        }
		if(temp < 20 && temp != ''){
      if(pid1 == 1 && pid2 == 6){
        /* ---- newly added */ 
      }else{
        $('#adultQbox'+adultArr[i]['ref']).css({'border':'3px solid red'});
        flag = 0;
        min50Alert = 1;
      }
	  }
        if(adultArr[i]['refc'] == 'Custom' && temp > 0){
          pmsId = adultArr[i]['ref'];
          pmsTemp = $.trim($('#pmsBox'+pmsId).val());
          if(pmsTemp == ''){
            $('#pmsBox'+pmsId).css({'border':'3px solid red'});
            flag = 0;
          }
          if(pid1 == 3 || pid1 == 4){
            if($('#fontAbox'+pmsId).find('option:selected').attr('value') == ''){
              $('#fontAbox'+pmsId).css({'border':'3px solid red'});
              flag = 0;
            }
          }
        }
        if(adultArr[i]['refc'] != 'Custom' && temp > 0 && temp.indexOf('.') == -1){
          pmsId = adultArr[i]['ref'];
          if(pid1 == 3 || pid1 == 4){
            if($('#fontAbox'+pmsId).find('option:selected').attr('value') == ''){
              $('#fontAbox'+pmsId).css({'border':'3px solid red'});
              flag = 0;
            }
          }
        }
      }
    }
  }

  len = youthArr.length;
  for(i=1;i<len;i++){
    temp = $.trim(youthArr[i]['value']);
	if(temp == "0"){
      $('#youthQbox'+adultArr[i]['ref']).css({'border':'3px solid red'});
      flag = 0;
    }
    if(temp != '' && isNaN(temp)){
      $('#youthQbox'+youthArr[i]['ref']).css({'border':'3px solid red'});
      flag = 0;
    }else{
      if(!isNaN(temp)){
        if(temp.indexOf('.') != -1){
          $('#youthQbox'+youthArr[i]['ref']).css({'border':'3px solid red'});
          flag = 0;
        }
        if(temp < 0){
          $('#youthQbox'+youthArr[i]['ref']).css({'border':'3px solid red'});
          flag = 0;
        }
		if(temp < 20 && temp != ''){
      if(pid1 == 1 && pid2 == 6){
        /* ---- newly added */ 
      }else{
          $('#youthQbox'+youthArr[i]['ref']).css({'border':'3px solid red'});
          flag = 0;
		      min50Alert = 1;
      }
    }
        if(youthArr[i]['refc'] == 'Custom' && temp > 0){
          pmsId = youthArr[i]['ref'];
          pmsTemp = $.trim($('#pmsBox'+pmsId).val());
          if(pmsTemp == ''){
            $('#pmsBox'+pmsId).css({'border':'3px solid red'});
            flag = 0;
          }
          if(pid1 == 3 || pid1 == 4){
            if($('#fontYbox'+pmsId).find('option:selected').attr('value') == ''){
              $('#fontYbox'+pmsId).css({'border':'3px solid red'});
              flag = 0;
            }
          }
        }
        if(youthArr[i]['refc'] != 'Custom' && temp > 0 && temp.indexOf('.') == -1){
          pmsId = youthArr[i]['ref'];
          if(pid1 == 3 || pid1 == 4){
            if($('#fontYbox'+pmsId).find('option:selected').attr('value') == ''){
              $('#fontYbox'+pmsId).css({'border':'3px solid red'});
              flag = 0;
            }
          }
        }
      }
    }
  }


  if(flag == 0){
    alert("Please correct errors in Step 3 (Marked in Red.)");
	if(min50Alert == 1){
		alert("Minimum Order Quantity 20 Per Color.");
	}

	window.scrollTo(100,690);
    return false;
  }

  //New validation for debossed and solid.

 if(pid1 == 1 && pid2 == 6){
  newValidation();
  if(flag == 0){
    alert("Please correct errors in Step 3 (Marked in Red.)");
	if(min50Alert == 1){
		alert("Minimum Order Quantity 20 Per Color.");
	}

	window.scrollTo(100,690);
    return false;
  }

 }

  if(flag == 1){
    recalculatePrice();
  }
}

function recalculatePrice(){
  $.post('getOtherPrices.php', {'qty': totqty}, getPostOtherPricesWorkingFinal, "json");
}

function getPostOtherPricesWorkingFinal(d,s){
    logoprice = parseFloat(d.logoPrice,10);
    frontmsgprice = parseFloat(d.frontPrice,10);
    backmsgprice = parseFloat(d.backPrice,10);
    internalmsgprice = parseFloat(d.internalPrice,10);
    $('#logoMsgText').html('(+'+logoprice+'/each)');
    $('#frontMsgText').html('(+'+frontmsgprice+'/each)');
    $('#backMsgText').html('(+'+backmsgprice+'/each)');
    $('#internalMsgText').html('(+'+internalmsgprice+'/each)');
    if(finalFixShipProd == 0){
      finalFixShipProd = 1;
      if(totqty < 20){
        temptot = 20;
      }else{
         temptot = totqty;
      }
      $.post('getdatapv2s.php', {'dataval' : temptot}, getPostData, "json");

    }
    //$.post('getpids.php', {'pid1' : pid1, 'pid2' : pid2, 'qty': totqty}, getPostPidFinal, "json");
}

function getPostPidFinal(d,s){
 if(d.success == 1){
    totaldefault = d.price;
    textboxamtdefault();
	  generateStrings();
  }
	
}


function generateStrings(){
  postTotal = 0;
  len = adultArr.length;
  adultStr = '';
  colorAStr = '';
  Atot = 0;
  AQStr = '';
  for(i=1;i<len;i++){
    if(adultArr[i]['refc'] == 'Custom'){
      if($.trim(adultArr[i]['value']) != ''){
        adultStr = adultStr + '(' + $('#pmsBox'+adultArr[i]['ref']).val() + '[' +adultArr[i]['value']+ 'A]) ';
      }
    }else{
      if($.trim(adultArr[i]['value']) != ''){
        adultStr = adultStr + '(' + adultArr[i]['refc'] + '[' +adultArr[i]['value']+ 'A]) ';
      }
    }
    if($.trim(adultArr[i]['value']) != ''){
      if(pid1 == 3 || pid1 == 4){
          tempc = $('#fontAbox'+adultArr[i]['ref']).find('option:selected').html();
          colorAStr = colorAStr + '(' + tempc + '[' +adultArr[i]['value']+ 'A]) ';
      }
      Atot = Atot + parseInt(adultArr[i]['value'],10);
    }
  }
  if(Atot > 0){
    AQStr = 'Adult('+Atot+')';
    postTotal = Atot;
  }

  len = youthArr.length;
  youthStr = '';
  colorYStr = '';
  Ytot = 0;
  YQStr = ''
  for(i=1;i<len;i++){
    if(youthArr[i]['refc'] == 'Custom'){
      if($.trim(youthArr[i]['value']) != ''){
        youthStr = youthStr + '(' + $('#pmsBox'+youthArr[i]['ref']).val() + '[' +youthArr[i]['value']+ 'Y]) ';
      }
    }else{
      if($.trim(youthArr[i]['value']) != ''){
        youthStr = youthStr + '(' + youthArr[i]['refc'] + '[' +youthArr[i]['value']+ 'Y]) ';
      }
    }
    if($.trim(youthArr[i]['value']) != ''){
      if(pid1 == 3 || pid1 == 4){
          tempc = $('#fontYbox'+youthArr[i]['ref']).find('option:selected').html();
          colorYStr = colorYStr + '(' + tempc + '[' +youthArr[i]['value']+ 'Y]) ';
      }
      Ytot = Ytot + parseInt(youthArr[i]['value'],10);
    }
  }
  if(Ytot > 0){
    YQStr = 'Youth('+Ytot+')';
    postTotal = postTotal + Ytot;
  }
  finalsubmit();
}



function gotFocus(dd){
 dd.style.border = "1px solid #999999";
}

function finalsubmit(){
  if(totProof > 0){
    $('#digitalprf').val(totProof);
  }

  if(pid1 == 1){
    sname = 'Debossed';
  }else if(pid1 == 2){
    sname = 'embossed';
  }else if(pid1 == 4){
    sname = 'Collor Filled';
  }else if(pid1 == 3){
    sname = 'Printed';
  }else if(pid1 == 9){
    sname = 'Blank';
  }
  $('#bandStylepost').attr('value', sname);
  if(pid2 == 6){
			bname = 'Solid';
  }else if(pid2 == 7){
    bname = 'segmented';
  }else if(pid2 == 8){
    bname = 'swirl';
  }else if(pid2 == 5){
    bname = 'glow';
  }
  $('#bandTypepost').attr('value', bname);
  tempdata = AQStr + YQStr;
  $('#fontsizepost').attr('value', tempdata);

  if(totalamt <= 0){
	alert("Please try re-selecting in Step 1 And Step 2.");
	return false;
  }

  tempdata = $('#f_change').find('option:selected').attr('value');
  if(tempdata == "Other"){
    temp = $('#otherfont').attr('value');
    if($.trim(temp) == ''){
      alert('Please type other font name.')
      $('#otherfont').focus();
      return false;
    }else{
      tempdata = temp;
    }
  }else{
    tempdata = $('#f_change').find('option:selected').html();
  }
  if(pid1 != 9){
    $('#fontpost').attr('value', tempdata);
  }
  $('#totalpost').val(totalamt);
  $('#quantitypost').val(postTotal);
  prodval = $('#production_time').find('option:selected').html();
  shipval = $('#shipping_time').find('option:selected').html();
  countryval = $('#country_pick').find('option:selected').html();
  $('#productionpost').attr('value', prodval);
  $('#shippingpost').attr('value', shipval);
  $('#madeinpost').attr('value', countryval);
  tempdata = adultStr + youthStr;
  $('#area1post').attr('value', tempdata)
  tempdata = $('#f_message').attr('value');
  $('#frontMessagepost').attr('value', tempdata);
  tempdata = $('#b_message').attr('value');
  $('#backMessagepost').attr('value', tempdata);
  tempdata = $('#i_message').attr('value');
  $('#internalMessagepost').attr('value', tempdata);
  tempdata = colorAStr + colorYStr;
  $('#fontcolorpost').attr('value', tempdata);
  /*tempdata = $('#f_change').find('option:selected').html();
  if(pid1 != 9){
    $('#fontpost').attr('value', tempdata);
  }*/

   tempdata = $('#inhanddt').attr('value') + '\n' + $('#spins').attr('value');
  $('#spinspost').attr('value', tempdata);
  countryval = $('#country_pick').find('option:selected').attr('value');
  if(countryval != 'china'){
    $('#fslogoid').attr('value', fscid);
    $('#felogoid').attr('value', fecid);
    $('#bslogoid').attr('value', bscid);
    $('#belogoid').attr('value', becid);
  }
  if(keychaincheck ==  1){
		$('#keychainpost').attr('value', 'on');
	}else{
		$('#keychainpost').attr('value', '');
	}
	if(wrappedcheck == 1){
		$('#wrappedpost').attr('value', 'on');
	}else{
		$('#wrappedpost').attr('value', '');
	}
  document.chart.submit();
}


