$(document).ready(function() {
    dropDownProducts();
    dropDownExpert();
    swapImage();
    startSlideShowInterval();
    resizeFooter();
    $(window).resize(resizeFooter);
    ingredientsBox();
    macFix();
    $(".printPage").click(function(){ window.print();});
    tableStyle();
    boldLabel();
    unboldNone();    
});
var slideshowTime;
var slideshowTimeSkip;

function startSlideShowInterval() {
	slideshowTimeSkip = false;
	slideshowTime = setInterval(slideShow, 10000);
}



var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
//---------Validation date--------------
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
	    alert(strMonth);
		alert("Please enter a valid month")
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false;
	}
return true
}

//--------end validation date


function checkmail(e){
    var returnval=emailfilter.test(e)
return returnval
}


function CheckEmail(x)
// This function checks if  object.value  is a domainname, a subdomainname or an emailaddress
{
    return(checkmail(x));
}


function checkfield(e,valor)
{

if (e.value == valor)
    e.value="";   
return false;

}

function uncheckedfile(e,valor)
{
if (e.value == "")
    e.value=valor;   
    return false;

}

function swapImage(){

    $('#pictureContainerTwo').css("opacity", 0).show();
    
    //check if the browser is IE
    if($.browser.msie){
        
        $('#pictOne').click(function(e){
        e.preventDefault();
		    if(!$('#pictOne').hasClass('active')){
			    clearInterval(slideshowTime);
			    $('#pictureContainerTwo').stop().animate({ opacity: 0 }, 200, "swing").removeClass('contActive');
			    $('#newCurel').stop().hide();
			    $('.textContainerTwo').stop().hide();
			    $('#pictureContainerOne').stop().animate({ opacity: 1 }, 500, "swing").addClass('contActive').css('z-index',2);
			    $('#ultraHealing').stop().show();
			    $('.textContainerOne').stop().show();
			    $('#pictTwo').removeClass('pictTwoactive');
			    $('#pictOne').addClass('pictOneActive');
			    //startSlideShowInterval();
		    }
	    });
	
	    $('#pictTwo').click(function(e){
        e.preventDefault();
		    if(!$('#pictTwo').hasClass('active')){
			    clearInterval(slideshowTime);
			    $('#pictureContainerOne').stop().animate({ opacity: 0 }, 200, "swing").removeClass('contActive');
			    $('#ultraHealing').stop().hide();
			    $('.textContainerOne').stop().hide();
			    $('#pictureContainerTwo').stop().animate({ opacity: 1 }, 500, "swing").addClass('contActive').css('z-index',2);
			    $('#newCurel').stop().show();
			    $('.textContainerTwo').stop().show();
			    $('#pictOne').removeClass('pictOneActive');
			    $('#pictTwo').addClass('pictTwoactive');
			    //startSlideShowInterval();
		    }
	    });
    }
    else{
        $('#newCurel').css("opacity", 0).show();
        
        $('#pictOne').click(function(e){
        e.preventDefault();
		    if(!$('#pictOne').hasClass('active')){
			    clearInterval(slideshowTime);
			    $('#pictureContainerTwo').stop().animate({ opacity: 0 }, 500, "swing").removeClass('contActive');
			    $('#newCurel').stop().animate({ opacity: 0 }, 500, "swing");
			    $('.textContainerTwo').stop().hide();
			    $('#pictureContainerOne').stop().animate({ opacity: 1 }, 1000, "swing").addClass('contActive').css('z-index',2);
			    $('#ultraHealing').stop().animate({ opacity: 1 }, 500, "swing");
			    $('.textContainerOne').stop().show();
			    $('#pictTwo').removeClass('pictTwoactive');
			    $('#pictOne').addClass('pictOneActive');
			    //startSlideShowInterval();
		    }
		
	    });
	
	    $('#pictTwo').click(function(e){
        e.preventDefault();
		    if(!$('#pictTwo').hasClass('active')){
			    clearInterval(slideshowTime);
			    $('#pictureContainerOne').stop().animate({ opacity: 0 }, 500, "swing").removeClass('contActive');
			    $('#ultraHealing').stop().animate({ opacity: 0 }, 500, "swing");
			    $('.textContainerOne').stop().hide();
			    $('#pictureContainerTwo').stop().animate({ opacity: 1 }, 1000, "swing").addClass('contActive').css('z-index',2);
			    $('#newCurel').stop().animate({ opacity: 1 }, 500, "swing");
			    $('.textContainerTwo').stop().show();
			    $('#pictOne').removeClass('pictOneActive');
			    $('#pictTwo').addClass('pictTwoactive');
			    //startSlideShowInterval();
		    }
    		
	    });
    }
}

function slideShow() {
    
    if (slideshowTimeSkip) {
		slideshowTimeSkip = false;
		return;
	}
	//check if the browser is IE
	if($.browser.msie){
	    if($('#pictureContainerOne').hasClass('contActive')){
		    $('#pictureContainerOne').animate({ opacity: 0 }, 200, "swing").removeClass('contActive');
		    $('#ultraHealing').hide();
		    $('.textContainerOne').hide();
		    $('#pictureContainerTwo').animate({ opacity: 1 }, 200, "swing").addClass('contActive').css('z-index',2);
		    $('#newCurel').show();
		    $('.textContainerTwo').show();
		    $('#pictOne').removeClass('pictOneActive');
		    $('#pictTwo').addClass('pictTwoactive');
		    slideshowTimeSkip = true;
	    }
	    else if($('#pictureContainerTwo').hasClass('contActive')){
		    $('#pictureContainerTwo').animate({ opacity: 0 }, 200, "swing").removeClass('contActive');
		    $('#newCurel').hide();
		    $('.textContainerTwo').hide();
		    $('#pictureContainerOne').animate({ opacity: 1 }, 200, "swing").addClass('contActive').css('z-index',2);
		    $('#ultraHealing').show();
		    $('.textContainerOne').show();
		    $('#pictTwo').removeClass('pictTwoactive');
		    $('#pictOne').addClass('pictOneActive');
		    slideshowTimeSkip = true;
	    }    
	}
	else{
	    if($('#pictureContainerOne').hasClass('contActive')){
		    $('#pictureContainerOne').animate({ opacity: 0 }, 500, "swing").removeClass('contActive');
		    $('#ultraHealing').animate({ opacity: 0 }, 500, "swing");
		    $('.textContainerOne').hide();
		    $('#pictureContainerTwo').animate({ opacity: 1 }, 500, "swing").addClass('contActive').css('z-index',2);
		    $('#newCurel').animate({ opacity: 1 }, 500, "swing");
		    $('.textContainerTwo').show();
		    $('#pictOne').removeClass('pictOneActive');
		    $('#pictTwo').addClass('pictTwoactive');
		    slideshowTimeSkip = true;
	    }
	    else if($('#pictureContainerTwo').hasClass('contActive')){
		    $('#pictureContainerTwo').animate({ opacity: 0 }, 500, "swing").removeClass('contActive');
		    $('#newCurel').animate({ opacity: 0 }, 500, "swing");
		    $('.textContainerTwo').hide();
		    $('#pictureContainerOne').animate({ opacity: 1 }, 500, "swing").addClass('contActive').css('z-index',2);
		    $('#ultraHealing').animate({ opacity: 1 }, 500, "swing");
		    $('.textContainerOne').show();
		    $('#pictTwo').removeClass('pictTwoactive');
		    $('#pictOne').addClass('pictOneActive');
		    slideshowTimeSkip = true;
	    }
	
	}
}

function dropDownProducts() {
	var actualItem = $('ul li a#ctl00_header_products');
	
	$('#productsMenu').hover(function() {
	    $('#menuBox1').css('display', 'block');
		actualItem.css('backgroundPosition', '0 -32px');
	    
	}, function() {
		$('#menuBox1').css('display', 'none');
		if(!actualItem.hasClass('current')){actualItem.css('backgroundPosition', '0 0');}
		
	});
	
}
function dropDownExpert() {
	var actualItem = $('ul li a#ctl00_header_experts');
	$('#expertsMenu').hover(function() {
		$('#menuBox2').css('display', 'block');
		actualItem.css('backgroundPosition', '0 -32px');
		
	}, function() {
		$('#menuBox2').css('display', 'none');
		if(!actualItem.hasClass('current')){actualItem.css('backgroundPosition', '0 0');}
		
	});
}
function resizeFooter() {
    var screenHeight = $(window).height();
    var wrapperHeight = $('#wrapper').height();
    var footerHeight = $('#footer').height();
    var missedHeight;

    var actualHeigh = wrapperHeight + footerHeight;
    if (actualHeigh < screenHeight) {
        missedHeight = screenHeight - actualHeigh;
        footerHeight = footerHeight + missedHeight;
        $('#footer').css('height', (footerHeight - 28 + 'px'));

    }
    var screenWidth = $(window).width();
    var footerWidth = $('#footer').width();
    var missedWidth;

    if (footerWidth < screenWidth) {
       
        missedWidth = screenWidth - footerWidth;
        footerWidth = footerWidth + missedWidth;
        $('#footer').css('width', (footerWidth + 'px'));
        
    }
    
}
function ingredientsBox(){
	$('#a-ingredients').click(function(e){
	    e.preventDefault();
        if($(this).hasClass('ing-close')){
            $(this).removeClass('ing-close');
            $(this).addClass('ing-open');
        }
        else{
            $(this).removeClass('ing-open');
            $(this).addClass('ing-close');
        }
        
        $('.ingredients-box').slideToggle('2000');	
	});
}
function macFix(){
    if (navigator.userAgent.indexOf("Mac") > -1 && navigator.userAgent.indexOf("Firefox") > -1) {
        
    }  
    if (navigator.userAgent.indexOf("Mac") > -1 && navigator.userAgent.indexOf("Safari") > -1) {
        
    }
}
function tableStyle(){
    $('#ctl00_cphMainContent_heard_about_non_rx_products tr td:odd,#ctl00_cphMainContent_way_to_reach_you tr td:odd').addClass('row');
}
function boldLabel(){
    
    $('#form_advisor input').click(function(){
        if(this.checked == true){
            $(this).closest('li').addClass('bl');
        }
        else{
            $(this).closest('li').removeClass('bl');
        }
        
    })
    $('#skin_form input:checkbox').click(function(){
        if(this.checked == true){
            $(this).closest('li').addClass('bl');
        }
        else{
            $(this).closest('li').removeClass('bl');
        }
        
    })
     $('#radio-group-1 input:radio').click(function(){
        $('#radio-group-1 li').removeClass('bl');
        $(this).closest('li').addClass('bl');
     })
     $('#radio-group-2 input:radio').click(function(){
        $('#radio-group-2 li').removeClass('bl');
        $(this).closest('li').addClass('bl');
     })
     $('#radio-group-3 input:radio').click(function(){
        $('#radio-group-3 li').removeClass('bl');
        $(this).closest('li').addClass('bl');
     })
}
function noneCkeck(){
    $('#form_advisor label').closest('li').removeClass('bl');
}
function unboldNone(){
    $('#form_advisor input:not(#Checkbox17)').click(function(){
        $('#none').removeClass('bl');
        uncheckNone();
    })       
}
function uncheckNone(){
    document.getElementById("Checkbox17").checked = false;
}
