// JavaScript Document
var GLOBAL_elementName = "";
var GLOBAL_contactCost = 0;


// Toggle the visibility of a div
function toggleVisibilityDiv(divID) {
	var visibleState = document.getElementById(divID).style.display;
	if (visibleState != "block") {
		document.getElementById(divID).style.display = "block";
	} 
	else {
		document.getElementById(divID).style.display = "none";
	}
}


function verifyDeleteForm() {
	var verify; 
	verify = confirm('Are you sure you want to delete?'); 
	return verify;
}



function xGetElementById(e) {
   if(typeof(e)!='string')    
      return e;
	  
   if(document.getElementById) 
       e=document.getElementById(e);
   else if(document.all) 
       e=document.all[e];
   else 
       e=null;
   return e;
}



// Credit card standard checker
function luhn_check(number) {
  // Strip any non-digits (useful for credit card numbers with spaces and hyphens)
  var number=number.replace(/\D/g, '');

  // Set the string length and parity
  var number_length=number.length;
  var parity=number_length % 2;

  // Loop through each digit and do the maths
  var total=0;
  for (i=0; i < number_length; i++) {
    var digit=number.charAt(i);
    // Multiply alternate digits by two
    if (i % 2 == parity) {
      digit=digit * 2;
      // If the sum is two digits, add them together (in effect)
      if (digit > 9) {
        digit=digit - 9;
      }
    }
    // Total up the digits
    total = total + parseInt(digit);
  }

  // If the total mod 10 equals 0, the number is valid
  if (total % 10 == 0) {
    return true;
  } else {
    return false;
  }

}

// check card length and prefix against standards
function validateCreditCard(cardNum, cardType) {
	var errorMsg = "";
	
	switch (cardType) {
		case "MasterCard":
			if (cardNum.length != 16) {
				errorMsg += "MasterCard is not the correct length.\n";
			}
			prefix = cardNum.substring(0,2);
			if ((prefix < 51) || (prefix > 55)) {
				errorMsg += "MasterCard did not start with the correct prefix.\n";
			}
		break;
		case "Visa":
			if ((cardNum.length != 16) && (cardNum.length != 13)) {
				errorMsg += "Visa Card is not the correct length.\n";
			}
			if (cardNum.substring(0,1) != '4') {
				errorMsg += "Visa Card did not start with the correct prefix.\n";
			}
		break;
		case "Discover":
			if (cardNum.length != 16) {
				errorMsg += "Discover Card is not the correct length.\n";
			}
			
			if ((cardNum.substring(0,4) != '6011') && (cardNum.substring(0,3) != '622') 
				&& (cardNum.substring(0,2) != '64') && (cardNum.substring(0,2) != '65')) {
				errorMsg += "Discover Card did not start with the correct prefix.\n";
			}
		break;
		default: 
		break;	
	}

	if (!luhn_check(cardNum)) {
		errorMsg += "Your cardType card number is not valid.\n";
	}
	
	return errorMsg;
}

// email checker
function emailCheck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		var errorMsg = "";
		if (str.indexOf(at)==-1){
		   errorMsg = "Invalid E-mail Address";
		   return errorMsg;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   errorMsg = "Invalid E-mail Address";
		   return errorMsg;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    errorMsg = "Invalid E-mail Address";
		    return errorMsg;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    errorMsg = "Invalid E-mail Address";
		    return errorMsg;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    errorMsg = "Invalid E-mail Address";
		    return errorMsg;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    errorMsg = "Invalid E-mail Address";
		    return errorMsg;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    errorMsg = "Invalid E-mail Address";
		    return errorMsg;
		 }

 		 return errorMsg;					
}


function clearValue(elementName) {
	elementName.value = "";
}


