function ts_check_all(document,checkbox_name,value) {
        var n = 1;
        while ( document.all[checkbox_name+'_'+n] ) { document.all[checkbox_name+'_'+n++].checked = value; }
}

function ts_email_check(email) {
        var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
        var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
        return (!r1.test(email) && r2.test(email));
}

var win = null;
function NewWindow(mypage,myname,w,h,scroll,resizable){
lp = (screen.width) ? (screen.width-w)/2 : 0;
tp = (screen.height) ? (screen.height-h)/2-35 : 0;
scroll = (scroll) ? scroll : 'yes';
resizable = (resizable) ? resizable : 'no';

//"fullscreen=no,toolbar=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,directories=yes,location=yes,width=454,height=545,left=32,top=12")
win = window.open(mypage,myname,'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars='+scroll+',resizable='+resizable+',directories=no,location=no,width='+w+',height='+h+',left='+lp+',top='+tp)
}
//var win = null;
//function NewWindow(mypage,myname,w,h,scroll,resizable){
//LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
//TopPosition = (screen.height) ? (screen.height-h)/2-35 : 0;

//if (resizable='yes') {settings ='height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable';}
//if (resizable='no') {settings ='height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll;}
//win = window.open(mypage,myname,settings)
//}

function ts_window(mypage,myname,w,h,scroll,resizable){
        NewWindow(mypage,myname,w,h,scroll,resizable);
}

function ts_check_date(date, msg) {
        var error = 0;
        var ts_year=parseInt(date.value.substring(6),10);
        var ts_month=parseInt(date.value.substring(3,5),10);
        var ts_day=parseInt(date.value.substring(0,2),10);

        if (String(Number(date.value.substring(6)))=='NaN') { error = 1; };
        if (String(Number(date.value.substring(3,5)))=='NaN') { error = 1; };
        if (String(Number(date.value.substring(0,2)))=='NaN') { error = 1; };
        if (String(Number(date.value.substring(6)))=='NaN') { error = 1; };
        if (String(Number(date.value.substring(3,5)))=='NaN') { error = 1; };
        if (String(Number(date.value.substring(0,2)))=='NaN') { error = 1; };
        if (date.value.length<=0 || date.value=='00.00.0000' || date.value=='00/00/0000') { error = 1; }
        if (date.value.length!=10 || (date.value.substring(2,3)!='.' && date.value.substring(2,3)!='/') || (date.value.substring(5,6)!='.' && date.value.substring(5,6)!='/')) { error = 1; }
        if (ts_month<1 || ts_month>12) { error = 1; }
        if ( (ts_month==1 || ts_month==3 || ts_month==5 || ts_month==7 || ts_month==8 || ts_month==10 || ts_month==12) && (ts_day<1 || ts_day>31) ) { error = 1; }
        if ( (ts_month==4 || ts_month==6 || ts_month==9 || ts_month==11) && (ts_day<1 || ts_day>30) ) { error = 1; }
        if ( ts_month==2 && (ts_day<1 || ts_day>28) && ts_year%4!=0 ) { error = 1; }
        if ( ts_month==2 && (ts_day<1 || ts_day>29) && ts_year%4==0 ) { error = 1; }

        if ( error == 1 ) {
                alert(msg);
                date.select();
                return 0;
        } else {
                date.value = date.value.substring(0,2) + '.' + date.value.substring(3,5) + '.' + date.value.substring(6);
                return 1;
        }
}

function ts_change_lang(curr_lang_id, selObj){
  var curr_link = location.href;
  var new_link = '';
  if ( curr_link.indexOf('lang_id='+curr_lang_id) >= 0) {
    new_link=replaceSubstring(curr_link,'lang_id='+curr_lang_id,'lang_id='+selObj.options[selObj.selectedIndex].value);
  } else {
    if ( curr_link.indexOf('?') == -1) {
      new_link=curr_link+'?lang_id='+selObj.options[selObj.selectedIndex].value;
    } else {
      new_link=curr_link+'&lang_id='+selObj.options[selObj.selectedIndex].value;
    }
  }
  self.location.href=new_link;
}

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring"

function formatNumber( num ) {
        var isNegative = false;
        num = num.toString().replace(/\\./g,'');
        if( isNaN( num ) ) { num = "0"; }
        if ( num < 0 ) {
                num = Math.abs( num );
                isNegative = true;
        }
        cents = Math.floor( ( num * 100 + 0.5 ) % 100 );
        num = Math.floor( ( num * 100 + 0.5 ) / 100 ).toString();
        if ( cents < 10 ) {
                cents = "0" + cents;
        }
        for ( i = 0; i < Math.floor( ( num.lenght - ( 1 + i ) ) / 3 ); i++) {
                num = num.substring( 0 ,num.lenght - ( 4 * i + 3 ) ) + '.' + num.substring( num.lenght - ( 4 * i + 3 ) );
        }

        var result = num; // + ',' + cents;
        if ( isNegative ) {
                result = "-" + result;
        }
        return result;
}
