var glbElem;
var vf = new Array("fname", "lname", "email", "address", "city", "state", "zip");
var errMsgs = new Array("First Name", "Last Name", "Email Address", "Address", "City", "State", "Zip Code");

function loadPaymentType(ptype){
	jQuery("div.payment_type").slideUp();
	if(ptype == 'cc'){
		jQuery("#creditcard").slideDown();
	}
	if(ptype == 'pp'){
		jQuery("#paypal").slideDown();
	}
}

function verifyAgreement(){
	var agree = document.getElementById('agree_id');
	var valid = true;
	if(agree.checked){
		document.getElementById('form_overlay').style.visibility = 'visible';
		document.getElementById('form_indicator').style.visibility = 'visible';
	}
	else{
		alert('To complete the transation, you must agree to the terms of the store.');
		agree.focus();
		valid = false;
	}
	return valid;
}

function verifyPersonalInfo(){
	var ph = document.getElementById('phone_id');
	var p_area = document.getElementById('area_id');
	var p_pre = document.getElementById('pre_id');
	var p_num = document.getElementById('number_id');
	ph.value = p_area.value + p_pre.value + p_num.value;
	var valid = verifyInfo(null);
	if(valid){
		valid = verifyCreditCard();
	}
	return valid;
}

function verifyCreditCard(){
	var valid = true;
	var iVal = new String();
	document.getElementById('cc_exp_id').value = document.getElementById('month_id').value + document.getElementById('year_id').value;
	var cc_elems = new Array("cc_type", "cc", "cc_exp", "csc");
	var errMsg = new Array("Credit Card Type", "Credit Card Number", "Credit Card Expiration", "Credit Card Security Code");
	var msg = "Missing required Information";
	for(var i=0;i<cc_elems.length;i++){
		iVal = document.getElementById(cc_elems[i]+"_id").value;
		iVal = iVal.trim();
		if(cc_elems[i] == "cc_exp"){
			if(iVal.length != 6){
				valid = false;
				jQuery("#cc_exp_lbl").css('color', 'red');
				msg += "\n"+errMsg[i];
			}
		}
		else{
			if(iVal == 0 || iVal.length == 0){
				valid = false;
				jQuery("#"+cc_elems[i]+"_lbl").css('color', 'red');
				msg += "\n"+errMsg[i];
			}
		}
	}
	if(! valid){
		alert(msg);
	}
	return valid;
}

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function loadCategory(cid){
	toggleOverlay();
	jQuery('li.categories').css('font-weight', 'normal');
	jQuery('#cat_'+cid).css('font-weight', 'bold');
	xajax_loadCategory(cid);
}

function loadSubCategory(sid){
	toggleOverlay();
	jQuery('span.link').css('font-weight', 'normal');
	jQuery('#sub_'+sid).css('font-weight', 'bold');
	xajax_loadSubCategory(sid);
}

function toggleOverlay(){	
	if(visible == "hidden")
		visible = "visible";
	else
		visible = "hidden";
	document.getElementById('form_overlay').style.visibility = visible;
	document.getElementById('form_indicator').style.visibility = visible;
}

function loadThumb(pict){
	var a_obj = document.getElementById("athumb_"+pict);
	hs.expand(a_obj, {preserveContent: false, dimmingOpacity: 0.75});
	return false;
}

function validEmail(email){
    var tValid = true;
    if((email.length < 8) ||
        ((email.length>0) && (! email.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi)))){
        tValid = false;
    }
    return tValid;
}

function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57)){
        if(charCode == 32 || (charCode >= 44 && charCode <= 46)){
            return true;
        }
        else{
            return false;
        }
    }
    return true;
}

function clearWhiteSpace(){
	var txt, i;
	txt = document.getElementsByTagName('input');
	for(i=0; i<txt.length; i++){
		if(txt[i].type == 'text' && txt[i].value.match(/^\s*$/)){
			txt[i].value = '';
		}
	}
}

function delayedSetFocus(){
	if(glbElem != null){
    	glbElem.focus();
	}
}

function verifyInfo(elem){
	clearWhiteSpace();
	jQuery("label.fields").css('color', 'black');
	first = true;
	var errMsg = "Missing Required Information:";
	var valid = true;
	var item = '';
	var iVal = new String();
	
	for(var i=0;i<vf.length;i++){
		item = document.getElementById(vf[i]+"_id");
		iVal = item.value;
		iVal = iVal.trim();
		switch (vf[i]){
			case 'email':
				if(! validEmail(iVal)){
					valid = false;
					errMsg += "\n"+errMsgs[i];
					jQuery("#"+vf[i]+"_lbl").css('color', 'red');
					if(first){
						glbElem = item;
						first = false;
					}
				}
				break;
		case 'phone':
			if(iVal.length != 10){
				valid = false;
			  	errMsg += "\n"+errMsgs[i];
			  	jQuery("#"+vf[i]+"_lbl").css('color', 'red');
			  	if(first){
			  		glbElem = document.getElementById('area_id');
			    	first = false;
			  	}
			}
			break;
		default :
			if(iVal.length == 0 || iVal == 0){
				valid = false;
			  errMsg += "\n"+errMsgs[i];
			  jQuery("#"+vf[i]+"_lbl").css('color', 'red');
			  if(first){
			  	glbElem = item;
			    first = false;
			  }
			}
		}
	}
	if(! valid){
		document.getElementById('msg').innerHTML = "Missing Required Information...";
		alert(errMsg);
  		setTimeout('delayedSetFocus()', 100);
	}
	return valid;
}

function contactValidate(){
	var r_fields = new Array("name", "phone", "email");
	jQuery("label.fields").css('color', 'black');
	prepPhone();
	var msg = "Missing required information:";
	var valid = true;
	var tmp = new String();
	for(var i=0; i<r_fields.length;i++){
		tmp = document.getElementById(r_fields[i]+'_id').value;
		tmp = tmp.trim();
		switch(r_fields[i]){
			case "email":
				if(! validEmail(tmp)){
					valid = false;
					msg += "\nValid Email Address";
					jQuery("#"+r_fields[i]+"_lbl").css("color", "red");
				}
				break;
			case "phone":
				if(tmp.length > 2 && tmp.length != 12){
					valid = false;
					msg += "\nValid Phone Number";
					jQuery("#"+r_fields[i]+"_lbl").css("color", "red");
				}
				break;
			default:
				if(tmp.length < 4){
					valid = false;
					msg += "\nValid Name";
					jQuery("#"+r_fields[i]+"_lbl").css("color", "red");
				}
		}
	}
	if(valid){
		xajax_submitContact(xajax.getFormValues('cinfo'));
	}
	else{
		document.getElementById('msg').innerHTML = 'Missing Required Information';
		alert(msg);
	}
	return false;
}

function prepPhone(){
	var c_area = document.getElementById('area_id').value;
	var c_pre = document.getElementById('pre_id').value;
	var c_num = document.getElementById('num_id').value;
	c_area = c_area.trim();
	c_pre = c_pre.trim();
	c_num = c_num.trim();
	document.getElementById('phone_id').value = c_area+'.'+c_pre+'.'+c_num;
}

function applyCoupons(){
	xajax_submitCoupons(jQuery("#coupon_codes_id").val());
	return false;
}

