function alertmsg(var1,var2,var3){
//	alert(var1 +"\n"+ var2 +"\n"+ var3);
	document.getElementById('alertmsgtitle').innerHTML = var1;
	document.getElementById('alertmsgbody').innerHTML = var2;
	document.getElementById('alertmsgbutton').innerHTML =var3;

	document.getElementById('alertmsgouter').style.display = "block";
	document.getElementById('alertmsgbg').style.display = "block";
	winheight = getHeight();
	winscroll = getScrollY();
	topY = winscroll/2;
	
	document.getElementById('alertmsgouter').style.marginTop = topY +'px';
	document.getElementById('alertmsgbg').style.height = winheight +'px';
}

function closealert(){
	document.getElementById('alertmsgouter').style.display = "none";
	document.getElementById('alertmsgbg').style.display = "none";
}

function getHeight() {
    var D = document;

	newheight = Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );

	return newheight;
}

function getScrollY() {
  var scrOfY = 300;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;

  } else if( document.documentElement && document.documentElement.scrollTop  ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }

  return scrOfY;
}

function toggle_error(ev1,ev2){
	mainobj = document.getElementById(ev1).childNodes;

	var childid = ev1+'-error';
	child = document.getElementById(childid);

	if (ev2){
		if (child){
			var pdiv = document.getElementById(ev1);
			pdiv.removeChild(child);
		}

		var newdiv = document.createElement('div');
		newdiv.id = childid;
		newdiv.className = "ulabel-error";
		document.getElementById(ev1).appendChild(newdiv);
		document.getElementById(childid).innerHTML = ev2;
		return false;
		
	} else{
		if (child){
			var pdiv = document.getElementById(ev1);
			pdiv.removeChild(child);
			}

		return true;
	}
}



function validatefield(vv1,vv2){
	if (!vv1 || !vv2) return toggle_error(vv1);
	vvvalue = vv2.value;
	vva= vv2.className;
	vvb = vva.split(' ');
	err = "";
	dependent = '';
	dependentvalue='';
	optional='';
	var tooshort = 0;

	for(ax=0;ax< vvb.length;ax++){
		vvc = vvb[ax];
		if (vvc.indexOf('_d') == 0){
			vvct = vvc.substring(2);
			vvdt = vvct.split('|');
			dependent = vvdt[0];
			dependentvalue = vvdt[1];
		}

		if (vvc.indexOf('_v') == 0){
			vvcr= vvc.substring(2);
			vs = vvcr.split('|');
			vs0 = vs[0];
			vmin = vs[1];
			if(vs0.indexOf('optional') > -1) optional = true;
			vvd = vs0.split('_');
		}
	}

	if (optional && !vvvalue) return toggle_error(vv1);
	
	if (dependent){
		if (dependentvalue && document.getElementById(dependent).value != dependentvalue) return toggle_error(vv1);
		if (!document.getElementById(dependent).value) return;
	}

	for(vx=0;vx<vvd.length;vx++){
		vvdd = vvd[vx];

		switch(vvdd){
			case "ssn":
				var re = /^\d{3}\-\d{2}\-\d{4}$/;
				if (vvvalue && !re.test(vvvalue)){
					var newval = vvvalue.replace(/\D/g,'');
					if (newval.length == 9){
						ssn1 = newval.substring(0,3);
						ssn2 = newval.substring(3,5);
						ssn3 = newval.substring(5);
					} else {
						ssn1 = newval.substring(1,3);
						ssn2 = newval.substring(3,5);
						ssn3 = newval.substring(5);
					}

					if (ssn1.length == 3 && ssn2.length ==2 && ssn3.length==4) {
						vv2.value =  ssn1 +"-"+ ssn2 +"-"+ssn3;
					} else {
						err += 'Social Security Number must be in the format 123-45-6789';
					}
				}
			break;
			case "taxid":
				var re = /^\d{2}\-\d{7}$/;
				if (vvvalue && !re.test(vvvalue)){
					var newval = vvvalue.replace(/\D/g,'');
					if (newval.length == 9){
						tid1 = newval.substring(0,2);
						tid2 = newval.substring(2,9);
					} else {
						tid1 = newval.substring(1,2);
						tid2 = newval.substring(2);
					}

					if (tid1.length == 2 && tid2.length ==7) {
						vv2.value =  tid1 +"-"+ tid2;
					} else {
						err += 'Tax ID must be in the format 12-3456789';
					}
				}
			break;
			case "phone":
				var re = /^[1-9]\d{2}\-\d{3}\-\d{4}$/;
				if (vvvalue && !re.test(vvvalue)){
					var newval = vvvalue.replace(/\D/g,'');
					if (newval.length == 10){
						phone1 = newval.substring(0,3);
						phone2 = newval.substring(3,6);
						phone3 = newval.substring(6);
					} else {
						phone1 = newval.substring(1,4);
						phone2 = newval.substring(4,7);
						phone3 = newval.substring(7);
					}

					if (phone1.length == 3 && phone2.length ==3 && phone3.length==4) {
						vv2.value =  phone1 +"-"+ phone2 +"-"+phone3;
					} else {
						err += 'Phone # must be in the format 333-555-7890';
					}
				}
			break;
			case "alphanumeric":
				var re = /[^a-zA-Z0-9]/;
				if (vvvalue && re.test(vvvalue)){
					err += 'Please enter alphabets and numbers only<BR>';
				}
			break;
			case "digits":
				var re = /\D/;
				if (vvvalue && re.test(vvvalue)){
					err += 'Please enter numbers only<BR>';
				}
			break;
			case 'email':
				var re = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if (vvvalue && !re.test(vvvalue)){
					err +='This is not a valid email address<BR>';
				}
			break;
			case 'creditcard':
				iscard = checkccnum(vvvalue);
				if (!iscard){
					err += ccErrors[ccErrorNo] +'<BR>';
				}
			break;
			case 'cvv':
				iscvv = checkcvv();
				if (iscvv){
					err += iscvv;
				}
			break;
			case 'required':
				if (!vvvalue){
					err += 'This field is required<BR>'; 
				}
			break;
		}

		if (vv2.value && vmin && !isNaN(vmin) && (vvvalue.length < vmin) && tooshort < 1){
			err += 'This field is too short<BR>';
			tooshort++;
		}
	}

	return toggle_error(vv1,err);

}

function openterms(){
	openwin('/terms','mywindow','width=960,height=690,toolbar=no,location=no,menubar=no,copyhistory=no,scrollbars=no');
}

function openwin(url,tgt,options){
	tgt = (tgt) ? tgt : "newwin";
	options = (options) ? options : 	'width=960,height=690,toolbar=no,location=no,menubar=no,copyhistory=no,scrollbars=no';
	window.open(url,tgt,options);
}


function checkcvv(){
	cardtype = document.getElementById('Users_pay_infoCcType').value;
	cvv = document.getElementById('Users_pay_infoCcCvv').value;

	if (cardtype && cvv){
		switch (cardtype){
			case "AmEx":
				if (cvv.length != 4){
					return "Your Security code must be 4 digits long";
				}
			break;
			default:
				if (cvv.length != 3){
					return "Your Security code must be 3 digits long";
				}
			break;
		}
	}
	return;
}


var ccErrorNo = 0;
var ccErrors = new Array ()

ccErrors [0] = "Unknown card type";
ccErrors [1] = "No card number provided";
ccErrors [2] = "Credit card number is in invalid format";
ccErrors [3] = "Credit card number is invalid";
ccErrors [4] = "Credit card number has an inappropriate number of digits";
ccErrors [5] = "Credit card number and Card type does not match";

function checkccnum(cardnumber) {
	cardname = document.getElementById('Users_pay_infoCcType').value;
  // Array to hold the permitted card characteristics
  var cards = new Array();

  // Define the cards we support. You may add addtional card types as follows.
  
  //  Name:         As in the selection box of the form - must be same as user's
  //  Length:       List of possible valid lengths of the card number for the card
  //  prefixes:     List of possible prefixes for the card
  //  checkdigit:   Boolean to say whether there is a check digit
  
  cards [0] = {name: "Visa", 
               length: "13,16", 
               prefixes: "4",
               checkdigit: true};
  cards [1] = {name: "MasterCard", 
               length: "16", 
               prefixes: "51,52,53,54,55",
               checkdigit: true};
  cards [2] = {name: "DinersClub", 
               length: "14,16", 
               prefixes: "305,36,38,54,55",
               checkdigit: true};
  cards [3] = {name: "CarteBlanche", 
               length: "14", 
               prefixes: "300,301,302,303,304,305",
               checkdigit: true};
  cards [4] = {name: "AmEx", 
               length: "15", 
               prefixes: "34,37",
               checkdigit: true};
  cards [5] = {name: "Discover", 
               length: "16", 
               prefixes: "6011,622,64,65",
               checkdigit: true};
  cards [6] = {name: "JCB", 
               length: "16", 
               prefixes: "35",
               checkdigit: true};
  cards [7] = {name: "enRoute", 
               length: "15", 
               prefixes: "2014,2149",
               checkdigit: true};
  cards [8] = {name: "Solo", 
               length: "16,18,19", 
               prefixes: "6334,6767",
               checkdigit: true};
  cards [9] = {name: "Switch", 
               length: "16,18,19", 
               prefixes: "4903,4905,4911,4936,564182,633110,6333,6759",
               checkdigit: true};
  cards [10] = {name: "Maestro", 
               length: "12,13,14,15,16,18,19", 
               prefixes: "5018,5020,5038,6304,6759,6761",
               checkdigit: true};
  cards [11] = {name: "VisaElectron", 
               length: "16", 
               prefixes: "417500,4917,4913,4508,4844",
               checkdigit: true};
  cards [12] = {name: "LaserCard", 
               length: "16,17,18,19", 
               prefixes: "6304,6706,6771,6709",
               checkdigit: true};
               
  // Establish card type
  var cardType = -1;
  for (var i=0; i<cards.length; i++) {

 // See if it is this card (ignoring the case of the string)
    if (cardname.toLowerCase () == cards[i].name.toLowerCase()) {
     cardType = i;
     break;
    }
  }
  
  // If card type not found, report an error
  if (cardType == -1) {
     ccErrorNo = 0;
     return false; 
  }
   
  // Ensure that the user has provided a credit card number
  if (cardnumber.length == 0)  {
     ccErrorNo = 1;
     return false; 
  }
    
  // Now remove any spaces from the credit card number
  cardnumber = cardnumber.replace (/\s/g, "");
  
  // Check that the number is numeric
  var cardNo = cardnumber
  var cardexp = /^[0-9]{13,19}$/;
  if (!cardexp.exec(cardNo))  {
     ccErrorNo = 2;
     return false; 
  }
       
  // Now check the modulus 10 check digit - if required
  if (cards[cardType].checkdigit) {
    var checksum = 0;                                  // running checksum total
    var mychar = "";                                   // next char to process
    var j = 1;                                         // takes value of 1 or 2
  
    // Process each digit one by one starting at the right
    var calc;
    for (i = cardNo.length - 1; i >= 0; i--) {
    
      // Extract the next digit and multiply by 1 or 2 on alternative digits.
      calc = Number(cardNo.charAt(i)) * j;
    
      // If the result is in two digits add 1 to the checksum total
      if (calc > 9) {
        checksum = checksum + 1;
        calc = calc - 10;
      }
    
      // Add the units element to the checksum total
      checksum = checksum + calc;
    
      // Switch the value of j
      if (j ==1) {j = 2} else {j = 1};
    } 
  
    // All done - if checksum is divisible by 10, it is a valid modulus 10.
    // If not, report an error.
    if (checksum % 10 != 0)  {
     ccErrorNo = 3;
     return false; 
    }
  }  

  // The following are the card-specific checks we undertake.
  var LengthValid = false;
  var PrefixValid = false; 
  var undefined; 

  // We use these for holding the valid lengths and prefixes of a card type
  var prefix = new Array ();
  var lengths = new Array ();
    
  // Load an array with the valid prefixes for this card
  prefix = cards[cardType].prefixes.split(",");
      
  // Now see if any of them match what we have in the card number
  for (i=0; i<prefix.length; i++) {
    var exp = new RegExp ("^" + prefix[i]);
    if (exp.test (cardNo)) PrefixValid = true;
  }
      
  // If it isn't a valid prefix there's no point at looking at the length
  if (!PrefixValid) {
     ccErrorNo = 5;
     return false; 
  }
    
  // See if the length is valid for this card
  lengths = cards[cardType].length.split(",");
  for (j=0; j<lengths.length; j++) {
    if (cardNo.length == lengths[j]) LengthValid = true;
  }
  
  // See if all is OK by seeing if the length was valid. We only check the length if all else was 
  if (!LengthValid) {
     ccErrorNo = 4;
     return false; 
  };   
  
  // The credit card is in the required format.
  return true;
}

function toggleview(var1){
	if (document.getElementById(var1)){
		divobj = document.getElementById(var1);

		if (divobj.style.display == "none" || !divobj.style.display){
			divobj.style.display = "block";
		} else {
			divobj.style.display = "none";
		}
	}

	linkname = var1+"-link";
	if (document.getElementById(linkname)){
		divlink = document.getElementById(linkname);
		if (divlink.style.display == "none"){
			divlink.style.display = "block";
		} else {
			divlink.style.display = "none";
		}
	}
}
