var bill_select, ship_select;

function PopUp(url, name, width, height, place, arg, scroll) {
	var top = 0;
	var left = 100;

	if (place && place == 'center') {
	  top = (768 - height)/2;
	  left = (1024 - width)/2;
	}

	scroll = (scroll) ? 'yes' : '0';
	window.open(url, name, 'width='+width+',height='+height+',menubar=no,scrollbars='+scroll+',toolbar=no,location=no,directories=no,resizable=no,top='+top+',left='+left);
}

function round (num, dec)
{ // low-level numeric format with upward rounding at 5+
 var cDec = '.'; // decimal point symbol
 if (!(dec >= 0 && dec <= 9))
  dec = 2;
 if (isNaN (num) || num == '')
 { // zero values are returned in proper decimal format
  var sdec = "";
  for (var i = 0; i < dec; i++)
   sdec += '0';
  return "0" + (sdec != "" ? cDec + sdec : "");
 }
 var snum = new String (num);
 var arr_num = snum.split (cDec);
 var neg = '';
 var nullify = 0;
 dec_a = arr_num.length > 1 ? arr_num[1].length : 0;
 if (dec_a <= dec)
 { // fill decimal places with trailing zeros if necessary
  if (!dec_a)
   arr_num[1] = "";
  for (var i = 0; i < dec - dec_a; i++)
   arr_num[1] += '0';
  dec_a = dec;
 }
 // total decimal places in value before rounding and formatting
 dec_i = dec_a;
 dec_a -= dec;
 if (arr_num[0].charAt(0) == '-')
 { // preserve negative symbol, remove from value (calculations)
  neg = '-';
  arr_num[0] = arr_num[0].substring (1, arr_num[0].length);
 }
 if (!parseInt (arr_num[0])) // case when whole value is 0
 { // nullify a zero whole value for correct decimal point placement
  arr_num[0] = "1"; // 0 whole # would not preserve amount in calc.
  nullify = 1; // flag to remove greatest 1 portion from whole #
 }
 var whole = parseInt (arr_num[0] * Math.pow (10, arr_num[1].length));
 // remove leading zeros
 for (i = 0; i < arr_num[1].length; i++)
  if (arr_num[1].charAt (i) != '0')
   break;
 if (arr_num[1].length == i) // decimal portion blank or all zeros
  return (neg + arr_num[0] + (arr_num[1] != "" ? (cDec + arr_num[1]) : ""));
 whole += parseInt (arr_num[1].substring (i, arr_num[1].length));
 if (arr_num[1].length != dec)
 { // round number affecting appropriate cluster of decimal places
  var diff = "";
  var str = new String (whole);
  for (i = dec_a; i > 0; i--)
   diff += str.charAt (str.length - i);
  diff = Math.pow (10, dec_a) - parseInt (diff);
  whole += ((diff <= 5 * Math.pow (10, dec_a - 1)) ? diff : 0);
 }
 str = new String (whole);
 var str_f = "";
 var j = 0;
 var k = 0;
 if (nullify)
 {
  arr_num[0] = "0"; // remove 1 from greatest decimal place (restoration)
  str = (parseInt (str.charAt(0)) - 1) + str.substring (1, str.length);
 }
 else // re-assign whole numeric portion from entire numeric string value
  arr_num[0] = str.substring (0, str.length - dec_i);
 for (i = 0; i < str.length; i++)
 { // combine portions of decimal number (whole, fraction, sign)
  if (k - 1 > dec)
   break; // fraction termination case
  if (j == arr_num[0].length)
  {
   if (!j)
    str_f += 0;
   str_f += (dec != 0 ? cDec : ''); // insert decimal point
   --i; // backtrack one character
   k++; // signal fraction count
  }
  else // assign character by character
   str_f += str.charAt (i);
  j++;
  if (k) // fractional counter increment
   k++;
 }
 return neg + str_f;
}

 function StringBuffer() {
   this.buffer = [];
 }

 StringBuffer.prototype.append = function append(string) {
   this.buffer.push(string);
   return this;
 };

 StringBuffer.prototype.toString = function toString() {
   return this.buffer.join("");
 };



/******************************************************************
*
* setshiptype_checkout(Shipping Type Name, Shipping Type Cost, ID of Selected Row, Total Number of Rows)
*
******************************************************************/
function setshiptype(a,b,c,d) {
    var e;
    var subtotal = 0
    var total = 0;

    if (document.getElementById) {
       for (e = 1; e <= d; e++) {
        document.getElementById("id" + e).bgColor = "white"
       }

        document.getElementById(c).bgColor = "yellow";

        subtotal = document.getElementById('subtotal').value;

        total = Number(subtotal) + Number(b);

        document.getElementById('order_total').value = round(total,2);
        document.getElementById('order_total_disp').innerHTML = "$"+round(total,2);

    } else if (document.layers) {
        alert("to be completed");
    } else if (document.all) {
      for (e = 1; e <= d; e++) {
       document.all("id" + e).bgColor = "white"
      }

      document.all[c].bgColor = "yellow";

      alert("values need to be changed in INPUT types for document.all type");

    }
}

/******************************************************************
*
* select_address(Type Prefix)
*
******************************************************************/
function select_address(sel_item, type) {

       if (type == "bill_") {
	   	  bill_select = sel_item;
       } else if (type == "ship_") {
         	  ship_select = sel_item;
       }

}

/******************************************************************
*
* select_address_auto(Type Prefix)
*
******************************************************************/
function select_address_auto(id, type) {

       if (type == "bill_") {
	   	  bill_select = document.getElementById("bill_use_saved_address_" + id);
       } else if (type == "ship_") {
            ship_select = document.getElementById("ship_use_saved_address_" + id);
       }

}

/******************************************************************
*
* deselect_address(Type Prefix)
*
******************************************************************/
function deselect_address(type) {
       if (type == "bill_") {
	     if (typeof(bill_select) != "undefined") {
		  bill_select.checked = false;
		}
	  } else if (type == "ship_") {
	     if (typeof(ship_select) != "undefined") {
 		  ship_select.checked = false;
		}
	  }

}

/******************************************************************
*
* disable_address(Type Prefix)
*
******************************************************************/
function toggle_ship_address(checkbox) {

    if (document.getElementById) {
    	   if (checkbox.checked == true) {
	       document.getElementById("ship_nickname").style.backgroundColor = "#C0C0C0";
            document.getElementById("ship_company").style.backgroundColor = "#C0C0C0";
            document.getElementById("ship_first_name").style.backgroundColor = "#C0C0C0";
            document.getElementById("ship_last_name").style.backgroundColor = "#C0C0C0";
            document.getElementById("ship_address").style.backgroundColor = "#C0C0C0";
            document.getElementById("ship_address2").style.backgroundColor = "#C0C0C0";
            document.getElementById("ship_city").style.backgroundColor = "#C0C0C0";
            document.getElementById("ship_state").style.backgroundColor = "#C0C0C0";
            document.getElementById("ship_zip").style.backgroundColor = "#C0C0C0";
            document.getElementById("ship_phone").style.backgroundColor = "#C0C0C0";
            document.getElementById("ship_phone2").style.backgroundColor = "#C0C0C0";
            document.getElementById("ship_fax").style.backgroundColor = "#C0C0C0";

            document.getElementById("ship_nickname").disabled = true;
            document.getElementById("ship_company").disabled = true;
            document.getElementById("ship_first_name").disabled = true;
            document.getElementById("ship_last_name").disabled = true;
            document.getElementById("ship_address").disabled = true;
            document.getElementById("ship_address2").disabled = true;
            document.getElementById("ship_city").disabled = true;
            document.getElementById("ship_state").disabled = true;
            document.getElementById("ship_zip").disabled = true;
            document.getElementById("ship_phone").disabled = true;
            document.getElementById("ship_phone2").disabled = true;
            document.getElementById("ship_fax").disabled = true;
	   } else {
	       document.getElementById("ship_nickname").style.backgroundColor = "#FFFFFF";
            document.getElementById("ship_company").style.backgroundColor = "#FFFFFF";
            document.getElementById("ship_first_name").style.backgroundColor = "#FFFFFF";
            document.getElementById("ship_last_name").style.backgroundColor = "#FFFFFF";
            document.getElementById("ship_address").style.backgroundColor = "#FFFFFF";
            document.getElementById("ship_address2").style.backgroundColor = "#FFFFFF";
            document.getElementById("ship_city").style.backgroundColor = "#FFFFFF";
            document.getElementById("ship_state").style.backgroundColor = "#FFFFFF";
            document.getElementById("ship_zip").style.backgroundColor = "#FFFFFF";
            document.getElementById("ship_phone").style.backgroundColor = "#FFFFFF";
            document.getElementById("ship_phone2").style.backgroundColor = "#FFFFFF";
            document.getElementById("ship_fax").style.backgroundColor = "#FFFFFF";

            document.getElementById("ship_nickname").disabled = false;
            document.getElementById("ship_company").disabled = false;
            document.getElementById("ship_first_name").disabled = false;
            document.getElementById("ship_last_name").disabled = false;
            document.getElementById("ship_address").disabled = false;
            document.getElementById("ship_address2").disabled = false;
            document.getElementById("ship_city").disabled = false;
            document.getElementById("ship_state").disabled = false;
            document.getElementById("ship_zip").disabled = false;
            document.getElementById("ship_phone").disabled = false;
            document.getElementById("ship_phone2").disabled = false;
            document.getElementById("ship_fax").disabled = false;
	   }
    } else if (document.layers) {
        alert("to be completed");
    } else if (document.all) {
    	   alert("to be completed");
    }

}


/******************************************************************
*
* CalcReturnQuant(Changed Input Field)
*
******************************************************************/
function CalcReturnQuant () {
	var intReturnQuant = 0;
	var i = 0;
	
	if (document.getElementById) {  // DOM3 = IE5, NS6

	        for (i = 0;document.getElementById("UPC_"+i); ++i) {
	            if (document.getElementById(document.getElementById("UPC_" + i).value + "_quant").value != "") {
		       if (parseInt(document.getElementById(document.getElementById("UPC_" + i).value + "_quant").value)) {
			      if (parseInt(document.getElementById(document.getElementById("UPC_" + i).value + "_quant").value) < 0) {
			            alert("You have entered an invalid return quantity. You cannot return a negative quantity");
                        document.getElementById(document.getElementById("UPC_" + i).value + "_quant").value = 0
			      } else if (parseInt(document.getElementById(document.getElementById("UPC_" + i).value + "_quant").value) <= parseInt(document.getElementById(document.getElementById("UPC_" + i).value + "_ordquant").value)) {
	            	  	    intReturnQuant = intReturnQuant + Number(document.getElementById(document.getElementById("UPC_" + i).value + "_quant").value);
			      } else {
			            alert("You have entered an invalid return quantity. You cannot return more items than you have purchased");
                        document.getElementById(document.getElementById("UPC_" + i).value + "_quant").value = 0
			      }
		       } else if (document.getElementById(document.getElementById("UPC_" + i).value + "_quant").value != '0') {
		          alert("'"+document.getElementById(document.getElementById("UPC_" + i).value + "_quant").value + "' is not a valid quantity");
		       }
             	    }

         	}

        if (intReturnQuant > 1) {
            document.getElementById('total_return_items').innerHTML = intReturnQuant + ") items";
            return intReturnQuant; 
        } else {
            document.getElementById('total_return_items').innerHTML = intReturnQuant + ") item"; 
            return intReturnQuant;
        }    

	} else {
	  alert("TBC:CalcReturnQuant:NotGetElementByID");
	}

}

/**
function CalcBoxRQ () {
    var intReturnQuant = CalcReturnQuant();
    var intBoxes = 1;
    
    if (document.getElementById) {  // DOM3 = IE5, NS6

        if (document.getElementById("numofboxes").value != "") {
            if (parseInt(document.getElementById("numofboxes").value)) {
                if (parseInt(document.getElementById("numofboxes").value) <= 0) {
                    alert("You have entered an invalid return box quantity. Number of boxes must be greater than 0.");
                    document.getElementById("numofboxes").value = 1
                } else if (parseInt(document.getElementById("numofboxes").value) <= intReturnQuant) {
                    intBoxes = document.getElementById("numofboxes").value;
			 } else {
                    alert("You have entered an invalid return box quantity. You cannot have more boxes than items you are returning.");
                    document.getElementById("numofboxes").value = 1
                }
            } else {
                alert("'"+ document.getElementById("numofboxes").value + "' is not a valid quantity");
                document.getElementById("numofboxes").value = 1
            }
        }


        if (intBoxes > 1) {
            document.getElementById('numofboxtext').innerHTML = " boxes";
        } else {
            document.getElementById('numofboxtext').innerHTML = " box";  
        }    

    } else {
      alert("TBC:CalcBoxRQ:NotGetElementByID");
    }

}
 **/  


/**
 *
 * @access public
 * @return void
 **/
function calcShipping() {
 var xmlHttp;
 var strZipcode = document.getElementById('zipcode').value;
 var intPounds = document.getElementById('pounds').value;
 var intOunces = document.getElementById('ounces').value;
 var strShipType = document.getElementById('shiptype').value;
 var decSubtotal = document.getElementById('subtotal').value;
 var strPromoShipType = document.getElementById('promoshiptype').value;
 var intShipId = 0;

 var intMaxDays = document.getElementById('MaxDays').value;
 var blnInStockItems = document.getElementById('InStockItems').value;
 var blnSPORItems = document.getElementById('SPORItems').value;

 if (document.getElementById('calcshipid')) {
    intShipId = document.getElementById('calcshipid').value;
 }
 
  try {
	// Firefox, Opera 8.0+, Safari
	xmlHttp=new XMLHttpRequest();    }
  catch (e){
    // Internet Explorer
	try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      }
    catch (e) {
		try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
      	catch (e) {
		  alert("Your browser does not support AJAX!");
		  return false;
		  		}
	}
  }

    xmlHttp.onreadystatechange=function()
      {
	      if(xmlHttp.readyState==4) {
	        document.getElementById('shipinfo').innerHTML = xmlHttp.responseText;
		document.getElementById('shipinfo').bgColor = "#fefefe";

		var shipping = 0;

		if (document.getElementById('selshipcost')) {
            shipping = document.getElementById('selshipcost').value;
		}

		var total = document.getElementById('order_total').value;
		total = Number(total) + Number(shipping);
        	document.getElementById('order_total').value = round(total,2);
        	document.getElementById('order_total_disp').innerHTML = "$"+round(total,2);
	      } else {
	        document.getElementById('shipinfo').bgColor = "#4c6989";
	        document.getElementById('shipinfo').innerHTML = "<img src=\"/img/loadanim.gif\"><BR>Calculating Shipping Costs and Times...";
	      }
      }

    xmlHttp.open("GET","/calc_shipping.html?zipcode="+strZipcode+"&pounds="+intPounds+"&ounces="+intOunces+"&shiptype="+strShipType+"&blnReadOnly=FALSE&shipid="+intShipId+"&Subtotal="+decSubtotal+"&MaxDays="+intMaxDays+"&InStockItems="+blnInStockItems+"&SPORItems="+blnSPORItems+"&PromoShipType="+strPromoShipType+"&A=0",true);
    xmlHttp.send(null);

    return true;
}


/**
 *
 * @access public
 * @return void
 **/
function changecolor (strColor) {
	var i = 0;
	var strSizes = new StringBuffer();
	var strMfrnum = strColor.value;
	var strCurColor = strColor.options[strColor.selectedIndex].text;

	strSizes.append();

	if (typeof(sizes[strMfrnum+"_size_0"]) != "undefined" ) {
       strSizes.append("<SELECT name=\"upc\"  CLASS=\"prodinput\"  onchange=\"changeavail(this);\">");

	    for (i = 0; sizes[strMfrnum+"_size_" + i]; ++i) {
		    strSizes.append("<OPTION value=");
		    strSizes.append(sizes[strMfrnum+"_upc_" + i]);
		    strSizes.append(">");
		    strSizes.append(sizes[strMfrnum+"_size_" + i]);
		    strSizes.append("</OPTION>\n");
	    }

	  strSizes.append("</SELECT>");
	} else {
	  strSizes = '<P ALIGN=\"CENTER\">Currently out of stock</P>';
	}

	if (document.getElementById) {  // DOM3 = IE5, NS6
		document.getElementById('sizeandwidth').innerHTML = strSizes.toString();
  		document.getElementById('mfrnum').innerHTML = "(#"+strMfrnum+")";
		document.getElementById('current_color').innerHTML = strCurColor;
			}

	switch_image(strMfrnum);
	
	return true;
}

/**
 *
 * @access public
 * @return void
 **/
function changeavail (cboUPC) {
	var strAvailability = new StringBuffer();
	var strMfrnum;
	var strCurUPC = cboUPC.value;

	if (document.getElementById) {  // DOM3 = IE5, NS6
		strMfrnum = document.getElementById('cboMfrnum').value
	}

	strAvailability.append();

	if (typeof(sizes[strMfrnum+"_"+strCurUPC+"_av"]) != "undefined" ) {
	  strAvailability.append(sizes[strMfrnum+"_"+strCurUPC+"_av"]);
	} else {
	  strAvailability.append('Availability: N/A');
	}

	if (document.getElementById) {  // DOM3 = IE5, NS6
		document.getElementById('availability_info').innerHTML = strAvailability.toString();
	}

	return true;
}

/**
 *
 * @access public
 * @return void
 **/
function switch_image(strMfrnum) {
 var xmlHttp;

  try {
	// Firefox, Opera 8.0+, Safari
	xmlHttp=new XMLHttpRequest();    }
  catch (e){
    // Internet Explorer
	try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      }
    catch (e) {
		try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
      	catch (e) {
		  alert("Your browser does not support AJAX!");
		  return false;
		  		}
	}
  }

    xmlHttp.onreadystatechange=function()
      {
	      if(xmlHttp.readyState==4) {
	        document.getElementById('prod_images').innerHTML = xmlHttp.responseText;
	      } else {
	        document.getElementById('prod_images').innerHTML = "Loading Image...";
	      }
      }

    xmlHttp.open("GET","/image_switch.html?mfrnum="+strMfrnum+"&A=0",true);
    xmlHttp.send(null);
    
    return true;
}

function changesizes(strGender, intCatID, intSubcatID){
 var xmlHttp;

  try {
	// Firefox, Opera 8.0+, Safari
	xmlHttp=new XMLHttpRequest();    }
  catch (e){
    // Internet Explorer
	try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      }
    catch (e) {
		try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
      	catch (e) {
		  alert("Your browser does not support AJAX!");
		  return false;
		  		}
	}
  }

    xmlHttp.onreadystatechange=function()
      {
	      if(xmlHttp.readyState==4) {
	        document.getElementById('sizecombo').innerHTML = xmlHttp.responseText;
	      } else {
	        document.getElementById('sizecombo').innerHTML = "Loading Sizes...";
	      }
      }

    xmlHttp.open("GET","/size_switch.html?gender="+strGender+"&catid="+intCatID+"&subcatid="+intSubcatID+"&A=0",true);
    xmlHttp.send(null);
    
    return true;
}

/**
 *
 * @access public
 * @return boolean
 **/
function validateQuantities() {
	var strUPC = document.addtocart.upc[document.addtocart.upc.selectedIndex].value;
	var elementupc = document.getElementById(strUPC);
	var spor_elementupc = document.getElementById(strUPC+"_sporquant");
     var message;

     if (document.addtocart.quantity.value == "") {
       message = "Please enter a valid quantity before attempting to add this item to your cart"

	  alert(message);
	  return false;
	} else if (IsNumeric(document.addtocart.quantity.value) == false) {
       message = "'" + document.addtocart.quantity.value + "' is not a valid number of items to add to your cart"

	  alert(message);
	  return false;
	} else if (Number(document.addtocart.quantity.value) <= 0) {
       message = "'" + document.addtocart.quantity.value + "' is not a valid number of items to add to your cart"

	  alert(message);
	  return false;
	}

	if (Number(document.addtocart.quantity.value) > (Number(elementupc.value) + Number(spor_elementupc.value))) {
		message = "Cannot add quantity of "+document.addtocart.quantity.value+" to cart, there is currently only "+elementupc.value+" in stock ";

		if (spor_elementupc.value > 0) {
            message += "and " + spor_elementupc.value + " available to special order";
		}

		alert(message);
		return false;
	} else {
		return true;
	}

}

/**
 *
 * @access public
 * @return boolean
 **/
function validateAllQuantities() {
	for ($i=0;document.cart.upc[$i]; $i++) {
	 alert(document.cart.upc[$i].value)
	}

	return true;
}


/**
 *
 * @access public
 * @return boolean
 **/
function toggleTabs(whichLayer) {
 var elem, vis;
 var i, name;

 if(document.getElementById) {
   elem = document.getElementById(whichLayer);

     for (i=1; i < 4; i++) {
	document.getElementById("tab"+String(i)).style.display = "none";
     }

   elem.style.display = "block";

 } else if(document.all) { // this is the way old msie versions work
   elem = document.all[whichLayer];
 } else if(document.layers) { // this is the way nn4 works
   elem = document.layers[whichLayer];
   vis = elem.style;  // if the style.display value is blank we try to figure it out here
 }


 
}

/**
 *
 * @access public
 * @return boolean
 **/
function ShowProdInfo(a) {

  if (a == "1") {
    changeimg("overview_tab","/img/tabs/overview.gif");
    changeimg("reviews_tab","/img/tabs/reviews_off.gif");
    changeimg("features_tab","/img/tabs/features_off.gif");
    toggleTabs("tab1");
  } else if (a == "2") {
    changeimg("overview_tab","/img/tabs/overview_off.gif");
    changeimg("reviews_tab","/img/tabs/reviews.gif");
    changeimg("features_tab","/img/tabs/features_off.gif");
    toggleTabs("tab2");
  } else if (a == "3") {
    changeimg("overview_tab","/img/tabs/overview_off.gif");
    changeimg("reviews_tab","/img/tabs/reviews_off.gif");
    changeimg("features_tab","/img/tabs/features.gif");
    toggleTabs("tab3");
  }
  
  return true;
}

function changeimg(img_name,img_src) {
  document[img_name].src=img_src;
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;


   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;

   }

// ******************************************* //
function confirmEmptyCart() {
 var answer = confirm("Are you sure you want to empty your shopping cart?");

 return answer;
}


 // ******************************************* //
function confirmDeleteAddress() {
 var answer = confirm("Are you sure you want to delete this address from your address book?");

 return answer;
}


// ******************************************* //
function CheckTab(e, input) {


if (e) {
  if (typeof event != 'undefined') { e = event }
}

if (!e) {
  return false;
}

if (e.keyCode == 9) {
     if (input.value.length != input.maxLength) {
        return false;
     }
}

}


// ******************************************//
function setCursorAtEnd(textbox) {

 if (textbox.createTextRange) {
   var r = textbox.createTextRange();
   r.moveStart('character', (textbox.value.length));
   r.collapse();
   r.select();
 }

}


// ******************************************* //
function CCMoveFocus(e, current, number) {

if (e) {
  if (typeof event != 'undefined') { e = event }
}

if (!e) {
  return false;
}

if (e.keyCode == 8) {
    // TAKE BACK TO THE PREVIOUS INPUT FIELD UNLESS IT IS THE FIRST /////
    if (number != 1) {
      if (current.value == "") {
          if (document.getElementById) {
            document.getElementById("x_cc_number"+(number - 1)).focus();
	     } else {
            alert("To be completed 20");
	     }
      }
    }
} else {
  //// AUTOMATICALLY MOVE FORWARD TO THE NEXT FIELD IF REACHED MAX LENGTH OF INPUT ////
  if (number != 4) {
      if (current.value.length == current.maxLength) {
              if (document.getElementById) {
		    	 if ((number + 1) == 4) {
		          if (document.getElementById("x_cc_number4_all").style.visibility != "hidden") {
                   	    document.getElementById("x_cc_number4").focus();
			     }
			 } else {
                   document.getElementById("x_cc_number"+(number + 1)).focus();
			 }

	         } else {
                alert("To be completed 21");
	         }
      } else if (current.value.length > current.maxLength) {
        alert("Fix");
      }
  }
}

}

// ******************************************* //
function UpdateCardFields(type) {
 var fullcc = "";
 var cctype = "";

 if (type.value) {
  cctype = type.value;
 } else if (type.options[type.selectedIndex]) {
  cctype = type.options[type.selectedIndex].text;
 }

     if(document.getElementById) {
	  fullcc = document.getElementById("x_cc_number1").value + document.getElementById("x_cc_number2").value + document.getElementById("x_cc_number3").value + document.getElementById("x_cc_number4").value

       if (cctype == "American Express") {
        	 document.getElementById("x_cc_number2").size = 6;
        	 document.getElementById("x_cc_number2").maxLength = 6;
        	 document.getElementById("x_cc_number3").size = 5;
        	 document.getElementById("x_cc_number3").maxLength = 5;
        	 document.getElementById("x_cc_number_first").style.width = '190px';
        	 document.getElementById("x_cc_number4_all").style.visibility = 'hidden';

           if (fullcc.length >= 10) {
		    document.getElementById("x_cc_number1").value = fullcc.substr(0, 4);
        	    document.getElementById("x_cc_number2").value = fullcc.substr(4, 6);
        	    document.getElementById("x_cc_number3").value = fullcc.substr(10);
		 }

        	 document.getElementById("x_cc_number4").value = "";

        	 document.getElementById("cc_cv2").size = "4";
        	 document.getElementById("cc_cv2").maxLength = "4";
	  } else {
           if (fullcc.length > 12) {
		    document.getElementById("x_cc_number1").value = fullcc.substr(0, 4);
        	    document.getElementById("x_cc_number2").value = fullcc.substr(4, 4);
        	    document.getElementById("x_cc_number3").value = fullcc.substr(8, 4);
        	    document.getElementById("x_cc_number4").value = fullcc.substr(12);
		 }

        	 document.getElementById("x_cc_number2").size = 4;
        	 document.getElementById("x_cc_number2").maxLength = 4;
        	 document.getElementById("x_cc_number3").size = 4;
        	 document.getElementById("x_cc_number3").maxLength = 4;
        	 document.getElementById("x_cc_number_first").style.width = '175px';
        	 document.getElementById("x_cc_number4_all").style.visibility = 'visible';

        	 document.getElementById("cc_cv2").size = "3";
        	 document.getElementById("cc_cv2").maxLength = "3";
		 document.getElementById("cc_cv2").value = document.getElementById("cc_cv2").value.substr(0, 3);
	  }

     } else if(document.all) { // this is the way old msie versions work
        alert("To be completed");
	   // elem = document.all[whichLayer];
     } else if(document.layers) { // this is the way nn4 works
        alert("To be completed");
       //elem = document.layers[whichLayer];
       //vis = elem.style;  // if the style.display value is blank we try to figure it out here
     }

}