$j(document).ready(function(){
	$j(".textbox_change").blur(function(){
		var base = $j(this).attr('change');
		var value = $j(this).val();
		if(value == ''){
			$j(this).val(base);
			$j(this).css("color", '');
		}
	});

	$j(".textbox_change").focus(function(){
		var base = $j(this).attr('change');
		var value = $j(this).val();
		if(value == base){
			$j(this).val('');
			$j(this).css("color", '#000');
		}
	});

	
});

var start_panel = {
		onclick:function(obj){
			var div=$j('#start_panel_menu');
			var divC=div.children();
			div.css('display','block');
			divC=$j(divC[0]).children();
			
			start_panel.highlightli(divC[0]);
		},
		check:function(e){//check (for hide)
			var target = (e && e.target) || (event && event.srcElement);
			var obj = $j('#start_panel_menu');
			var obj2 = $j('#5tart_menu_btn');
			start_panel.checkParent(target)?obj.css('display','none'):null;
			target==obj2?start_panel.onclick($j('#5tart_menu_btn')):null;
		},
		checkParent:function(t){
			while(t.parentNode){
				if(t==document.getElementById('start_panel_menu') || t==document.getElementById('5tart_menu_btn')){
					return false;
				}
				t=t.parentNode;
			}
			return true;
		},
		highlightli:function(obj){
			var div=$j('#start_panel_menu');
			var pChildren;
			
			div.children().each(
				function(o,p){
					p=$j(p);
					if(!(p.hasClass('separator'))){
						pChildren=p.children();
						if(pChildren.length){
							$j(pChildren[0]).removeClass('start_menu_item_open');
							if(pChildren.length>1){
								$j(pChildren[1]).css({display:'none',visibility:'hidden'});
							}
						}
					}
				}
			); //each
			
			var objpos = ObjPosition.get(obj);
		
			$j(obj).addClass('start_menu_item_open');
			pChildren=$j(obj.parentNode).children();
			if(pChildren.length>1){
				//13:paddings,margins,and etcs..
				$j(pChildren[1]).css({display:'block',visibility:'visible',left:(div.width()+13)+'px',minHeight:(div.height()+4)+'px'});
				var objpos2 = ObjPosition.get(pChildren[1]);
				var objChildren2 = $j(pChildren[1]).children();
				
				if(objChildren2.length>1){
					objChildren2[0].style.marginTop = (objpos.top-objpos2.top)+'px';
				}
			}
		}
};

var subscribe = {
		onclick:function(){},
		personal_info:function(value,dataFullname,dataEmail,dataErrMsg){
			var d = new Dialog(Dialog.prototype.DIALOG_POP,'subscribe_cc');
			var startStyle=dataErrMsg!='' ? 'display:block' : 'display:none';
			var errMsg=dataErrMsg;
			var html = '<div class="alert" id="alertdiv" style="'+startStyle+'"><div class="inner_alert" id="alerttext">'+errMsg+'</div></div>';
			html += '<h3 class="account_lock">Provide New Account Information</h3>';
			html += '<div class="clearfix">';
			html += '<div class="left cc_form"><form id="cc_form">';
			html += '<div class="formRow clearfix">';
			html += '<label>Full Name:</label>';
			html += '<div class="formValue"><input type="text" class="inputtext" name="fullname" value="'+(dataFullname?dataFullname:'')+'"></div>';
			html += '</div>';
			html += '<div class="formRow clearfix">';
			html += '<label>Your Email:</label>';
			html += '<div class="formValue"><input type="text" class="inputtext" name="email" value="'+(dataEmail?dataEmail:'')+'"></div>';
			html += '</div>';
			html += '<div class="formRow clearfix">';
			html += '<label>New Password:</label>';
			html += '<div class="formValue"><input type="password" class="inputpassword" name="password"></div>';
			html += '</div>';
			html += '</form></div>';
			html += '<div class="right cc_note_container">';
			html += '<span class="secure_site">Secure site</span><br>';
			html += '<div class="help_billing">Having problems?<br><b>Customer Service</b><br>1-888-377-0588</div>';
			html += '</div></div>';
			html += '</div>';
			
			d.showChoice('Subscribe',html,'Continue','Cancel');
	//		$(d._button_okay).addClass('right');
			d._button_cancel.style.background = 'transparent';
			d._button_cancel.style.border = '0';
			d._button_cancel.style.color = '#0000FF';
			d._button_cancel.style.cursor = 'pointer';
			d._button_cancel.style.outline = 'none';
			
			d.oncancel = function(){
				d.destroy();
			}
			d.onconfirm = function(){
				var newdata = $('cc_form').serialize(1);
				if(newdata.fullname && newdata.email && (newdata.password.length>=5)){
					d.destroy();
					subscribe.cc_info(value,newdata.fullname,newdata.email,newdata.password);
				}else{
					var t = 'Oops.. please provide your ';
					var l = 0;
					
					var required = ['fullname','email','password'];
					var reqd = [];
					for(i=0;i<3;i++){
						if(!newdata[required[i]]){
							switch(required[i]){
								case 'fullname':reqd.push('full name');break;
								case 'password':reqd.push('new password');break;
								default: reqd.push(required[i]);break;
							}
						}
					}
					if(reqd.length){
						l = reqd.length-2;
						for(i=0;i<reqd.length;i++){
							t += reqd[i];
							if(i!= (reqd.length-1)){
								if(reqd.length>1){
									if(i==l){
										t+= ' and ';
									}else{
										t+= ', ';
									}
								}
							}
						}
					}else{
						if(newdata['password'].length<5){
							t = 'Oops.. your password must be at least <b>5 characters</b> long.'
						}
					}
					$('alerttext').innerHTML = t+'.';
					$('alertdiv').style.display='block';
				}
			}
		},
		cc_info:function(value,dataFullname,dataEmail,dataPassword){
			var values = (value.split('_'));
			var d = new Dialog(Dialog.prototype.DIALOG_POP,'subscribe_cc');
			var html = '<div class="alert" id="alertdiv" style="display:none"><div class="inner_alert" id="alerttext"></div></div>';
			html += '<h3>Provide Credit Card Information</h3>';
			html += '<div class="clearfix">';
			html += '<div class="left cc_form"><form id="cc_form">';
			html += '<div class="formRow clearfix">';
			html += '<label>Name on Card:</label>';
			html += '<div class="formValue"><input name="ccName" id="ccName" type="text" class="inputtext" value="'+dataFullname+'"></div>';
			html += '</div>';
			html += '<div class="formRow clearfix">';
			html += '<label>Credit Card Number:</label>';
			html += '<div class="formValue"><input name="ccNum" onkeypress="subscribe.getCCType(event,this);" id="ccNum" type="text" class="inputtext" style="background:url(images/lock.gif) no-repeat 99% 50%"></div>';
			html += '</div>';		
			html += '<div class="formRow clearfix">';
			html += '<label>Credit Card Type</label>';
			html += '<div class="formValue"><select id="ccType"><option value="0">---</option><option>Visa</option><option>Mastercard</option><option>Discover</option><option>American Express</option></select></div>';
			html += '</div>';
			html += '<div class="formRow clearfix">';
			html += '<label>Expiration Date:</label>';
			html += '<div class="formValue"><select name="ccMonth" id="ccMonth"><option value="0">---</option><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option><option>11</option><option>12</option></select><select name="ccYear" id="ccYear"><option value="0">------</option><option>2008</option><option>2009</option><option>2010</option><option>2011</option><option>2012</option><option>2013</option><option>2014</option><option>2015</option><option>2016</option><option>2017</option></select></div>';
			html += '</div>';
			html += '<div class="formRow clearfix">';
			html += '<label>Phone Number:</label>';
			html += '<div class="formValue"><input name="phone" id="phone" type="text" class="inputtext"></div>';
			html += '</div>';
			
			html += '<div class="formRow clearfix">';
			html += '<label>&nbsp;</label>';
			html += '<div class="formValue"><input type="checkbox" name="chkTandC" id="chkTandC" value="1" /> I agree with the <a href="/TermsAndConditions.aspx" target="_blank">Terms and Conditions</a></div>';
			html += '</div>';
			
			html += '</form></div>';
			html += '<div class="right cc_note_container">';
			html += '<div class="cc_note">By clicking \'Complete\', you agree to our <a href="/TermsAndConditions.aspx" target="_blank">Terms of Service</a>, and your credit card will be billed';
			html += '<br><br><span class="secure_site">Secure site</span><br>';
			html += '<div class="help_billing">Having problems?<br><b>Customer Service</b><br>1-888-377-0588</div>';
			html += '</div></div>';
			html += '</div>';
			
			html += '<div class="amount_due">Your order total is <b>$'+values[2]+'</b> USD per '+values[1]+' for the '+values[0]+' Collection. <b class="sales">Plus applicable sales tax.</b></div>';
			d.showChoice('Subscribe',html,'Complete','Back');
			//$(d._button_okay).addClass('right');
			d._button_cancel.style.background = '#CCC';
			d._button_cancel.style.border = '1px solid #000';
			d._button_cancel.style.color = '#000';
			d._button_cancel.style.borderColor = '#FFF #000 #000 #FFF';
			
			d.oncancel = function(){
				d.destroy();
				subscribe.personal_info(value,dataFullname,dataEmail,'');
			}
			d.onconfirm = function(){
					var ccdata = $('cc_form').serialize(1);
					var err = '';
					
					//ccdata.ccNum = luhn_check(ccdata.ccNum);
					if(!luhn_check(ccdata.ccNum)){
						ccdata.ccNum = 0;
					}
					
					if(!ccdata.ccName.length){
						err = 'Oops.. please provide the credit card\'s name.';
					}else if(ccdata.ccNum == 0){
						err = 'Oops.. please double check the credit card number.';
					}else if(ccdata.ccMonth==0){
						err = 'Oops.. please provide the expiration month.';
					}else if(ccdata.ccYear==0){
						err = 'Oops.. please provide the expiration year.';
					}else if(!document.getElementById('chkTandC').checked){
						err = 'Oops.. please agree with terms and conditions.';	
					}else{
						__doPostBack('lbHandlePostbacks','fullname='+dataFullname+'&email='+dataEmail+'&password='+dataPassword+'&ccname='+ccdata.ccName+'&ccnum='+ccdata.ccNum+'&ccmonth='+ccdata.ccMonth+'&ccyear='+ccdata.ccYear+'&phone='+ccdata.phone);
					d.destroy();
					}
					
					if(err){
						$('alerttext').innerHTML = err;
						$('alertdiv').style.display='block';
					}
			}
		},
		subscribe_now:function(fullname,email,errMsg){
			var frm = $('form1').serialize(1);
			if(frm.subscription){
				subscribe.personal_info(frm.subscription,fullname,email,errMsg);
			}
		},getCCType:function(e,obj){
			/*Visa 4
			Mastercard 51-55
			Discover 6011, 60112-60114, 601174, 601177-601179, 601186-601199, 622126-622925, 644-649, 65
			American Express 34, 37*/
			
			var keynum, keychar;
			var type='';
			var value = obj.value;
	
			if(window.event){
				keynum = e.keyCode;
			}else if(e.which){
				keynum = e.which;
			}
			if(keynum==8){
				value = value * .1;
			}else{
				keychar = String.fromCharCode(keynum);
				value += keychar;
			}
			value = parseInt(value);
			
			if(value == 4){
				type=1;//type='Visa';
			}else if(value >= 34 && value <= 37){
				type=4;//type='American Express';
			}else if(value >= 51 && value <=55){
				type=2;//type='Mastercard';
			}else if(
					value == 6011 || 
					(value >= 60112 && value <= 60114) || 
					value == 601174 || 
					( value >= 601177 && value <= 601179) || 
					( value >= 601186 && value <= 601199) ||
					( value >= 622126 && value <= 622925) ||
					( value >= 644 && value <= 649) ||
					( value == 65 )
				){
				type=3;//type='Discover';
			}else if(value == 0){
				$('ccType').selectedIndex = 0;
			}
			if(type){
				$('ccType').selectedIndex = type;
			}
		}
};

function luhn_check(num) {
  // Strip any non-digits (useful for credit card numbers with spaces and hyphens)
  var number=num.replace(/\D/g, '');

  // Set the string length and parity
  var number_length=number.length;
  var parity=number_length % 2;

  // Loop through each digit and do the maths
  var total=0;
  for (i=0; i < number_length; i++) {
    var digit=number.charAt(i);
    // Multiply alternate digits by two
    if (i % 2 == parity) {
      digit=digit * 2;
      // If the sum is two digits, add them together (in effect)
      if (digit > 9) {
        digit=digit - 9;
      }
    }
    // Total up the digits
    total = total + parseInt(digit);
  }

  // If the total mod 10 equals 0, the number is valid
  if (total % 10 == 0) {
    return true;
  } else {
    return false;
  }
}

document.onclick=start_panel.check;
