var xmlHttp;
var origContents;
var htmlWait = 'Please wait <img src="/images/ajax-loader.gif" height="16" width="16" border="0">';

function GetXmlHttpObject() {
	var xmlHttp = null;
	try {
		xmlHttp = new XMLHttpRequest;
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}


function getNowPlaying() {
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
		return;
	}
	var url = "/ajaxShowPlaying.php";
	var postVals = "sid=" + Math.random();
	xmlHttp.onreadystatechange = stateChangedNowPlaying;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(postVals);
}


function stateChangedNowPlaying() {
	if (xmlHttp.readyState == 4) {
		document.getElementById("nowPlaying").innerHTML = xmlHttp.responseText;
	}
}

// listAdd
function addToList () {

	var cEmail = document.getElementById('listAddEmail').value;

	if (cEmail != 'your email') {

		xmlHttp = GetXmlHttpObject();
		if (xmlHttp == null) {
			return;
		}
		var url = "/ajaxAddList.php";
		var postVals = "sid=" + Math.random();
		postVals = postVals + '&email=' +  escape(cEmail);
		xmlHttp.onreadystatechange = stateChangedListAdd;
		xmlHttp.open('POST', url, true);
		xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		xmlHttp.setRequestHeader('Content-length', postVals.length);
		xmlHttp.setRequestHeader('Connection', 'close');
		xmlHttp.send(postVals);
	}

}

function stateChangedListAdd () {
	if (xmlHttp.readyState == 4) {
		origContents = document.getElementById('listAdd').innerHTML;
		document.getElementById('listAdd').innerHTML = '<span class="listAddMessage">' + xmlHttp.responseText + '</span>';
	}
}

function listAddResetForm () {
	document.getElementById('listAdd').innerHTML = origContents;
}

function URLDecode(str) {
	var regg = /\+/g;
	return unescape(String(str).replace(regg, ' '));
}

function OShideAll() {
	var theform = document.forms.form1;
	if (document.all || document.getElementById) {
		for (i = 0; i < theform.length; i++) {
			var formElement = theform.elements[i];
			if (formElement.type == 'text' ||
				formElement.type == 'submit') {
				formElement.style.visibility = 'hidden';
			}
		}
	}
}


function OSshowInput(inTxt, inBtn) {
	OShideAll();
	var eleTxt = document.getElementById(inTxt);
	var eleBtn = document.getElementById(inBtn);
	eleTxt.style.visibility = "visible";
	eleBtn.style.visibility = "visible";
}


function apToggleDisplay(szDivID, iState) {

        var obj = document.getElementById(szDivID);
		if (iState == 1) {
			obj.style.visibility = 'visible';
		} else {
			obj.style.visibility = 'hidden';
		}



return;


    if(document.layers)	   //NN4+
    {
       document.layers[szDivID].visibility = iState ? "show" : "hide";
    }
    else if(document.getElementById)	  //gecko(NN6) + IE 5+
    {
        var obj = document.getElementById(szDivID);
        obj.style.visibility = iState ? "visible" : "hidden";
    }
    else if(document.all)	// IE 4
    {
        document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
    }
}
function apGetCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


function apProductAvailable() {

var stl = apGetCheckedValue(document.form1.gender);
var sz = apGetCheckedValue(document.form1.size);


	if (stl.length == 0 || sz.length == 0) { 
	  return;
	}
	
	var strSend = stl + '_' + sz;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
	  alert ("Your browser does not support AJAX!");
	  return;
	} 
	var url="/ajaxApparelAvailability.php";
	url=url+"?q="+strSend;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=apStateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

function apStateChanged() {


	var arrParts;
	if (xmlHttp.readyState==4) { 
	
		arrParts = xmlHttp.responseText.split('|');

	apToggleDisplay('btnSubmit', arrParts[0]);
	
	document.getElementById('avail').innerHTML = arrParts[1];

	}
}

function apPreloadImages() {
	var d = document;
	if (d.images) {
		if (!d.MM_p) {
			d.MM_p = new Array;
		}
		var i, j = d.MM_p.length, a = apPreloadImages.arguments;
		for (i = 0; i < a.length; i++) {
			if (a[i].indexOf("#") != 0) {
				d.MM_p[j] = new Image;
				d.MM_p[j++].src = a[i];
			}
		}
	}
}

function apSwapImage(intImage) {
	switch (intImage) {
	 case 1:
	   document.imgShirt.src = "/images/tshirt_noisechan_man.gif"
	   return(false);
	case 2:
	   document.imgShirt.src = "/images/tshirt_noisechan_woman.gif"
	   return(false);
	 }
}


function COSisValidEmail(str) {
	return (str.indexOf('@') > 0);
}

function COScheckForm(theform) {

	var msg = 'Please check the following:\n\n';
	var retVal = true;

	if(!COSisValidEmail(theform.email.value)){
		msg = msg + '* Enter a valid email address \n';
		retVal = false;
	}
	
	if(theform.address_first_name.value.length < 2){
		msg = msg + '* Enter your first name \n';
		retVal = false;
	}

	if(theform.address_last_name.value.length < 2){
		msg = msg + '* Enter your last name \n';
		retVal = false;
	}

	if(theform.address_address1.value.length < 2){
		msg = msg + '* Enter your address \n';
		retVal = false;
	}

	if(theform.address_city.value.length < 2){
		msg = msg + '* Enter your city \n';
		retVal = false;
	}

	if(theform.address_state.value.length == 0 || theform.address_state.value.length < 2){
		msg = msg + '* Enter your state/provence/prefecture/etc. \n';
		retVal = false;
	}

	if(theform.address_zip.value.length < 2){
		msg = msg + '* Enter your Zip or postal code \n';
		retVal = false;
	}

	if(theform.address_country.value.length == 0){
		msg = msg + '* Select your country \n';
		retVal = false;
	}

	if (retVal == false) {
		alert(msg);
	}
	
	return retVal;
}

// checkout billing stuff...
function COBtoggleInfo() {
	if(document.forms['form1'].sameinfo.checked) {
		document.forms['form1'].address_first_name.value = unescape(ship_first_name);
		document.forms['form1'].address_last_name.value = unescape(ship_last_name);
		document.forms['form1'].address_company.value = unescape(ship_company);	
		document.forms['form1'].address_address1.value = unescape(ship_address1);
		document.forms['form1'].address_address2.value = unescape(ship_address2);
		document.forms['form1'].address_city.value = unescape(ship_city);
		// document.forms['form1'].address_state_us.value = unescape(ship_state_us);
		// document.forms['form1'].address_state.value = unescape(ship_state);
		document.forms['form1'].address_zip.value = unescape(ship_zip);
		// document.forms['form1'].address_country.value = unescape(ship_country);
		document.forms['form1'].address_phone.value = unescape(ship_phone);
		// document.forms['form1'].address_fax.value = unescape(ship_fax);


		countryStateInit(unescape(ship_country),'address_country', unescape(ship_state), 'address_state');

		if (ship_phone == '') {
			alert('Please make sure to enter your phone number');
		}
	} else { 
		document.forms['form1'].address_first_name.value = unescape(clear_first_name);
		document.forms['form1'].address_last_name.value = unescape(clear_last_name);
		document.forms['form1'].address_company.value = unescape(clear_company);
		document.forms['form1'].address_address1.value = unescape(clear_address1);
		document.forms['form1'].address_address2.value = unescape(clear_address2);
		document.forms['form1'].address_city.value = unescape(clear_city);
		// document.forms['form1'].address_state_us.value = unescape(clear_state_us);
		// document.forms['form1'].address_state.value = unescape(clear_state);
		document.forms['form1'].address_zip.value = unescape(clear_zip);
		// document.forms['form1'].address_country.value = unescape(clear_country);
		document.forms['form1'].address_phone.value = unescape(clear_phone);
		// document.forms['form1'].address_fax.value = unescape(clear_fax);

		countryStateInit(unescape(clear_country),'address_country', unescape(clear_state), 'address_state');
	}
}

function COBcheckForm(theform) {

	var msg = 'Please check the following:\n\n';
	var retVal = true;
	
	if(theform.address_first_name.value.length < 2){
		msg = msg + '* Enter your first name \n';
		retVal = false;
	}

	if(theform.address_last_name.value.length < 2){
		msg = msg + '* Enter your last name \n';
		retVal = false;
	}

	if(theform.address_address1.value.length < 2){
		msg = msg + '* Enter your address \n';
		retVal = false;
	}

	if(theform.address_city.value.length < 2){
		msg = msg + '* Enter your city \n';
		retVal = false;
	}

	if(theform.address_state.value.length == 0 || theform.address_state.value.length < 2){
		msg = msg + '* Enter your state/provence/prefecture/etc. \n';
		retVal = false;
	}

	if(theform.address_zip.value.length < 2){
		msg = msg + '* Enter your Zip or postal code \n';
		retVal = false;
	}

	if(theform.address_country.value.length == 0){
		msg = msg + '* Select your country \n';
		retVal = false;
	}

	if(theform.address_phone.value.length < 2){
		msg = msg + '* Enter your phone number \n';
		retVal = false;
	}

	// check ccnum
	if(theform.cc_num.value.length < 6 || theform.cc_num.value.length > 32){
		msg = msg + '* Enter a valid credit card number\n';
		retVal = false;
	}

	// check cc_expires_month and cc_expires_year
	if(theform.cc_expires_month.value.length == 0 || theform.cc_expires_year.value.length == 0){
		msg = msg + '* Enter a valid credit card expiration date\n';
		retVal = false;
	}

	// check card_type
	if(theform.card_type.value.length == 0){
		msg = msg + '* Enter a valid credit card type\n';
		retVal = false;
	}
	
	if (retVal == false) {
		alert(msg);
	}
	
	return retVal;

}

var formerrormsg="You\'ve attempted to submit the form more than one time.\n Please wait."

function checksubmit(submitbtn){
	submitbtn.form.submit()
	checksubmit=blocksubmit
	return false
}

function blocksubmit(){
	if (typeof formerrormsg!="undefined")
	alert(formerrormsg)
	return false
}


function CFcheckForm(theform) {

	var msg = 'Please check the following:\n\n';
	var retVal = true;

	if(theform.address_city.value.length < 2){
		msg = msg + '* Enter your city \n';
		retVal = false;
	}

	if(theform.address_state.value.length < 2){
		msg = msg + '* Enter your state/provence/prefecture/etc. \n';
		retVal = false;
	}

	if(theform.address_zip.value.length < 2){
		msg = msg + '* Enter your Zip or postal code \n';
		retVal = false;
	}

	if(theform.address_country.value.length == 0){
		msg = msg + '* Select your country \n';
		retVal = false;
	}

	if (retVal == false) {
		alert(msg);
	} else {
		document.getElementById('cfFrmInput').innerHTML = '    Please wait.....    ';
	}
	return retVal;
}


function clearText(frmElement) {
	frmElement.value='';
}

function submitCartForm() {
	document.frmCart.submit();
}

function cartAdd(prodId) {
	var elIe = 'addToCart' + prodId;
	document.getElementById(elIe).innerHTML = '<span class="pleaseWait">please wait</span>';
	var url = '/cart.php?add=' + prodId;
	window.location = url;
}

function signIn() {
	document.frmSignIn.submit();

}

function switchPaymentMethod() {
	var selectedVal = document.getElementById('affilPayMethod').value;
	if (selectedVal == 'Company Check') {
		document.getElementById('affilPaymentMethodPayPal').style.display = 'none';
		document.getElementById('affilPaymentMethodCheck').style.display = 'block';
	} else {
		document.getElementById('affilPaymentMethodPayPal').style.display = 'block';
		document.getElementById('affilPaymentMethodCheck').style.display = 'none';
	}
}

function AFILREGcheckForm(theform) {

	var msg = 'Please check the following:\n\n';
	var retVal = true;

	if(!COSisValidEmail(theform.email.value)){
		msg = msg + '* Enter a valid email address \n';
		retVal = false;
	}

	if(theform.passwd.value.length < 2){
		msg = msg + '* Enter a password \n';
		retVal = false;
	}

	if(theform.passwd_confirm.value.length < 2){
		msg = msg + '* Enter a password confirmation \n';
		retVal = false;
	}

	if(theform.passwd.value != theform.passwd_confirm.value){
		msg = msg + '* Your password and password confirmation do no match \n';
		retVal = false;
	}

	if(theform.first_name.value.length < 2){
		msg = msg + '* Enter your first name \n';
		retVal = false;
	}

	if(theform.last_name.value.length < 2){
		msg = msg + '* Enter your last name \n';
		retVal = false;
	}

	if(theform.address1.value.length < 2){
		msg = msg + '* Enter your address \n';
		retVal = false;
	}

	if(theform.city.value.length < 2){
		msg = msg + '* Enter your city \n';
		retVal = false;
	}

	if(theform.state.value.length == 0 || theform.state.value.length < 2){
		msg = msg + '* Enter your state/provence/prefecture/etc. \n';
		retVal = false;
	}

	if(theform.zip.value.length < 2){
		msg = msg + '* Enter your Zip or postal code \n';
		retVal = false;
	}

	if(theform.country.value.length == 0){
		msg = msg + '* Select your country \n';
		retVal = false;
	}

	if(theform.phone.value.length < 7){
		msg = msg + '* Enter your phone number \n';
		retVal = false;
	}

	if(theform.website_url.value.length < 8){
		msg = msg + '* Enter your website address \n';
		retVal = false;
	}

	var v = new RegExp();
	v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_~%&\?\/.=]+$");
	if (!v.test(theform.website_url.value)) {
		msg = msg + '* Enter a vaild website address \n';
		retVal = false;
	}

	if (theform.country.value != 'US' && theform.payment_method.value != 'PayPal') {
		msg = msg + '* Affiliates outside of the USA must choose PayPal as their payment method \n';
		retVal = false;
	}

	if (theform.payment_method.value == 'PayPal' && !COSisValidEmail(theform.paypal_account.value)) {
		msg = msg + '* Enter a valid PayPal account (email address) \n';
		retVal = false;
	}

	if (theform.payment_method.value == 'Company Check' && theform.payable_to.value.length < 3) {
		msg = msg + '* Enter the name to write checks to \n';
		retVal = false;
	}

	if (theform.accepted.checked == false) {
		msg = msg + '* You must read and agree to the affiliate program terms and conditions \n';
		retVal = false;
	}

	if (retVal == false) {
		alert(msg);
	}
	
	return retVal;

}


