/* 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 = '';

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

$(document).ready(function(){
  $.post('getdatapv2.php', {'dataval' : 100}, 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();
      $.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();
    $.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;
    }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;
    }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');
});

function getShipProdData(){
  if(totqty > -1 && totqty < 100){
    tempval = 100;
  }else{
    tempval = totqty;
  }
  $.post('getdatapv2.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('getpid.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++;
        }
      }
    }
  });
  //return totqty;
}

function calculatePriceForQty(){
  if(totqty > -1 && totqty < 100){
    displayPriceAndQty(totqty, '0');
    textboxamtdefault();
  }else{
    $.post('getpid.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 qtyKeyPess(){
  calculateQty();
  calculatePriceForQty();
  getShipProdData();
}

function displayPriceAndQty(tot, pr){
  $('#showPartQty').text(tot);
  $('#showPartPrice').text(pr);
  $('#qtyShow11').text(tot);
  $('#total_right').text(pr);
  $('#qtyShow22').text(tot);
  $('#total_amount').text(pr);
}

/* 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();
  if(keychaincheck == 1){
    totalamt = totalamt + (totqty * 0.15);
  }

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

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

  if($.trim(internalvalue) != ""){
    totalamt = totalamt + (totqty * 0.05)
  }
  
  if(extraPriceCounterA > 0 && extraPriceCounterY > 0){
	extraPrice = 40;
  }else{
    extraPrice = 0;
  }
  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 < 100){
    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 * 0.10);
  }
  totalamt = totalamt + prodval + shipval + countryval + totaldefault + extraPrice + extracolorCost + totProof;
	totalamt = Math.round(totalamt*Math.pow(10,2))/Math.pow(10,2);
  displayPriceAndQty(totqty, totalamt)
}



function selectpost(){
  if(totqty < 100){
    alert('Minimum Quantity must be 100.');
    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 < 50 && 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;
            }
          }
        }
      }
    }
  }

  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 < 50 && 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;
            }
          }
        }
      }
    }
  }

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

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

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

function recalculatePrice(){
	if(finalFixShipProd == 0){
      finalFixShipProd = 1;
      $.post('getdatapv2.php', {'dataval' : totqty}, getPostData, "json");
    }
  //$.post('getpid.php', {'pid1' : pid1, 'pid2' : pid2, 'qty': totqty}, getPostPidFinal, "json");
}

function getPostPidFinal(d,s){
 if(d.success == 1){
	
    //displayPriceAndQty(d.qty, d.price);
    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 = $('#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();
}

