var pcb;

if(typeof window.screenLeft != 'undefined') {
  var wl=window.screenLeft+75;
  var wt=window.screenTop+75;
} else {
  var wl=window.screen.left+75;
  var wt=window.screen.top+75;
}

function run_first() {
 
  if(typeof document.form1 == 'object') {
  	if(!document.form1.elements[0].disabled) { 
  		document.form1.elements[0].focus(); 
  	}
  }
  if(typeof page_init != 'undefined') { page_init(); }
}
function URLencode(sStr) {
    return escape(sStr).replace(/\+/g, '%2C').replace(/\"/g,'%22').replace(/\'/g, '%27');
}


function inactive_link() {
  return;
}

function format_input(obj,type) {

  var new_value = '';
  switch(type) {

    case 'name':

      var ignore_case=false;

      for(x=0;x<obj.value.length;x++) {
        if(obj.value.charAt(x)=='"') {
          if(ignore_case) { ignore_case=false; } else { ignore_case=true; }
        } else {

        if(ignore_case) {

            new_value = new_value.concat(obj.value.charAt(x));

          } else {

				if(obj.value.charAt(x-1)=="'") {
					new_value=new_value.concat(obj.value.charAt(x));
				} else {
	            if(x==0 || ((obj.value.charAt(x-1) < 'a' || obj.value.charAt(x-1) > 'z') && (obj.value.charAt(x-1) < 'A' || obj.value.charAt(x-1) > 'Z'))) {
	              new_value=new_value.concat(obj.value.charAt(x).toUpperCase());
	            } else {
	              new_value=new_value.concat(obj.value.charAt(x).toLowerCase());              
	            }
				}
          }
        }
      }

    break;

    case 'state':

      new_value = obj.value.charAt(0).toUpperCase() + obj.value.charAt(1).toUpperCase();

    break;

    case 'zipcode':

      for(x=0;x<obj.value.length;x++) {
        if(obj.value.charAt(x) >= '0' && obj.value.charAt(x) <= '9') {
          if(new_value.length==5) { new_value = new_value.concat('-'); }
          new_value = new_value.concat(obj.value.charAt(x));
        }
      }

    break;

    case 'ccard':

      for(x=0;x<obj.value.length;x++) {
        if(obj.value.charAt(x) >= '0' && obj.value.charAt(x) <= '9') {
          if(new_value.length==4 || new_value.length==9 || new_value.length==14)
            { new_value = new_value.concat('-'); }
          new_value = new_value.concat(obj.value.charAt(x));
        }
      }

    break;

    case 'email':

      for(x=0;x<obj.value.length;x++) {
        if((obj.value.charAt(x) >= 'a' && obj.value.charAt(x) <= 'z')
        || (obj.value.charAt(x) >= '0' && obj.value.charAt(x) <= '9')
        || (obj.value.charAt(x) >= 'A' && obj.value.charAt(x) <= 'Z')
        || obj.value.charAt(x)=='_' || obj.value.charAt(x)=='-'
        || obj.value.charAt(x)=='.' || obj.value.charAt(x)=='@') {
          new_value = new_value.concat(obj.value.charAt(x).toLowerCase());
        }
      }

    break;

    case 'phone':

      for(x=0;x<obj.value.length;x++) {
        if(obj.value.charAt(x) >= '0' && obj.value.charAt(x) <= '9') {
          if(new_value.length==3 || new_value.length==7)
            { new_value = new_value.concat('-'); }
          new_value = new_value.concat(obj.value.charAt(x));
        }

     }

    break;
    
    case 'username':

      for(x=0;x<obj.value.length;x++) {
        if((obj.value.charAt(x) >= 'a' && obj.value.charAt(x) <= 'z')
        || (obj.value.charAt(x) >= '0' && obj.value.charAt(x) <= '9')
        || (obj.value.charAt(x) >= 'A' && obj.value.charAt(x) <= 'Z')) {
          new_value = new_value.concat(obj.value.charAt(x).toLowerCase());
        }
      }
          
	 break;
	 
    case 'url':

      for(x=0;x<obj.value.length;x++) {
        if((obj.value.charAt(x) >= 'a' && obj.value.charAt(x) <= 'z')
        || (obj.value.charAt(x) >= '0' && obj.value.charAt(x) <= '9')
        || (obj.value.charAt(x) >= 'A' && obj.value.charAt(x) <= 'Z')
		  || obj.value.charAt(x) == '.' || obj.value.charAt(x) == '-') {
          new_value = new_value.concat(obj.value.charAt(x).toLowerCase());
        }
      }
          
	 break;	 

	 case 'decimal':

		for(x=0;x<obj.value.length;x++) {
		if((obj.value.charAt(x) >= '0' && obj.value.charAt(x) <= '9') ||
			obj.value.charAt(x) == '.') {
				new_value = new_value.concat(obj.value.charAt(x));							
			}
		}
		if(new_value=='') { new_value='0.00'; }
		
	 break;

	 case 'integer':

		for(x=0;x<obj.value.length;x++) {
		if(obj.value.charAt(x) >= '0' && obj.value.charAt(x) <= '9') {
				new_value = new_value.concat(obj.value.charAt(x));							
			}
		}

		if(new_value=='') { new_value='0'; }


		
	 break;


  }

obj.value = new_value;

}

function send_form(form_obj,required,serverValidate) {
	
  var validated = true;
  var field_required = '';
  var post_output = '';
  	
  for(i=0;i<form_obj.elements.length;i++) {

    fo_type = form_obj[i].type;
    fo_name = form_obj[i].name;
    fo_value = form_obj[i].value;
	 fo_value = fo_value.replace(/=/g,'~@E');    

	 if(fo_name!='') {  
    	if(typeof required!='undefined' && required.search(fo_name)!=-1 && (fo_value=='' || fo_value=='undefined')) { validated=false; field_required += fo_name + ' ';}
    }
    	
	 switch(fo_type) {

      case 'checkbox':
        if(form_obj[i].checked) { post_output += '<input type="hidden" name="'+fo_name+'" value="'+fo_value+'">'; }
      break;

      case 'radio':
        if(form_obj[i].checked) { post_output += '<input type="hidden" name="'+fo_name+'" value="'+fo_value+'">'; }
      break;

      case 'text':
        post_output += '<input type="hidden" name="'+fo_name+'" value="'+fo_value+'">';	       
      break;

      case 'password':
        post_output += '<input type="hidden" name="'+fo_name+'" value="'+fo_value+'">';
      break;

      case 'textarea':
        post_output += '<input type="hidden" name="'+fo_name+'" value="'+fo_value+'">';
      break;

      case 'hidden':
        post_output += '<input type="hidden" name="'+fo_name+'" value="'+fo_value+'">';
      break;

      case 'select-one':
        post_output += '<input type="hidden" name="'+fo_name+'" value="'+form_obj[i].options[form_obj[i].selectedIndex].value+'">';        
      break;
      
    }

  }
  
  if(!validated) {
  	
  		alert("Please complete the following required field(s):\n"+field_required);
    	eval('document.'+form_obj.name + '.' + field_required.split(' ')[0] + '.focus();');
    	
  } else if(serverValidate != '' && serverValidate != undefined) {
  	
		window.form_obj = form_obj;
		var comm = document.getElementById('comm');
		comm = comm.contentDocument || comm.contentWindow.document;	
		comm.open();
		comm.write('<form id="dPost" name="dPost" method="POST" action="validate.php?type=validate_form">');
		comm.write('<input type="hidden" name="validate_form" value="'+serverValidate+'">');		
		comm.write(post_output);
		comm.write('</form>');
		comm.close();
		comm.getElementById('dPost').submit();
	
  } else {
			
		form_obj.submit();
    
  }

}

function callback_accept() {
	window.form_obj.submit();		
}

function callback_error(msg,rFocus) {
	alert(msg);
	if(rFocus!='undefined') { 
		eval('document.'+window.form_obj.name + '.' + rFocus + '.focus()'); 
	}		
}

function disable_form(form_name) {
	
	var elem = eval("document."+form_name+".elements");
	for(var i = 0; i < elem.length; i++) {
		elem[i].disabled=true;
	}
}

function disable_date_entry(field_name) {

	document.getElementById(field_name+'_ID_Link').href='javascript:;';

}

function disable_form_field(field_name) {
	var elem = eval(field_name);
	elem.disabled=true;	
}

function showHide(show,hide,gfoc) {

	if(show!='') {
		show_a=show.split(',');
		for(i in show_a) { document.getElementById(show_a[i]).style.display='inline'; }
		if(gfoc!='') { document.getElementById(gfoc).focus(); }
	}
	if(hide!='') {
		hide_a=hide.split(',');	
		for(i in hide_a) { document.getElementById(hide_a[i]).style.display='none'; }
	}
}

function payment_changePrompt() {
  pay_type=document.form2.payment_type.selectedIndex;
  switch(pay_type) {
    case 0:
      document.form2.payment_prompt.value='';
      document.form2.payment_argument.value='';
      document.form2.payment_amount.focus();
    break;
    case 1:
      document.form2.payment_prompt.value='Check Num:';
      document.form2.payment_argument.value='';
      document.form2.payment_argument.focus();
    break;
    case 2:
      document.form2.payment_prompt.value='Card Number:';
      document.form2.payment_argument.value='';
      document.form2.payment_argument.focus();
    break;
    case 3:
      document.form2.payment_prompt.value='Gift Number:';
      document.form2.payment_argument.value='';
      document.form2.payment_argument.focus();
    break;
    case 4:
      document.form2.payment_prompt.value='Coup Code:';
      document.form2.payment_argument.value='';
      document.form2.payment_argument.focus();
    break;
    case 5:
      document.form2.payment_prompt.value='Prev Order#:';
      document.form2.payment_argument.value='';
      document.form2.payment_argument.focus();
    break;
    case 6:
      document.form2.payment_prompt.value='';
      document.form2.payment_argument.value='';
    break;
  }
}

function payment_focusCC() {
  if(document.form2.payment_type.selectedIndex==0) { document.form2.payment_type.selectedIndex=2; }
}

function payment_cback() {
	
	clearTimeout(pcb);
	pcb=setTimeout("payment_parseCC()",600);
			
}

function payment_parseCC() {
	
	clearTimeout(pcb);
	
   pa = document.form2.payment_argument.value;
   
   if(pa.indexOf(';') > -1 && pa.indexOf('=') > -1) {
    a=pa.split(';'); b=a[1].split('=');
    document.form2.payment_argument.value = b[0].replace (/[^\d]/g, "");
    yr = parseInt(b[1].substr(0,2)); mo = b[1].substr(2,2); 
   } else {       
    if(pa.substr(0,2)=='%B') { pa = pa.substr(3); } else if (pa.substr(0,1)=='B') { pa = pa.substr(2); }    
    a=pa.split('^');     
    document.form2.payment_argument.value = a[0].replace (/[^\d]/g, "");
    yr = parseInt(a[2].substr(0,2)); mo = a[2].substr(2,2);  
   } 
      
   document.form2.payment_month.selectedIndex = (mo-1);
   yr_obj = document.form2.payment_year;
   for(i=0;i<yr_obj.length;i++) { if(yr_obj[i].value==yr) { yr_obj.selectedIndex = i; } }

   document.form2.payment_button.click();

}

function reconcile_mark(transaction_id,transaction_type) {	

		var comm = document.getElementById('comm');
		comm.src = 'validate.php?type=mark_reconcile&transaction_id='+transaction_id+'&transaction_type='+transaction_type;
				
}

function reconcile_result(cl,ncl) {
	document.form1.not_cleared.value = numberToCurrency(ncl);
	document.form1.total_cleared.value = numberToCurrency(cl);	
}

function numberToCurrency(number, decimalSeparator, thousandsSeparator, nDecimalDigits){
    //default values
    decimalSeparator = decimalSeparator || '.';
    thousandsSeparator = thousandsSeparator || ',';
    nDecimalDigits = nDecimalDigits || 2;

    var fixed = number.toFixed(nDecimalDigits), //limit/add decimal digits
        parts = RegExp('^(-?\\d{1,3})((\\d{3})+)\\.(\\d{'+ nDecimalDigits +'})$').exec( fixed ); //separate begin [$1], middle [$2] and decimal digits [$4]

    if(parts){ //number >= 1000 || number <= -1000
        return parts[1] + parts[2].replace(/\d{3}/g, thousandsSeparator + '$&') + decimalSeparator + parts[4];
    }else{
        return fixed.replace('.', decimalSeparator);
    }
}

