function togCvn(obj)
{
    var cvnArea = document.getElementById('cvn_enabled');
    var cvnAmexNote = document.getElementById('cvn_disabled');
    
    if (cvnArea && cvnAmexNote) {
        //var cvnBox = document.getElementById('cvv');
	
        if (obj == 'Visa' || obj == 'MasterCard' || obj == 'Discover' || obj == 'American Express') {
            cvnArea.style.display = 'block';
            cvnAmexNote.style.display = 'none';
            //cvnBox.disabled = false;

            var cvnImg = document.getElementById('ys_cvnImage');
            var cvnAmexImg = document.getElementById('ys_cvnAmexImage');

            if ( cvnImg && cvnAmexImg ) {
                var ys_cvnAmexImg = document.getElementById('ys_ccAmexImg'); 
                if ( obj == 'American Express' ) {

                    if ( ys_cvnAmexImg ) {
                        cvnImg.style.display = 'none';
                        cvnAmexImg.style.display = 'block';
                    }
                    else {
                        cvnArea.style.display = 'none';
                        cvnAmexNote.style.display = 'block';
                    }
                }
                else {
                    cvnImg.style.display = 'block';
                    cvnAmexImg.style.display = 'none';
                }
            }
        }
        else {
            cvnArea.style.display = 'none';
            cvnAmexNote.style.display = 'block';
            //cvnBox.disabled = true;
        }
    }
}
/*
Script Name: Javascript Cookie Script
Author: Public Domain
*/

function getCookie( name ) {
	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

/*
only the first 2 parameters are required, the cookie name, the cookie
value. Cookie time is in milliseconds, so the below expires will make the 
number you pass in the Set_Cookie function call the number of days the cookie
lasts, if you want it to be hours or minutes, just get rid of 24 and 60.

Generally you don't need to worry about domain, path or secure for most applications
so unless you need that, leave those parameters blank in the function call.
*/
function setCookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	// if the expires variable is set, make the correct expires time, the
	// current script below will set it for x number of days, to make it
	// for hours, delete * 24, for minutes, delete * 60 * 24
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	//alert( 'today ' + today.toGMTString() );// this is for testing purpose only
	var expires_date = new Date( today.getTime() + (expires) );
	//alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

// this deletes the cookie when called
function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function alertNoCookie(ysco_custom_cookie_msg) {
    setCookie( 'ysco_check_cookie', 'none', '', '/', '', '' );
    if ( getCookie( 'ysco_check_cookie' ) ) {
        cookie_set = true;
        deleteCookie('ysco_check_cookie', '/', '');
    }
    else {
        alert(ysco_custom_cookie_msg);
        cookie_set = false;
    }
}

function setRadioHiddenFields(radioObj, hiddenIdPrefix)
{
    var fieldClicked = radioObj.value;

    var i = 0;
    var x = document.getElementById(hiddenIdPrefix+'_'+i);
    while(x) {
       if (x.id == fieldClicked) {
              x.value = 1;
           }
           else {
              x.value = 0;
           }

       i++;
       x = document.getElementById(hiddenIdPrefix+'_'+i);
    }
}

function enableButton(btnName, enableIt)
{
    var btnObjs = document.getElementsByName(btnName);
    if (btnObjs) {
        btnObjs[0].disabled = ! enableIt;
    }
}

// following for disable/enable div(s)

function addEvent(elm, evType, fn, useCapture) 
{
    if (elm.addEventListener) { 
        elm.addEventListener(evType, fn, useCapture); 
        return true; 
    }
    else if (elm.attachEvent) { 
        var r = elm.attachEvent('on' + evType, fn); 
        return r; 
    }
    else {
        elm['on' + evType] = fn;
    }
}

function getEventSrc(e) 
{
    if (!e) e = window.event;

    if (e.originalTarget)
        return e.originalTarget;
    else if (e.srcElement)
        return e.srcElement;
}

function addLoadEvent(func) 
{
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = 
            function() {
                oldonload();
                func();
            }
    }
}

/* Prototype getElementsByClass
http://www.dustindiaz.com/getelementsbyclass */
function getElementsByClass(node,searchClass,tag) {
	var classElements = new Array();
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function ys_initBillingAddr()
{
	var otherOpt = document.getElementById('useBillingRadio');
	var sameOpt = document.getElementById('useShippingRadio');
	var billingAddrInputs = document.getElementById('ys_billingInputs');
	var ccRadio = document.getElementById('ccSelectedRadio');
	if ( sameOpt && otherOpt && billingAddrInputs && ccRadio ) {
		addEvent(otherOpt,'click',ys_toggleBillingAddr,false);
		addEvent(sameOpt,'click',ys_toggleBillingAddr,false);
		addEvent(ccRadio,'click',ys_toggleBillingAddrCC,false);
		ys_toggleBillingAddr();
	}
	else if ( sameOpt && otherOpt && billingAddrInputs ) {
		addEvent(otherOpt,'click',ys_toggleBillingAddr,false);
		addEvent(sameOpt,'click',ys_toggleBillingAddr,false);
		ys_toggleBillingAddr();
	}
}

function ys_toggleBillingAddrCC() {
	window.setTimeout("ys_toggleBillingAddr()",5);
}

function ys_toggleBillingAddr()
{
	var sameOpt = document.getElementById('useShippingRadio');
	var disabled = sameOpt.checked ? true : false;
	var className = sameOpt.checked ? 'dis' : '';

	var billingAddrInputs = document.getElementById('ys_billingInputs');
	var inputList = billingAddrInputs.getElementsByTagName('input');
	var selectList = billingAddrInputs.getElementsByTagName('select');
	for (var i=0; i<inputList.length; i++) {
		//inputList[i].disabled = disabled;
                if (inputList[i].getAttribute('type') != 'hidden') {
		   inputList[i].disabled = disabled;
                }
	}
	for (var i=0; i<selectList.length; i++) {
		selectList[i].disabled = disabled;
	}
	billingAddrInputs.className = className;
}

function ys_initFormCheck(actor)
{
	ys_chkSubmitOnce = true;
	ys_submitBtnObj = actor;
}

function ys_chkFormSubmit() 
{
	if (ys_formSubmitted && ys_chkSubmitOnce) {
		if (ys_submitBtnObj) {
			ys_submitBtnObj.disabled = true;
		}
		return false;
	}
	else {
		ys_formSubmitted = true;
		if (ys_submitBtnObj) {
			ys_submitBtnObj.className += ' dis';
		}
		return true;
	}
}

Array.prototype.inArray = function (value) 
{
    var i;
    for (i=0; i < this.length; i++) {
        if (this[i] == value)
            return true;
    }

    return false;
};

var groupTog = {
	enablers: new Array(),

    findSrc : function(e) {
        var obj = getEventSrc(e);
        var tgt = obj.getAttribute('rel');
		if (tgt) {
        groupTog.applyActions(tgt);
		}
		else {
	groupTog.initToggleState();
		}
    },
	
    applyActions : function(targets) {
        var targetGroups = new Array();
        targetGroups = targets.split(' ');
        var groups = getElementsByClass(document,'togGroup','*');
        var groupsLen = groups.length;

        for ( i=0;i<groupsLen;i++ ) {
            var gId = groups[i].id;
        
            if ( targetGroups.inArray(gId) ) {
                // enable groups
                groups[i].className = (groups[i].className).toString().replace('dis','');
				// enable inputs
				groupTog.setInputDisableAtt(groups[i],false);
            } 
            else {
                // disable groups
                groups[i].className = (groups[i].className).toString().replace('dis','');
                groups[i].className += ' dis';
				// disable inputs
				groupTog.setInputDisableAtt(groups[i],true);
            }
        }
    },

	setInputDisableAtt: function(groupObj, state) {
		var inputList = groupObj.getElementsByTagName('input');
		var selectList = groupObj.getElementsByTagName('select');
		for (var i=0; i<inputList.length; i++) {
			//inputList[i].disabled = state;
                        if (inputList[i].getAttribute('type') != 'hidden') {
			   inputList[i].disabled = state;
                        }
		}
		for (var i=0; i<selectList.length; i++) {
			selectList[i].disabled = state;
		}
	},

	getEnablers : function(refresh) {
		if (refresh || groupTog.enablers.length < 1) {
 			groupTog.enablers = getElementsByClass(document,'togEnable','*');
		}
		return groupTog.enablers;
	},
	
	disableSoleTogGroup : function() {
		var groups = getElementsByClass(document,'togGroup','*');
		var groupsLen = groups.length;
		var pattern = new RegExp(' dis');

		for ( i=0;i<groupsLen;i++ ) {
			var gId = groups[i].id;
			var gClassName = groups[i].className;
			if (gClassName.match(pattern)) {
				groupTog.setInputDisableAtt(groups[i],true);
			}
		}
    },

    initToggleState : function() {
        var enablers = groupTog.getEnablers(false);
        if (enablers.length < 1) {
		groupTog.disableSoleTogGroup();
        }
        else {
            for ( i=0;i<enablers.length;i++ ) {
                if ( enablers[i].checked == true ) {
                    groupTog.applyActions(enablers[i].getAttribute('rel'));
                }
            }
        }
    },
	
	
    initTogglers : function() {
        var enablers = groupTog.getEnablers(true);
        for ( i=0;i<enablers.length;i++ ) {
           addEvent(enablers[i],'click',groupTog.findSrc,false);
        }
    }
};


/*
       Fix Selection Bug for label controls and 'for' attributes -> as per IE Bug:
       http://support.microsoft.com/default.aspx?scid=kb;en-us;314279&Product=iep
*/
function SelectOnFocusIn() {
	try {
		var eSrc = window.event.srcElement;
		if (eSrc) {
			eSrc.tmpIndex = eSrc.selectedIndex;
		}
	}
	catch (e) {
		return;
	}
}
function SelectOnFocus() {
	try {
		var eSrc = window.event.srcElement;
		if (eSrc) {
			eSrc.selectedIndex = eSrc.tmpIndex;
		}
	}
	catch (e) {
		return;
	}
}
/*
       Attach the selection methods to <select> elements in the DOM:
*/
var ieSelectionFix = function() {
	var selections = document.getElementsByTagName('select');
	for ( var i=selections.length-1; i >=0; --i ) {
		addEvent(selections[i], 'focusin', SelectOnFocusIn);
		addEvent(selections[i], 'focus', SelectOnFocus);
	}
};
addEvent(window, 'load', ieSelectionFix);


// prepare all page loading functions
function pageLoaders() {
  groupTog.initTogglers();
  groupTog.initToggleState();
  ys_initBillingAddr();
}

// assign pageLoaders function to window 'load'
addEvent(window, 'load', pageLoaders, false);

// above for disable/enable div(s)
//Check form to see if it's been submitted
var ys_formSubmitted = false;
var ys_chkSubmitOnce = false;
var ys_submitBtnObj = null;
