function padLeft(_Str,thePad,howLong)
{
    var _temp = ''; thePad=thePad.charAt(0);
    for(var i=0; i< howLong-_Str.length; i++)
	_temp = thePad + _temp;
	return _temp + _Str;
}// example    y =padLeft(x,'|',7) = "||||abc" 
 
function padRight(_Str,thePad,howLong)
{
    var _temp = '';thePad=thePad.charAt(0);
    for(var i=0; i< howLong-_Str.length; i++)
	_temp = thePad + _temp;	
        return _Str + _temp;
}// example    y = padRight(x,'|',5) = "abc||" 
 
function padBoth(_Str,thePad,howLong)
{
    var _temp = '';thePad=thePad.charAt(0);
    for(var i=0; i< howLong-_Str.length; i++)
	_temp = thePad + _temp;	
        var _Str1 = _temp.substring(0,_temp.length/2);
	var _Str2 = _temp.substring(_Str1.length);
	return _Str2 + _Str + _Str1;
} // example   y =padBoth(x,"|",8) = "|||abc||" 


function isNumeric(x) {
var RegExp = /^(-)?(\d*)(\.?)(\d*)$/; // allow a decimal & negative 
var result = x.match(RegExp); //1 if number
return result;
}

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 isNumericLimit(x,y) {
// this function test for a limit as well
var RegExp = /^(-)?(\d*)(\.?)(\d*)$/; // allow a decimal & negative 
var result = x.match(RegExp); //1 if number

if (result != 0){
 if (x > y){ return 0};
}
return result;

}


function cleanText(input) {

var output = "";

for (var i = 0; i < input.length; i++) {

   if ((input.charCodeAt(i) > 31) && (input.charCodeAt(i) < 127)) {  
      output += input.charAt(i);
   } else  {
	  i++;
      output += " "; 
   }

}

return output;

}

function nothing() {
  var one=1;  
}

function trim(str) {
  if (str != undefined){
     return str.replace(/^\s*|\s*$/g,"");
  }
}

//next two are for weeknumber
function getWeekNr()
{
	var today = new Date();
	Year = takeYear(today);
	Month = today.getMonth();
	Day = today.getDate();
	now = Date.UTC(Year,Month,Day+1,0,0,0);
	var Firstday = new Date();
	Firstday.setYear(Year);
	Firstday.setMonth(0);
	Firstday.setDate(1);
	then = Date.UTC(Year,0,1,0,0,0);
	var Compensation = Firstday.getDay();
	if (Compensation > 3) Compensation -= 4;
	else Compensation += 3;
	NumberOfWeek =  Math.round((((now-then)/86400000)+Compensation)/7);
	//return NumberOfWeek;  
	//changed this to match the foxweek not iso week
	//originally used this function and then swtitched to php function and then the below
	return document.getElementById('foxweek').value;
	
}


function takeYear(theDate)
{
	x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
}


function printPage() {

  if (window.print) {
    window.print();
  } else {
      document.body.style.cursor='auto';
      document.getElementById('confirmtext').innerHTML="Sorry, your browser doesn't support this feature."; //Error printing      
      showconfirm();
  } 

}


function printlayer(layer)

{

  var generator=window.open('','name','channelmode = no, directories = no,fullscreen = no ,height = 500,left = 100,location = no,menubar = no,resizable = yes ,scrollbars = yes,status = no,titlebar = yes,toolbar = no,top = 100,width = 500');
  var layertext = document.getElementById(layer);
  generator.document.write(layertext.innerHTML.replace("col1","col5"));
  generator.document.close();
  generator.print();
  generator.close();

}
  

function detectKey() {
    if (event.keyCode==13) { alert("Please click the appropriate button or use the tab key.") }
}


function startclock()
{
var thetime=new Date();

var nhours=thetime.getHours();
var nmins=thetime.getMinutes();
var nsecn=thetime.getSeconds();
var nday=thetime.getDay();
var nmonth=thetime.getMonth();
var ntoday=thetime.getDate();
var nyear=thetime.getYear();
var AorP=" ";

if (nhours>=12)
    AorP="P.M.";
else
    AorP="A.M.";

if (nhours>=13)
    nhours-=12;

if (nhours==0)
   nhours=12;

if (nsecn<10)
 nsecn="0"+nsecn;

if (nmins<10)
 nmins="0"+nmins;

if (nday==0)
  nday="Sunday";
if (nday==1)
  nday="Monday";
if (nday==2)
  nday="Tuesday";
if (nday==3)
  nday="Wednesday";
if (nday==4)
  nday="Thursday";
if (nday==5)
  nday="Friday";
if (nday==6)
  nday="Saturday";

nmonth+=1;

if (nyear<=99)
  nyear= "19"+nyear;

if ((nyear>99) && (nyear<2000))
 nyear+=1900;

//document.getElementById('clockspot').innerHTML=nhours+": "+nmins+" "+AorP;

// this currently runs every 60 seconds, be careful about running more often
// may cause slow down...

//upgetBCM();


setTimeout('startclock()',60000);

} 

function checkemail(midfld){
  
  var emailchk=document.getElementById(midfld).value;
  emailchk=trim(emailchk);
  if (emailchk.length > 0){

    if ((emailchk.indexOf('@') > -1) && (emailchk.indexOf('.') > -1)){ 
      document.getElementById(midfld).style.color='black';
      merr="Correct";
    } else { 
      document.getElementById(midfld).style.color='red';
      merr="The email format is missing @ and/or '.' , Please use 'EMAILNAME@ISP.COM'.<br>\r\n";
    }

  } else {
     document.getElementById(midfld).style.color='black';
     merr="Correct";
  } 

return merr;

}


function pause(numberMillis) { 
	var now = new Date(); 
	var exitTime = now.getTime() + numberMillis;  
	while (true) {   
  	  now = new Date();   
	  if (now.getTime() > exitTime){ 
	    return;   
	  }
    }    
}


function toggleAcctInfo(){

	if(document.getElementById('cc_use_acct').checked == true ){

		$('#cc_address_input').val(document.getElementById('acct_street').value);
	    $('#cc_city_input').val(document.getElementById('acct_city').value);   
		$('#cc_state_input').val(document.getElementById('acct_state').value);      
		$('#cc_zip_input').val(document.getElementById('acct_zip').value);    
	    //cc_email_input
	} else {
		$('#cc_address_input').val(" ");
	    $('#cc_city_input').val(" ");   
		$('#cc_state_input').val(" ");      
		$('#cc_zip_input').val(" "); 
		
    }		
  
}	

//promo code function --- need to work with randy on how to set this up
    function applyPromo(){
	   alert('Need to add promo code discount apply after working out details with randy.');
    }



//toggle credit card layer
function toggleCC() {

//$('#isCOD').val();

  if(document.getElementById('payment_type_input_3').checked == true ){
	jQuery("#order_invoice_dialog").dialog('option', 'position', [this.x,2]);  
	jQuery("#order_invoice_dialog").dialog('option', 'height', 780);
	
	document.getElementById('order_invoice_dialog').style.height='660px';
	//document.getElementById('order_invoice_dialog').style.border='1px solid black';
	
    document.getElementById('invoice_cc_div').style.display = 'block'; 
  } else {
    document.getElementById('invoice_cc_div').style.display = 'none';
  }		

}


//toggle label on order submit
function toggleOrderradio(){

//most commom	
if(document.getElementById('order_output_type_2').checked == true ){
  document.getElementById('order_shiptype_3').checked=true;
}		

if(document.getElementById('order_output_type_3').checked == true ){
  if(document.getElementById('order_shiptype_2').checked == false ){
	document.getElementById('order_shiptype_1').checked=true;
  }
  document.getElementById('data_div').style.display = 'none'; 
  document.getElementById('order_label_type_1').checked = true;
  document.getElementById('labeltype_div').style.display = 'block';

} else {
  document.getElementById('labeltype_div').style.display = 'none';
  document.getElementById('order_label_type_1').checked = false;
  document.getElementById('order_label_type_2').checked = false;
  document.getElementById('order_label_type_3').checked = false;
  document.getElementById('data_div').style.display = 'block'; 	
}		
		
 if(document.getElementById('order_output_type_1').checked == true ) {
	if(document.getElementById('order_shiptype_2').checked == false ) {      	 	 	  
       document.getElementById('order_shiptype_1').checked=true;
       document.getElementById('order_shiptype_3').checked=false;
    }
 }
 	
 if(document.getElementById('order_shiptype_3').checked == true) {	 	  
   document.getElementById('order_output_type_1').checked=false;
   document.getElementById('order_output_type_2').checked=true;
   document.getElementById('order_output_type_3').checked=false;
 }
 

 if(document.getElementById('order_shiptype_1').checked == true) {
   document.getElementById('shippingaddrinfo').style.display = 'block';
 } else {
   document.getElementById('shippingaddrinfo').style.display = 'none'; 	 
 }
 
 
 
		
}	

//the following are for non jquery http object calls
//javascript:getsic('car','YPH');
function getsic(msearch,mtable) {

  var url = "include/php/getsic.php";  
   
  http.open('POST', url , false);
  http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  http.onreadystatechange = getsicResponse;
  http.send("thesearch=" +msearch+ "&thetable=" + mtable);  
  
}

function getsicResponse() {
  //grayOut(false);
  
  if (http.readyState == 4) {

    mess = http.responseText;
  
    alert(mess)   
    
  } //end of ready state

}


 function selectallsic(mval) {
    //alert('line 359');

    grayOut2(true);
    document.getElementById('plswait').style.display = 'block';    
	    
    //toggle to uncheck all if -- this will check if checked and uncheck if not 
   	    
	if (mval=='E'){
		
		var exactchk=false;    
    	if (document.getElementById('selectallexact').checked==false){
	    	exactchk=true;
		} 
	
		var x=0;
		var mexact=document.getElementById('howmanyexact').value;
	
	    while (x < mexact){
		    
		   var mnum=x+"";
		   
		   //var z=document.getElementsByName('sic_exact'+mnum)[0].checked;
           //alert(z);


		   
		   if (document.getElementsByName('sic_exact'+mnum)[0].checked==exactchk){
		  
			  var v=document.getElementsByName('sic_exact'+mnum)[0].value;
		      if (exactchk==false) {
			      document.getElementsByName('sic_exact'+mnum)[0].checked=true;
				  CV['sic'].push(v);		
			  } else {
				  document.getElementsByName('sic_exact'+mnum)[0].checked=false;
				  CV['sic'] = _(CV['sic']).without(v);
				  
			  }
		      
	       }
		   
	       x=(x+1);
        } 					
	
    } else if (mval=='R') {
    
	    var relatedchk=false;    
    	if (document.getElementById('selectallrelated').checked==false){
		    relatedchk=true;
		} 
 
    	var x2=0;
    	var mrelated=document.getElementById('howmanyrelated').value;
	
	    while (x2 < mrelated){
		   var mnum=x2+"";
		    
		   if (document.getElementsByName('sic_related'+mnum)[0].checked==relatedchk){
		    
		      var v2=document.getElementsByName('sic_related'+mnum)[0].value;
		      if (relatedchk==false) {
			      document.getElementsByName('sic_related'+mnum)[0].checked=true;
				  CV['sic'].push(v2);		
			  } else {
				  document.getElementsByName('sic_related'+mnum)[0].checked=false;
				  CV['sic'] = _(CV['sic']).without(v2);
				  
			  }
		      
	       }
		  
		   
		   x2=(x2+1);
        } 	
	    
	    
    }
             		
	grayOut2(false);
    document.getElementById('plswait').style.display = 'none';						
       
 }


//tree functions

 
function exp_coll(ind,mlevel){
	 
//alert('in function');	 
 s = document.getElementById("sp_" + ind);
 i = document.getElementById("im_" + ind);
 
 if (s.style.display == 'none')
 { 
   s.style.display = 'block';
   i.src = "./img/minus_box.gif";
   
   
   //if its a 2 digit
   if (mlevel=='A'){
   
   	if (ind==0){
       getsicinfo('0','09',2);
   	} else if(ind==10){
   	   getsicinfo('10','14',2);   
   	} else if (ind==15){
	   getsicinfo('15','17',2);   
   	} else if (ind==20){
	   getsicinfo('20','39',2);    
   	} else if (ind==40){
	   getsicinfo('40','49',2);     
   	} else if (ind==50){
   	   getsicinfo('50','51',2);    	   	   
   	} else if (ind==52){
   	   getsicinfo('52','59',2);    	   	   
   	} else if (ind==60){
   	   getsicinfo('60','67',2);    	   	   
   	} else if (ind==70){
   	   getsicinfo('70','89',2);    	   	   
   	} else if (ind==91){
   	   getsicinfo('91','97',2);    	   	   
   	} else if (ind==99){
   	   getsicinfo('99','99',2);    	   	   
   	}

  }	else if (mlevel=='B'){
	 var theind=ind+'';
	 var tmp='99'+'';
	 var theend=theind.substring(0,2)+tmp.substring(0,2);
	 //alert(theind+" : "+theend);
	 getsic4info(theind,theend,4);
	  
  }  
	  	  
   	
   	
   	   
 }
 else if (s.style.display == 'block')
 {
   s.style.display = 'none';
   i.src = "./img/plus_box.gif";
 }
}


function sictoggle(ind){
	 
//alert('in function'+"sp_" + ind);	 
 s = document.getElementById("sp_" + ind);
 
 
 if (s.style.display == 'none')
 { 
   s.style.display = 'block';
 }
 else if (s.style.display == 'block')
 {
   s.style.display = 'none';
 }
}

 

function exp(ind)
{
 s = document.getElementById("sp_" + ind);
 i = document.getElementById("im_" + ind);
 
 if (!(s && i)) return false;
 s.style.display = 'block';
 i.src = "./img/minus_box.gif";
 
 
}

 
function coll(ind)
{
 s = document.getElementById("sp_" + ind);
 i = document.getElementById("im_" + ind);
 if (!(s && i)) return false;
 s.style.display = 'none';
 i.src = "./img/plus_box.gif";
}
 

function coll_all()
{
 var cnt=0;
 
 while (cnt < 100){
   coll(cnt);
  cnt=(cnt+1);
 }  
    
}
 
function exp_all()
{
	
 var cnt=0;
 
 while (cnt < 100){
   exp(cnt);
  cnt=(cnt+1);
 }  	
	
 
}


// the next two functions retrieve the dun information
function getsicinfo(low,high,FourOrSix) {

  //alert('in this function');
		
  var url = "include/php/getsicTreeAll_process.php?mfilter="; // The server-side script
  //alert("in this function");
  s = new Array();
  s[0] = low;
  s[1] = high;
  s[2] = 2;
  
  //document.body.style.cursor = "wait";
  //showwait();
  grayOut2(true);
  document.getElementById('plswait').style.display = 'block';

  http.open("GET", url + escape(s), true);
  http.onreadystatechange = getsicResponse;
  http.send(null);

 
}

function getsicResponse() {

  if (http.readyState == 4) {
	        
    results = http.responseText.split("^");
    var thesic="";
    //alert(http.responseText);
    //results=tresults.sort();
    grayOut2(false);
    document.getElementById('plswait').style.display = 'none';
    r1= new Array();

    reportArray = new Array();
    reportArray[0] = "";  
    
      
    
    for (x in results)
    {
     
     r1 = results[x].split("|");
    
         if (r1[1] != undefined){
	       thesic=r1[0]; 
	       //r1[1]=r1[1].replace(",",";");
	       r1[1]=r1[1].replace(/,/g,";")
	       reportArray[0] = reportArray[0]+"<div style=\"position: relative; margin-bottom: 3px;\"><table>";  
	       reportArray[0] = reportArray[0]+"<tr width='590px' style='zIndex:13;'>";
	       reportArray[0] = reportArray[0]+"<td width='24px'></td><td><li><a href='javascript:sictoggle(\""+r1[0]+"00\");'><img src='./img/plus_box.gif' width='11' height='11' alt='toggle' border='0' id='im_"+r1[0]+"00' /></a>&nbsp;";
           reportArray[0] = reportArray[0]+"<input type=\"checkbox\" value=\""+r1[1]+"\" id=\"sic_tree"+r1[1]+"_input\" name=\"sic_tree"+r1[1]+"\" /> <label>&nbsp;"+r1[1]+"</label></td>";
	       reportArray[0] = reportArray[0]+"</tr></table><table>";
	   
	       var tmpdiv="thediv_"+r1[0];
	       reportArray[0] = reportArray[0]+"<ul class='sicul' id='sp_"+r1[0]+"00' style='display:none;'><div style=\"position: relative;left:48px;height:auto;width:350px; margin-bottom: 16px;margin-top: 5px;\"><p id='"+trim(tmpdiv)+"' >";
            //alert("sp_"+r1[0]+"00");
             	       
	         thesic4=r1[2].split("~");
	         //alert(r1[2]);
	         for (x in thesic4){
		       if (x ==0){
			       //alert(thesic4[x]);
		       }      
	           sr1 = thesic4[x].split("{");
	           if (sr1[1] != undefined){
	             thesubsic=sr1[0]; 
	             sr1[1]=sr1[1].replace(/,/g,";")
	             
	             
	             reportArray[0] = reportArray[0]+"<div style=\"height:auto;margin-top: -12px;margin-bottom:-12px;\"><br><a href='javascript:sictoggle(\""+sr1[0]+"00\");'><img src='./img/plus_box.gif' width='11' height='11' alt='toggle' border='0' id='im_"+sr1[0]+"00' /></a>&nbsp;<input type=\"checkbox\" value=\""+sr1[1]+"\" id=\"sic_4tree"+sr1[1]+"_input\" name=\"sic_4tree"+sr1[1]+"\" /> <label>&nbsp;"+sr1[1]+"</label>";
                 var tmpdiv="thediv_"+sr1[0];
	             //if (x !=0){  
   	                reportArray[0] = reportArray[0]+"<ul class='sicul' id='sp_"+sr1[0]+"00' style='display:none;'><div style=\"position: relative; left:48px;height:auto;width:350px; margin-bottom: 5px;margin-top: -5px;\"><p id='"+trim(tmpdiv)+"' >";
                 //}
   	             thesictmp=sr1[2].split("@");
                 thesic6=thesictmp[1].split("[");
                 for (x in thesic6){
	                
	                 zr1 = thesic6[x].split("}");
	                 if (zr1[1] != undefined){
		                zr1[1]=zr1[1].replace(/,/g,";");
		                 
   	                    reportArray[0] = reportArray[0]+"<br><input type=\"checkbox\" value=\""+zr1[1]+"\" id=\"sic_6tree"+zr1[1]+"_input\" name=\"sic_6tree"+zr1[1]+"\" /> <label>&nbsp;"+zr1[1]+"</label>";
	                 }
                    
	             }
	             
   	             reportArray[0] = reportArray[0]+"</p></div></ul></div>";
	             
   	             
	                	             
   	               
	           }// end of defined condition
             
             } //end of internal loop        
	            
           //this is for another one
           reportArray[0] = reportArray[0]+"</p></div></ul></li></tr>";
	       reportArray[0] = reportArray[0]+"</table></div>"; 
	                
	     }// end of defined condition
    } // end of loop
  
  	
  	thediv=thesic+"";
  	thesic=parseInt(thesic);
  
  	if (thesic <=9){ 
	    document.getElementById('agrlist').innerHTML= reportArray[0];
  	} else if (thesic > 9 && thesic <= 14){  
	 	document.getElementById('minelist').innerHTML= reportArray[0]; 
  	} else if (thesic >= 15 && thesic <= 17){
	 	document.getElementById('constlist').innerHTML= reportArray[0];  
  	} else if (thesic >= 20 && thesic <=39){ 
	 	document.getElementById('manulist').innerHTML= reportArray[0];    
  	} else if (thesic >= 40 && thesic <=49){
	 	document.getElementById('translist').innerHTML= reportArray[0]; 
  	} else if (thesic >= 50 && thesic <=51){
	 	document.getElementById('wholelist').innerHTML= reportArray[0];  
  	} else if (thesic >= 52 && thesic <=59){
	 	document.getElementById('retaillist').innerHTML= reportArray[0];  
  	} else if (thesic >= 60 && thesic <=67){
	 	document.getElementById('finlist').innerHTML= reportArray[0];  
  	} else if (thesic >= 70 && thesic <=89){
	 	document.getElementById('servlist').innerHTML= reportArray[0];  
	} else if (thesic >= 91 && thesic <=97){
	 	document.getElementById('publiclist').innerHTML= reportArray[0];  
  	} else if (thesic >= 99 && thesic <=99){
	 	document.getElementById('misclist').innerHTML= reportArray[0];  
  	} 
  	
    //end of test for 2 digit
  
  //hidewait();
  //document.body.style.cursor='auto';
   	   
  } //end of ready state

}



function grayOut2(vis, options) {  
		
		// Stephen pulled this section from the net in tutorial- no credits
		// Pass true to gray out screen, false to ungray  
		// options are optional.  This is a JSON object with the following (optional) properties  
		// opacity:0-100         
		// Lower number = less grayout higher = more of a blackout   
		// zindex: #             
		// HTML elements with a higher zindex appear on top of the gray out  
		// bgcolor: (#xxxxxx)    
		// Standard RGB Hex color code  
		// grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});  
		// Because options is JSON opacity/zindex/bgcolor are all optional and can appear  
		// in any order.  Pass only the properties you need to set.  
		
		var options = options || {};   
		var zindex = options.zindex || 50;  
		var opacity = options.opacity || 10; 
		var opaque = (opacity / 100);  
		var bgcolor = options.bgcolor || '#000000';  
		var dark=document.getElementById('darkenScreenObject');  
		
		if (!dark) {    
			
			// The dark layer doesn't exist, it's never been created.  So we'll    
		    // create it here and apply some basic styles.    
		    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917    
		    
		    var tbody = document.getElementsByTagName("body")[0];    
		    var tnode = document.createElement('div');    // Create the layer.        
		    tnode.style.position='absolute';                 // Position absolutely        
		    tnode.style.top='0px';                           // In the top        
		    tnode.style.left='0px';                          // Left corner of the page        
		    tnode.style.overflow='hidden';                   // Try to avoid making scroll bars                    
		    tnode.style.display='none';                      // Start out Hidden        
		    tnode.id='darkenScreenObject';                   // Name it so we can find it later    
		    tbody.appendChild(tnode);                            // Add it to the web page    
		    dark=document.getElementById('darkenScreenObject');  // Get the object.  
		}  
		
		if (vis) {   // Calculate the page width and height     
		
		    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {        
			    var pageWidth = document.body.scrollWidth+'px';        
			    var pageHeight = document.body.scrollHeight+'px';    
			} else if( document.body.offsetWidth ) {      
				var pageWidth = document.body.offsetWidth+'px';      
				var pageHeight = document.body.offsetHeight+'px';    
			} else {       
				var pageWidth='100%';       
				var pageHeight='100%';    
			}   
			    
			//reload is not going to bottom
			var pageHeight='150%';
			var pageWidth='100%'; 
			//set the shader to cover the entire page and make it visible.    
			dark.style.opacity=opaque;                          
			dark.style.MozOpacity=opaque;                       
			dark.style.filter='alpha(opacity='+opacity+')';     
			dark.style.zIndex=zindex;            
			dark.style.backgroundColor=bgcolor;      
			dark.style.width= pageWidth;    
			dark.style.height= pageHeight;    
			dark.style.display='block';                            
			
		} else {     
			dark.style.display='none';  
		}

	} //end of function
	
	
	
	//this is the function for calling the pdf window
function rpdfopen(name, w, h) {

   //this is for the cc reports on individual tickets and reports
   url="include/php/invoices/"+document.getElementById('theheader').value+"_user_invoice.pdf";

 w += 32;
 h += 96;
 var win = window.open(url, name, 'width=' + w + ', height=' + h + ', ' + 'location=no, menubar=no, ' + 'status=no, toolbar=no, scrollbars=no, resizable=yes');

}
