//**************************************************************************//
function messagebox(message, okfun, title, width, height){
	title = title || 'System Message';
	width = width || 450;
	height = height || 110;
	jslib.messagebox.show(width,0,title,message,function(){
		jslib.messagebox.hidden();
		if(typeof(okfun) == 'function')okfun();
	});
}

function loading(){
	jslib.messagebox.show(150, 30, null, '<img src="' + site_root_path + 'skin/system/images/loading.gif" align="absmiddle" /> Loading...');
}


function usecoupon(c){
	var code = jslib.$('code');
	if(!c && code)c = code.value;
	if(c && c.length > 2){
	    jslib.ajax.load('{0}service/usecoupon.ashx?code={1}'.format(site_root_path, escape(c)), function(request) {
	        eval(request.responseText);
	        if (typeof (result) != 'undefined' && result != null) {
	            switch (result.status) {
	                case 'Success':
	                    messagebox('Your promotion code has been successfully redeemed.', function(){
							if (typeof (onusecoupon_success) == 'function') onusecoupon_success(result.rate);
						});	                    
	                    break;
	                case 'Error':
	                    messagebox('Only one promotion code is allowed for each purchase.');
	                    break;
	                case 'TargetUnmatched':
	                    messagebox('Invalid Promotion Code!<br />Please use this code for {0}'.format(result.target));
	                    break;
					case 'Overflow':
						messagebox('The maximum discount rate for current certification is 50%. The discount code you are using exceeds this limit.');
						break;
	                default:
	                    messagebox('Promotion code does not exist or has been used!');
	                    break;
	            }
	            result = null;
	        }
	    });
		if(code)code.value = '';
	}else{
		messagebox('Incorrect promotion code.');
	}
}

function usepoint(){
	var pointamount = jslib.$('pointamount');
	if(pointamount && pointamount.value != '' && !isNaN(pointamount.value)){
	    jslib.ajax.load('{0}service/shopping.ashx?do=usepoints&points={1}'.format(site_root_path, pointamount.value), function(request) {
			var data = eval('(' + request.responseText + ')');
			switch(data.result){
				case 'success':
					messagebox('Your award points have been successfully redeemed.', function(){
						if (typeof (onusepoint_success) == 'function'){
							onusepoint_success(pointamount.value);
						}else{
							self.location.href = self.location.href;
						}
					});
					pointamount.value = '0';
					break;
				case 'fail':
					messagebox('The award points you use exceed current order value. The maximum award points you can use towards this order is ' + data.max + '.');
					break;
				default:
					messagebox('Insufficient Points.');
					pointamount.value = '0';	
					break;
			}
	    });			
	}
}


function submit_contact(form){
	if(!jslib.form.validator.isvalid(form))return false;

	loading();
	return jslib.ajax.form.submit(form, function(request){
		if(request.responseText == 'success'){
			messagebox('Thank you for your inquiry!\r\nYour requst will be attended to within 24 hrs.',function(){
				self.location.reload();
			});
		}else{
			messagebox(request.responseText);
		}
	});

	return false;
}

function on_register_certification(form){
	loading();
	if(!jslib.form.validator.isvalid(form))return false;

	var interestedcertification = jslib.$('interestedcertification');
	if(interestedcertification){
		var certification = jslib.$('certification');
		if(certification && certification.selectedIndex.value!=''){
			jslib.$('interestedcertification').value=certification.value+'|'+certification.options[certification.selectedIndex].text;
		}
	}

	return jslib.ajax.form.submit(form, function(request){
		if(request.responseText != 'success'){
			messagebox(request.responseText);
		}else{
			document.cookie = 'regsuccess=1;path=/';
			var referUrl = jslib.$('ReferUrl').value;
			var message = referUrl.indexOf('/checkout.ashx') != -1 ? 'Your login account has been successfully created. Please continue to complete your order.' : 'Your login account has been created. Please continue to use our system.';
			messagebox(message, function(){
				self.location.href = jslib.$('ReferUrl').value || '/members/home.ashx?do=welcome';
			});
		}
	});

	return false;
}