﻿// JavaScript Document
// Author: Paul Roddick
// System: Tokyoflash Japan
// Date: March 2007


function txtboxfocus(mtxt,fld){
	if (document.getElementById(fld).value == mtxt) { document.getElementById(fld).value=''; }
}
function txtboxblur(mtxt,fld){
	if (document.getElementById(fld).value == '') { document.getElementById(fld).value=mtxt; }
}

function clear_txt(id){
	document.getElementById(id).value='';
}

function switchid(id){	
	hideallids();
	showdiv(id);
}

function switchvis(box,div){
	if(document.getElementById(box).checked==true) { 
		showdiv(div); 
	}else{ 
		hidediv(div); 
	} 
}
function switchvis2(div){
	if(document.getElementById(div).style.display=='block') { 
		hidediv(div); 
	}else{ 
		showdiv(div); 
	} 
}

function hideallids(){
	//loop through the array and hide each element by id
	for (var i=0;i<ids.length;i++){
		hidediv(ids[i]);
	}		  
}

function hidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function showdiv(id) {
	//safe function to show an element with a specified id
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}

function changetext(id,oldc,newc){
	
	var nowc;
	nowc=document.getElementById(id).innerHTML;
	if(nowc==oldc){
		document.getElementById(id).innerHTML = newc;
	}else{
		document.getElementById(id).innerHTML = oldc;
	}	
	
}

function chktotal(usdtotal,ctotal,cdisc,pts,curr){
	
	var newyen;
	var newcurr;
	
	discusd=((usdtotal*100)-pts)/100;
	disccurr=ctotal-cdisc;
	
	if(document.getElementById('usepoints').checked==true) {
		document.getElementById('dtotal').innerHTML = '$' + addCommas(format_number(discusd,2));
		
		if(discusd==0){
			document.getElementById('priceind').innerHTML = 'Because your points cover the value of your order, you will not be charged for this order.';
		}
		
	}else{
		document.getElementById('dtotal').innerHTML = '$' + addCommas(format_number(usdtotal,2));
	}
}


function do_shipping(goods,ems,baseship){

	var shipopt;
	var newtotal;
	var newShip;
	shipopt=document.getElementById('ShipCo').value;
	if(shipopt=='EMS'){
		newusdTotal=goods+baseship+ems;
		newShip=baseship+ems;
	}else{
		newusdTotal=goods+baseship;
		newShip=baseship;
	}
	document.getElementById('dtotal').innerHTML = '$' + addCommas(format_number(newusdTotal,2));
	document.getElementById('shipping').innerHTML = '$' + addCommas(format_number(newShip,2));
	
}



function addCommas(nStr)
{
  nStr += '';
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
  }
  return x1 + x2;
}

function format_number(pnumber,decimals){
    if (isNaN(pnumber)) { return 0};
    if (pnumber=='') { return 0};
    var snum = new String(pnumber);
    var sec = snum.split('.');
    var whole = parseFloat(sec[0]);
    var result = '';
    if(sec.length > 1){
        var dec = new String(sec[1]);
        dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals)));
        dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals));
        var dot = dec.indexOf('.');
        if(dot == -1){
            dec += '.';
            dot = dec.indexOf('.');
        }
        while(dec.length <= dot + decimals) { dec += '0'; }
        result = dec;
    } else{
        var dot;
        var dec = new String(whole);
        dec += '.';
        dot = dec.indexOf('.');       
        while(dec.length <= dot + decimals) { dec += '0'; }
        result = dec;
    }   
    return result;
}

function usecoup(){
	if(document.getElementById('ccode').checked==true) { 
		showdiv('t_coup'); 
		showdiv('b_coup'); 
		hidediv('e_points');
		hidediv('t_points');
		hidediv('b_points');
	}else{ 
		hidediv('t_coup'); 
		hidediv('b_coup'); 
		showdiv('e_points');
		showdiv('t_points');
		showdiv('b_points');
	}
}

function strpos (haystack, needle, offset) {
    // Finds position of first occurrence of a string within another  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/strpos
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman    
    // +   bugfixed by: Daniel Esteban
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
    // *     returns 1: 14
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}

