/* Front side Menu JS */
// Javascript originally by Patrick Griffiths and Dan Webb.
// http://htmldog.com/articles/suckerfish/dropdowns/
sfHover = function() {
    var sfEls = document.getElementById("navbar");
    if (typeof sfEls != 'undefined' && sfEls != null) {
        sfEls = sfEls.getElementsByTagName("li");
        for (var i = 0; i < sfEls.length; i++) {
            sfEls[i].onmouseover = function() { this.className += " hover"; }
            sfEls[i].onmouseout = function() { this.className = this.className.replace(new RegExp(" hover\\b"), ""); }
        }
    }
}
if(window.attachEvent){ window.attachEvent("onload", sfHover); }

$(document).ready(function(){
    $("#navbar > li.first > ul").each(function(i){
        //alert($(this).text());
        
        // Disable main links
        $(this).parent().find('a.firstlevel').attr("href", $(this).find("a:first").attr("href"))
        $(this).parent().find('a.firstlevel').click(function(){ return false; });
        
        switch(i){
            case 0:
                $(this).css("margin-left", "-7px");
            break;
            case 1:
                $(this).css("margin-left", "-26px");
            break;
            case 2:
                $(this).css("margin-left", "-27px");
            break;
            case 3:
                $(this).css("margin-left", "-5px");
            break;
            case 4:
                $(this).css("margin-left", "-7px");
            break;
            case 5:
                $(this).css("margin-left", "-30px");
            break;
            case 6:
                $(this).css("margin-left", "-5px");
            break;
            case 7:
                $(this).css("margin-left", "-27px");
            break;
            default:
            break;
        }
    });
    
    $("a[rel='playvideo']").colorbox({inline:true, href:"#videoHolder"});
    $("a[rel='lightbox']").colorbox();
    
    // $('#testimonials .slide');
    if($('#testimonials .slide').length > 0){
        setInterval(function(){
            $('#testimonials .slide').filter(':visible').fadeOut(1000, function(){
                if($(this).next('li.slide').size()){ $(this).next().fadeIn(1000); }
                else{ $('#testimonials .slide').eq(0).fadeIn(1000); }
            });
        }, 10000);
    }
    
    if($('ul.shop-bg .slide').length > 0){
        setInterval(function(){
            $('ul.shop-bg .slide').filter(':visible').fadeOut(1000, function(){
                if($(this).next('li.slide').size()){ $(this).next().fadeIn(1000); }
                else{ $('ul.shop-bg .slide').eq(0).fadeIn(1000); }
            });
        }, 3000);
    }
    
    if($('#divImageFlow').size() > 0){
        var Instance1 = new ImageFlow();
	    //Instance1.init({ ImageFlowID:'divImageFlow', imageFocusMax:5,  imageFocusM:1, reflectionP:0, captions:true,reflections:false, slider:true, buttons:true, startID:5 });
	    Instance1.init({ ImageFlowID:'divImageFlow', imageFocusMax:5, captions:false, reflections:false, slider:false, buttons:false });
    }
});
function DoesElementExists(objDOMElement){
    if(typeof objDOMElement != 'undefined' && objDOMElement != null) return true;
    return false;
}

function IsDate(strDate){ //Format is mm/dd/yyyy
  var validformat=/^\d{2}\/\d{2}\/\d{4}$/;
  var monthfield=strDate.split("/")[0];
  var dayfield=strDate.split("/")[1];
  var yearfield=strDate.split("/")[2];
  var dayobj = new Date(yearfield, monthfield-1, dayfield);
  if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
    return false;
  else
    return true;
}
function ShowHideCtrl(ShowCtrl,HideCtrl){$('#' + HideCtrl).slideUp('slow');$('#' + ShowCtrl).slideDown('slow');}
function ShowHideFader(ShowCtrl,HideCtrl){$('#' + HideCtrl).fadeOut();$('#' + ShowCtrl).fadeIn();
  $('#' + HideCtrl).css('display','none'); $('#' + ShowCtrl).css('display','block');
}
function ShowHideFaderWithNoBlock(ShowCtrl,HideCtrl){$('#' + HideCtrl).fadeOut();$('#' + ShowCtrl).fadeIn();
  $('#' + HideCtrl).css('display','none'); $('#' + ShowCtrl).css('display','');
}
function DisplMsg(Ctrl,ErrMsg,Msgclass){$('#'+ Ctrl).html(ErrMsg);$('#'+ Ctrl).attr('class',Msgclass);$('#'+ Ctrl).show();}

 function CheckUncheckallcheckbox(classname,type)
{
    if(type == 'check')
    {jQuery('.' + classname).each(function(){$(this).attr('checked','checked');});}
    else
    {jQuery('.' + classname).each(function(){$(this).attr('checked','');});}
}

function CalculateTotal(classname){
    var totalRec = 0;
    input = $('.' + classname);
    input.each(function(){
        if(jQuery(this).attr('checked')){totalRec += 1;}
    });
	$('#spanSelected').html(totalRec);
}
function checkQuantity(obj)
{
    var qty = document.getElementById(obj);
    if(qty.value != "")
    {
        if(isNaN(qty.value) == false)
        {
            if(parseInt(qty.value) <= 0)
            {
                alert("Quantity must be more than Zero.");
                qty.value="1";
                return false;
            }
            else
                return true;
        }
        else
        {
            alert("Please Enter only Numeric Value in Quantity Field.");
            qty.value="1";
            return false;
        }
    }
    else
    {
        alert("Please Enter Quantity.");
        qty.value="1";
        return false;
    }
}

//-validate credit card-//
function mod10( cardNumber ) 
{ 
	// LUHN Formula for validation of credit card numbers.
	var ar = new Array( cardNumber.length );
	var i = 0,sum = 0;

   	for( i = 0; i < cardNumber.length; ++i){ar[i] = parseInt(cardNumber.charAt(i));}
    
	for( i = ar.length -2; i >= 0; i-=2 ){ 
		// you have to start from the right, and work back.
    	ar[i] *= 2;		// every second digit starting with the right most (check digit)
   		if( ar[i] > 9 ) ar[i]-=9;	// will be doubled, and summed with the skipped digits.
   	}	// if the double digit is > 9, ADD those individual digits together 

   	for( i = 0; i < ar.length; ++i ){
    	sum += ar[i];	// if the sum is divisible by 10 mod10 succeeds
   	}
   	return (((sum%10)==0)?true:false);	 	
}

function expired( month, year){
   	var now = new Date();	// this function is designed to be Y2K compliant.
   	var expiresIn = new Date(year,month,0,0,0);		// create an expired on date object with valid thru expiration date
   	expiresIn.setMonth(expiresIn.getMonth()+1);		// adjust the month, to first day, hour, minute & second
                                                    //	of expired month
   	if(now.getTime() < expiresIn.getTime()){return false;}
    return true;	// then we get the miliseconds, and do a long integer comparison
}

function validateCreditCard(cardNumber,cardType,cardMonth,cardYear,cardCode){
    if(cardType.length == 0){ return "Please select credit card type.";	}
	
	if(cardNumber != null){
  	    if( cardNumber.length == 0 ){ return "Please enter valid card number."; }
    }
    if( cardCode.length == 0 ){ return "Please enter valid card code."; }
    
    if(cardNumber != null){
        for( var i = 0; i < cardNumber.length; ++i ){	
		    // make sure the number is all digits.. (by design)
            var c = cardNumber.charAt(i);

   		    if( c < '0' || c > '9' ){
        	    return "Please enter valid card number. Use only digits. do not use Spaces or Hyphens.";        	    
            }
        }
    }
    
	if(cardNumber != null){
	    var length = cardNumber.length;		//perform card specific length and prefix tests
    	
   	    switch( cardType ){
    	    case 'a':
			    if( length != 15 ){ return "Please enter valid American Express card number."; }
                var prefix = parseInt( cardNumber.substring(0,2));
   			    if( prefix != 34 && prefix != 37 ){ return "Please enter valid American Express card number."; }
            break;
            case 'd':
			    if( length != 16 ){ return "Please enter valid Discover card number."; }
                var prefix = parseInt( cardNumber.substring(0,4));
			    if( prefix != 6011 ){ return "Please enter valid Discover card number."; }
            break;
            case 'm':
			    if( length != 16 ){ return "Please enter valid MasterCard number."; }
                var prefix = parseInt( cardNumber.substring(0,2));
			    if( prefix < 51 || prefix > 55){ return "Please enter valid MasterCard number."; }
            break;
            case 'v':
			    if( length != 16 && length != 13 ){ return "Please enter valid Visa card number."; }
                var prefix = parseInt( cardNumber.substring(0,1));
			    if( prefix != 4 ){ return "Please enter valid Visa card number."; }
            break;
        }
        
	    if(!mod10(cardNumber)){ 	
		    // run the check digit algorithm
            return "Sorry! This is not a valid credit card number.";            
        }
    }
    
    if(cardMonth == ''){ return "Please select expiration month."; }
    if(cardYear == ''){ return "Please select expiration year."; }
    
    if(expired(cardMonth, cardYear)){
		// check if entered date is already expired.
        return "Sorry! The expiration date you have entered would make this card invalid.";
    }
    if (cardCode != ""){
		 for( var i = 0; i < cardCode.length; ++i ){
			var c = cardCode.charAt(i);
			if( c < '0' || c > '9' ){ return "Please enter valid card code. Use only digits."; }
		}
	}
	return "";	// at this point card has not been proven to be invalid
}

/* Front side Menu JS */
function EditorCheck(ctrl){
    // This is for Fck Editor checking
    for ( var name in FCKeditorAPI.Instances){
        var oEditor = FCKeditorAPI.Instances[ name ] ;
        if ( oEditor.GetParentForm && oEditor.GetParentForm() == ctrl )
        oEditor.UpdateLinkedField() ;
    }
    // end here
} 
