
var ship_name = "";
var ship_address = "";
var ship_city = "";
var  ship_state= "";
//var ship_phone = "";
var ship_zipcode = "";
var ship_country = "";
//var ShipStateIndex = 0;
//var ShipConfirm = 0;

function InitSaveVariables(form) {
ship_name = form.ship_name.value;
ship_address = form.ship_address.value;
ship_city = form.ship_city.value;
ship_state = form.ship_state.value;
//ship_phone = form.ship_phone.value;
ship_zipcode = form.ship_zipcode.value;

ship_country = form.ship_country.value;

//ShipStateIndex = form.ShipState.selectedIndex;
//ShipState = form.ShipState[ShipStateIndex].value;
//ShipConfirm = form.ShipConfirm.checked;
}

function ShipToBillPerson(form) {
if (form.copy.checked) {
InitSaveVariables(form);
form.ship_name.value = form.bill_name.value;
form.ship_address.value = form.bill_address.value;
form.ship_city.value = form.bill_city.value;
getStatesShip(form.bill_country.value,form.bill_state.value);
//form.ship_phone.value = form.bill_phone.value;
form.ship_zipcode.value = form.bill_zipcode.value;

form.ship_country.value = form.bill_country.value;
/*
form.ship_name.disabled =true;
form.ship_name.disabled = true;
form.ship_address.disabled =true;
form.ship_city.disabled = true;
form.ship_state.disabled = true;
form.ship_phone.disabled = true;
form.ship_country.disabled = true;
*/
}
else {
form.ship_name.value = ship_name;
form.ship_address.value = ship_address;
form.ship_city.value = ship_city;
form.ship_state.value = ship_state;
//form.ship_phone.value = ship_phone;
form.ship_zipcode.value = ship_zipcode;
form.ship_country.value = ship_country;
//form.ShipState.selectedIndex = ShipStateIndex;
//form.ShipConfirm.checked = ShipConfirm;

/*form.ship_name.disabled = false;
form.ship_name.disabled = false;
form.ship_address.disabled = false;
form.ship_city.disabled = false;
form.ship_state.disabled = false;
form.ship_phone.disabled = false;
form.ship_country.disabled = false;
*/

   }
}


function getStates(countryId, stateId){
    if(countryId != 'Other') {
  	var stateEl = $("#bill_state");
  	stateEl.find("option").remove();
  	$.ajax({ url: "nextStepState/"+countryId,
     dataType: "json",
     success: function(data){
     stateEl.append($(document.createElement("option")).val("").text("--Select--"));
     for (var i in data){
      stateEl.append($(document.createElement("option")).val(data[i].id).text(data[i].name));
     }     
     if (typeof stateId !== "undefined"){
      stateEl.val(stateId);
     }
     }
  });
  if($('#other_country')) {
   $('#other_country').remove();
  }
  $("#bill_state").attr("disabled", false);
 } else if(countryId == 'Other') {
  if($('#other_country')) {
   $('#other_country').remove();
  }
  $('.country').after('<input type="text" name="other_country" id="other_country" class="bodytxt" size="20" maxlength="50" value="'+$('#other_country_id').val()+'"/>');
  $("#bill_state").attr("disabled", "disabled");
 }
}

	
function getStatesShip(countryId, stateshipId){
    if(countryId != 'Other') {
  var stateEl = $("#ship_state");
  stateEl.find("option").remove();
  $.ajax({ url: "nextStepState/"+countryId,
     dataType: "json",
     success: function(data){
     stateEl.append($(document.createElement("option")).val("").text("--Select--"));
     for (var i in data){
      stateEl.append($(document.createElement("option")).val(data[i].id).text(data[i].name));
     }
     
     if (typeof stateshipId !== "undefined"){
      stateEl.val(stateshipId);
     }
     }
  });
  /*if($('#other_country')) {
   $('#other_country').remove();
  }*/
  $("#ship_state").attr("disabled", false);  
 } else if(countryId == 'Other') {/*
  if($('#other_country')) {
   $('#other_country').remove();
  }
  $('.country').after('<input type="text" name="other_country" id="other_country" class="bodytxt" size="20" maxlength="50" value="'+$('#other_country_id').val()+'"/>');
  $("#ship_state").attr("disabled", "disabled");*/
 }
}

 function checkCountryCode() {
	 var code = $('#country_code').val();
	 if(code == 'Other') {
	  if($('#other_country_code')) {
	   $('#other_country_code').remove();
	  }
	  $('.countrycode').after('<input type="text" name="other_country_code" id="other_country_code" class="bodytxt" size="6" maxlength="6" value=""/>');
	 } else {
	  $('#other_country_code').remove();
	 }
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


var popupStatus = 0;
//this code will load popup with jQuery magic!
function loadPopup(){
    //loads popup only if it is disabled
    if(popupStatus==0){
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#popupContact").fadeIn("slow");
        popupStatus = 1;
    }
}

//This code will disable popup when click on x!
function disablePopup(){
    //disables popup only if it is enabled
    if(popupStatus==1){
        $("#backgroundPopup").fadeOut("slow");
        $("#popupContact").fadeOut("slow");
        popupStatus = 0;
    }
}

//this code will center popup
function centerPopup(){
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popupContact").height();
    var popupWidth = $("#popupContact").width();
    //centering
    $("#popupContact").css({
        "position": "absolute",
        "top": windowHeight/2-popupHeight/2,
        "left": windowWidth/2-popupWidth/2
    });
    //only need force for IE6    
    $("#backgroundPopup").css({
        "height": windowHeight
    });
    
}

function showColorImg() {
	var color = $('#hair_selection').val();
	var all_divs = $('.all_col_images');
	if(color != '') {
		for(var i=0;i<all_divs.length;i++) {
			$(all_divs[i]).hide();
		}
		//$('#'+color).show();
		var child_img = $('#'+color).children();
		var baseArray = child_img[0].src.split('/');
		$('#doll_image').val(baseArray[baseArray.length-1]);
		$('#main_doll_img').attr('src',child_img[0].src);
	} else {
		for(var i=0;i<all_divs.length;i++) {
			$(all_divs[i]).hide();
		}
	}
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	if($('#popupContactClose').length) {
		$('img.captify').captify({
			// all of these options are... optional
			// ---
			// speed of the mouseover effect
			speedOver: 'fast',
			// speed of the mouseout effect
			speedOut: 'normal',
			// how long to delay the hiding of the caption after mouseout (ms)
			hideDelay: 500,	
			// 'fade', 'slide', 'always-on'
			animation: 'slide',		
			// text/html to be placed at the beginning of every caption
			prefix: '',		
			// opacity of the caption on mouse over
			opacity: '0.7',					
			// the name of the CSS class to apply to the caption box
			className: 'caption-bottom',	
			// position of the caption (top or bottom)
			position: 'bottom',
			// caption span % of the image
			spanWidth: '100%'
		});
		$(function() {
			$(".slider").jCarouselLite({
				btnNext: ".next",
				btnPrev: ".prev",
				visible: 4
			});
		});
		
		$(function() {
			$(".slider1").jCarouselLite({
				btnNext: ".next1",
				btnPrev: ".prev1",
				visible: 3
			});
		});
	
		if ($.cookie("anewsletter") != 1) {    
			//centering with css
			centerPopup();
			//load popup
			loadPopup();    
		}        
		//CLOSING POPUP
		//Click the x event!
		$("#popupContactClose").click(function(){
			disablePopup();
			$.cookie("anewsletter", "1", { expires: 7 });
		});
		//Click out event!
		$("#backgroundPopup").click(function(){
			disablePopup();
			$.cookie("anewsletter", "1", { expires: 7 });
		});
		//Press Escape event!
		$(document).keypress(function(e){
			if(e.keyCode==27 && popupStatus==1){
				disablePopup();
				$.cookie("anewsletter", "1", { expires: 7 });
			}
		});
	}
	if($('.rate_sys').length) {
		star_rating();
	}
	if($("#user").length) {
		$("#user").validate({
        rules: {
			first_name: "required",// simple rule, converted to {required:true}
			user_name: "required",// simple rule, converted to {required:true}
			dob_m: "required",// simple rule, converted to {required:true}
			dob_d: "required",
			dob_y: "required"
			
		}
		});
	}
	
	if($("#video").length) {
		$("#video").validate({
			rules: {
				video_name: "required",
				video_desc: "required",
				video: "required"
			}        
		});
	}
	if($("#doll").length) {
		$("#doll").validate({
			rules: {
				hair: "required"
			}        
		});
	}
	
	if($('.gallery a').length) {
		$(".thumb-top,.thumb-bottom").fadeTo("slow", 0.3);
		$(".thumb-top,.thumb-bottom").hover(function(){
		$(this).fadeTo("slow", 1.0);
		},function(){
		$(this).fadeTo("slow", 0.3);
		});
		
		$(function() {
			$('.gallery a').lightBox();
		});
	}
	
	if($("#ste2form").length) {
		$("#ste2form").validate({	  
        rules: {
          bill_name: "required",// simple rule, converted to {required:true}  
		   bill_address: "required",// simple rule, converted to {required:true}  
		    bill_city: "required",// simple rule, converted to {required:true}  
			 bill_country: "required",// simple rule, converted to {required:true}  q
			   bill_country: "required",// simple rule, converted to {required:true}  
				 bill_state: "required",// simple rule, converted to {required:true}  
			  		//bill_phone: { number: true, minlength:6, maxlength:12},// simple rule, converted to {required:true}  
					bill_zipcode: { required:true,number: true, minlength:5, maxlength:12},// simple rule, converted to {required:true}  
			  ship_name: "required",// simple rule, converted to {required:true}  
			   ship_address: "required",// simple rule, converted to {required:true}  
				ship_city: "required",// simple rule, converted to {required:true}  
				 ship_country: "required",// simple rule, converted to {required:true}  
				  ship_state: "required",// simple rule, converted to {required:true}  
				   ship_country: "required",// simple rule, converted to {required:true}  
				   // ship_phone: {  number: true, minlength:6, maxlength:12}// simple rule, converted to {required:true}  
					ship_zipcode: {  required:true, number: true, minlength:5, maxlength:12}// simple rule, converted to {required:true}  
        
        }        
      });
	  
	  $('#bill_country').change(function(){ 
   			getStates($('#bill_country').val());
  		});
		var countryId = $("#hid_bill_country").val();
  		var stateId = $('#hid_bill_state').val();
        if (countryId != "")
        {
            if (stateId != ""){
                getStates(countryId, stateId);
            }
            else{
                getStates(countryId);
            }					
        }  
  //$('#country_code').change(checkCountryCode);  
  	$('#ship_country').change(function(){ 
   		getStatesShip($('#ship_country').val());
  		});
		var countryId = $("#hid_ship_country").val();
  		var stateshipId = $('#hid_ship_state').val();
        if (countryId != ""){
            if (stateshipId != "") {
                getStatesShip(countryId, stateshipId);
            }
            else{
                getStatesShip(countryId);
            }
        }
	}
	if($("#image_rotate").length) {
		$('#image_rotate').innerfade({   
			speed: 'slow',   
			timeout: 2500,   
			type: 'sequence',   
			containerheight: '220px'  
    	});
		// image slider;
		$('img.captify').captify({
			// all of these options are... optional
			// ---
			// speed of the mouseover effect
			speedOver: 'fast',
			// speed of the mouseout effect
			speedOut: 'normal',
			// how long to delay the hiding of the caption after mouseout (ms)
			hideDelay: 500,	
			// 'fade', 'slide', 'always-on'
			animation: 'slide',		
			// text/html to be placed at the beginning of every caption
			prefix: '',		
			// opacity of the caption on mouse over
			opacity: '0.7',					
			// the name of the CSS class to apply to the caption box
			className: 'caption-bottom',	
			// position of the caption (top or bottom)
			position: 'bottom',
			// caption span % of the image
			spanWidth: '100%'
		});
		$(function() {
			$(".slider1").jCarouselLite({
				btnNext: ".next1",
				btnPrev: ".prev1",
				visible: 3
			});
		});
	}
	
	if($('#hair_selection').length) {
		$('#hair_selection').change(function(e) { 
			showColorImg();
		});
		showColorImg();
	}
});
