/* Valentin Malakhov . July 2004 Sydney
 Good Business Technology Pty Ltd
*/
function printpage() {
 window.print();  
}

function CancelIt() {
  window.close();
}

function SubmitParent() {
  opener.LinkFormReSubmit();
}

function CloseIt(location) {
  if(location == '')
    opener.location.href=opener.location.href;
  else
    opener.location.href=location;
  window.close();
}

function checkUncheckAll(theElement) {
  var theForm = theElement.form, z = 0;
  do {
    if (theForm[z].type == 'checkbox' && theForm[z].name != 'checkall')
      theForm[z].checked = theElement.checked;
    z++;
  } while(z < theForm.length);
}

function checkAll(elementName, theElement) {
  var theForm = theElement.form, z = 0;
  var check = true;
  var theAllElement = document.getElementById(elementName);
  do {
    if (theForm[z].type == 'checkbox' && theForm[z].name != elementName)
      check = check && theForm[z].checked;
    z++;
  } while(z < theForm.length);
  theAllElement.checked = check;
}

function preload_images(path,pics) {
  var images = new Array;
  for (var pic_num=0; pic_num<pics.length; pic_num++) {
   if (pics[pic_num]!='') {
    images[pic_num] = new Image();
    images[pic_num].src = (path + pics[pic_num]);
   }
  }
  return (images);
}

function validateCharacters(component){
  validateAllowed(component,'_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
}

function validateCharactersWithSpace(component){
  validateAllowed(component,'_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ')
}

function validateCharactersWithDash(component){
  validateAllowed(component,'_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-')
}

function validateCharactersWithDashAndSpace(component){
  validateAllowed(component,'_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-')
}

function validateDigits(component){
  validateAllowed(component,'1234567890')
}
  
function validateDigitsWithSpace(component){
  validateAllowed(component,'1234567890 ')
}

function validateDigitsWithDecimal(component){
  validateAllowed(component,'1234567890.')
}

function validateDigitsWithDash(component){
  validateAllowed(component,'1234567890-')
}

function validateDigitsWithDashAndSpace(component){
  validateAllowed(component,'1234567890- ')
}

function validateDisallowedQuotes(component){
  validateDisallowed(component,'\'\"`')
}


function validateAllowed(component,allowed){
  var element=document.getElementById(component);
  str=element.value;
  newstr='';
  for(i=0;i<str.length;i++)
  {
    character=str.charAt(i);
    if(allowed.indexOf(character)>=0)
      newstr=newstr+character;
  }
  if(element.value!=newstr)
    element.value=newstr;
}

function validateDisallowed(component,disallowed){
  var element=document.getElementById(component);
  str=element.value;
  for(i=0;i<disallowed.length;i++)
  {
    character=disallowed.charAt(i);
    var regEx = new RegExp (character, 'g') ;
    str=str.replace(regEx, '');
  }
  if(element.value!=str)
    element.value=str;;
}

function switchSource(which,$source){
  var img = eval("document.images." + which);
  img.src = $source;
}

//used by Character counter.
function getObject(obj) {
  var theObj;
  if(document.all) {
    if(typeof obj=="string") {
      return document.all(obj);
    } else {
      return obj.style;
    }
  }
  if(document.getElementById) {
    if(typeof obj=="string") {
      return document.getElementById(obj);
    } else {
      return obj.style;
    }
  }
  return null;
}

//Character counter.
function Count_character(input,output,text,characters) {
  var inputObj=getObject(input);
  var outputObj=getObject(output);
  var length=characters - inputObj.value.length;
  if(length <= 0) {
    length=0;
    text='<span class="disable"> '+text+' </span>';
    inputObj.value=inputObj.value.substr(0,characters);
  }
  outputObj.innerHTML = text.replace("{CHAR}",length);
}

function validDate (date)
{
    //no input
  if (!date)
  {
     return true;
  }
 
  // 5 signs are required
  if (date.length != 5 )
  {
    alert("Credit card expire date is invalid (MM/YY)");
    return false;
  }
  
    //convert date to string
 date=date.toString();

    //split the date
 date=date.split("/");
 
 if (date.length!=2)
 {
    alert("Credit card expire date is invalid (MM/YY)");
    return false;
 } 
    //delete 0 in month
 date[0]=parseInt(date[0],10)-1;
 
    //Conversion from YY to YYYY
 if (date[1].length==2) date[1]="20"+date[1];

    //new date object
 var controldate=new Date(date[1],date[0]);

    // get actual time, extract the year and add 10 years
 now = new Date();
 year1= now.getYear();
 year2= now.getYear()+10;

    //input date == JS-Date ??
 if (controldate.getMonth()==date[0] && controldate.getFullYear()==date[1] && date[0] > 0 && date[0] < 14&& date[1] >= year1  && date[1] <= year2)
     return true;

 else
 {
   alert("Credit card expire date is invalid (MM/YY)");
   return false;
 }
}


 // not working
function completeDate(component){
  var element=document.getElementById(component);
  str=element.value;
  newstr='';
  
  if (str.length == 2)
  {
    alert("2");
    newstr=str+"/";
    element.value=newstr;
  }
}

//__________________________________________________________

// Customization:
//
// There are 4 customization steps:
//

// Step 1:
// Between the quotes, type what the alert box message shall say when
// the credit card is unknown and the number did not pass?
// To force a line break, use "\n" (without the quotes).
// For no message, type "" (nothing between the quotes).

Step1Message = "Credit card is not known.\nCredit card number is invalid.";

//
// Step 2:
// Between the quotes, type what the alert box message shall say when
// the credit card is known and the number did not pass?
// To force a line break, use "\n" (without the quotes).
// For no message, type "" (nothing between the quotes).

Step2Message = "Credit card number is invalid.";

//
// Step 3:
// Between the quotes, type what the alert box message shall say when
// the credit card is unknown and the number passed?
// To force a line break, use "\n" (without the quotes).
// For no message, type "" (nothing between the quotes).

Step3Message = "Credit card is not known.";

//
// Step 4:
// When the credit card is known and the number passed,
// the script can display the credit card type and the
// number (stripped of hyphens and other non-digit characters).
// To display this message, type a Y or a y between the quotes.
// To not display this message, type "" (no space between the quotes).

DisplayAllOkayMessage = "y";



////////////////////////////////////////
//                                    //
// No further customization required. //
//                                    //
////////////////////////////////////////


////////////////////////////////////////
//
//    N O T E S :
//
// Three global variables are set when function Validate() is called.
// If appropriate, your application can consult these variables:
//
// Variable Name     Value
// ^^^^^^^^^^^^^     ^^^^^
// ccType          - The name of the credit card, if determinable.
//                   Otherwise null.
// ccNumberOkay    - Digit 1 if the credit card number is of a valid
//                   format. Otherwise digit 0.
// ccNumberChecked - The credit card number that was checked (all spaces
//                   and non-digit characters stripped out). Example:
//                   CC # 1234-5678-901-234 is checked as 12345678901234
//
////////////////////////////////////////



ccType = '';
ccNumberOkay = 0;
ccNumberChecked = '';

function StripNonDigits(number) {
var Re = /\d+/g;
if(Re.lastIndex > 1) { Re.lastIndex = 0; }
var Array = Re.exec(number);
if(Re.lastIndex < 1) { return 'X'; }
var ss = Array.join();
while(Re.lastIndex > 0 && Re.lastIndex < number.length) {
	Array = Re.exec(number);
	if(Array) { ss += Array.join(); }
	}
return ss;
} // StripNonDigits()


function GetType(number) {
var len = number.length;
var Re = /^5[1-5]/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		var Array = Re.exec(number);
			if(Array && len == 16) { return 'MasterCard'; }
Re = /^4/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
			if(Array && (len == 13 || len == 16)) { return 'Visa'; }
Re = /^3[47]/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
			if(Array && len == 15) { return 'American Express'; }
Re = /^(30[0-5]|3[68])/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
			if(Array && len == 14) { return 'Diners Club'; }
Re = /^6011/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
			if(Array && len == 14) { return 'Discover'; }
Re = /^(3|1800|2131)/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
			if(Array && (len == 15 || len == 16)) { return 'JCB'; }
Re = /^90360063/;
	/*if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
		if(Array && len == 16) { return 'SpotRewards'; }
Re = /^(90360064|502118)/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
		if(Array && len == 16) { return 'OneCard'; }
Re = /^90360069/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
		if(Array && len == 16) { return 'ShopSmart'; }
Re = /^90360070/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
		if(Array && len == 16) { return 'Giftcard'; }
Re = /^90360071/;
	if(Re.lastIndex > 1) { Re.lastIndex = 0; }
		Array = Re.exec(number);
		if(Array && len == 16) { return 'AusFuel'; }*/
return '';
} // GetType()


function Reverse(number) {
var n = '';
for(i = number.length; i >= 0; i--) { n += number.substr(i,1); }
return n;
} // Reverse()


function AddedTogether(number) {
var n = 0;
for(i = 0; i < number.length; i++) {
	var s = number.substr(i,1);
	var si = parseInt(s,10);
	if(i % 2 > 0) {
		var ii = si * 2;
		if(ii < 10) { n += ii; }
		else {
			var ss = ' ' + ii;
			for(xi = 1; xi < ss.length; xi++) {
				var xs = ss.substr(xi,1);
				var xsi = parseInt(xs,10);
				n += xsi;
				} // for
			} // else
		} // if
	else { n += si; }
	} // for
return n;
} // AddedTogether()


function Mod10(n) {
var reversed = Reverse(n);
var total = AddedTogether(reversed);
if(total % 10 > 0) { return 0; }
return 1;
} // Mod10()


function Validate(n) {
ccNumberChecked = StripNonDigits(n);
if(ccNumberChecked == 'X') {
	alert('Please supply a credit card number.');
	return false;
	}
ccType = GetType(ccNumberChecked);
ccNumberOkay = Mod10(ccNumberChecked);
var fourth = '-- Credit Card --\n' + ccType + '\n' + ccNumberChecked;
if(     Step1Message.length > 0 && ccType.length < 2 && ccNumberOkay == 0) { alert(Step1Message); }
else if(Step2Message.length > 0 && ccNumberOkay == 0                     ) { alert(Step2Message); }
else if(Step3Message.length > 0 && ccType.length < 2                     ) { alert(Step3Message); }
//else if(DisplayAllOkayMessage == "y" || DisplayAllOkayMessage == "Y"     ) { alert(fourth);       }
return false;
} // Validate()

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
