//fix for non-js (logic below will display accordingly)
document.write("<style type='text/css'>");
document.write(".edit {display: none;}");
document.write("div.fieldrow .exp-date {display: none;}");
document.write("div.fieldrow .csc {display: none;}");
document.write("p.addCardLink {display: block;}");
document.write("</style>");


// 2005.12.14 pkelley/kyeo
// Takes a comma-separated argument and creates an array out of it.
// If the argument contains only 1 value (instead of a comma separated list), make that a 1 element array
// Used in FormGroupCreditCard component by the following JS -- toggleDisplayProperty and toggleDisabledAttribute
function createArray(myval)
{
   var a = new Array();
   var input = myval;

   // if it's not array create one so we can loop over it
   if (input.constructor.toString().indexOf("Array") == -1) {
      // to do - remove any spaces form commas separated list
      a = input.split(",");
   }
   else {
      a = input;
   }
   return a;
}

// 2005.12.14 kyeo
// Disables a form field by setting an attribute "disabled" so the field will not be sent to the server
function toggleDisabledAttribute(myDiv,field,action){
   var a = new Array();
   a = createArray(myDiv);

   for (var i=0; i<a.length; i++) {
      if ( document.getElementById(a[i]) ) {
         var myField = document.getElementById(a[i]).getElementsByTagName(field);
			
         for (var x = 0; x<myField.length; x++) {
            if (action == 'set') myField[x].setAttribute('disabled','true');
            else myField[x].removeAttribute('disabled');
         }
      }
   }
}

// 2005.12.14 pkelley/kyeo                                                                                        
// Changes the class to hidden which is defined in the css                                                        
// as Display: none                                                                                               
// Pass in a comma separated list/array/scalar of ids                                                             
function toggleDisplayProperty(tag, display)
{
   var a = new Array();
   a = createArray(tag);

   var id = "";
   var show = (display == "show") ? true : false;

   for (var i=0; i<a.length; i++) {
      id = document.getElementById(a[i]);
		if (id) {
         //if (show) id.setStyle('display', 'block');
         //else id.setStyle('display', 'none');
			if( show ) YAHOO.util.Dom.setStyle(id, 'display', 'block');
			else YAHOO.util.Dom.setStyle(id, 'display', 'none');
      }
   }
}


// 2005.12.14 kyeo
// Used in hostedpayments/Billing.aml to hide/show credit card and bank form fields
function showMoreFieldsPayment(obj, isSelect)
{
	if( !obj && !isSelect ){
		//Firefox: correctly knows that we don't have a Select box with credit cards options
		//IE and Opera: will never detect this case
	   toggleDisplayProperty('fieldrowCCNumber,fieldrowCCExpDate,fieldrowCSC','show');
		toggleDisplayProperty('bank-fields,field-dob,dob-terms,cardIssueInformation,','hide');
	} else if( obj && isSelect ){
		//Firefox:  the first condition implies the second. Therefore proceed with the switch statement
				
			switch (obj.options[obj.selectedIndex].value)
			{
                                case "V": case "D": case "M": case "C":
                                        toggleDisabledAttribute('fieldrowStartDate','select','set');
                                        toggleDisabledAttribute('fieldrowStartDate,fieldrowCCDOB','input','set');
                                        toggleDisabledAttribute('fieldrowCCExpDate,fieldrowCSC','input','remove');
                                        toggleDisplayProperty('bank-fields,field-dob,dob-terms,cardIssueInformation,fieldrowCCDOB','hide');
                                        toggleDisplayProperty('fieldrowCCNumber,fieldrowCCExpDate,fieldrowCSC','show');
                                        break;
                                case"A":
                                        toggleDisabledAttribute('fieldrowStartDate','select','set');
                                        toggleDisabledAttribute('fieldrowStartDate,fieldrowCCDOB','input','set');
                                        toggleDisabledAttribute('fieldrowCCExpDate,fieldrowCSC','input','remove');
                                        toggleDisplayProperty('bank-fields,field-dob,dob-terms,cardIssueInformation,fieldrowCCDOB','hide');
                                        toggleDisplayProperty('fieldrowCCNumber,fieldrowCCExpDate,fieldrowCSC','show');
                                        break;
                                case "O": case "S":
                                        toggleDisabledAttribute('fieldrowStartDate','select','remove');
                                        toggleDisabledAttribute('fieldrowStartDate,fieldrowCCExpDate,fieldrowCSC','input','remove');
                                        toggleDisabledAttribute('fieldrowCCDOB','input','set');
                                        toggleDisplayProperty('fieldrowCSC','show');
                                        toggleDisplayProperty('fieldrowCCNumber,fieldrowCCExpDate,cardIssueInformation','show');
                                        toggleDisplayProperty('fieldrowCCDOB','hide');
                                        break;
                                case "N":
                                        toggleDisabledAttribute('fieldrowStartDate','select','set');
                                        toggleDisabledAttribute('fieldrowStartDate,fieldrowCSC','input','set');
                                        toggleDisabledAttribute('fieldrowCCExpDate,fieldrowCCDOB','input','remove');
                                        toggleDisplayProperty('bank-fields,field-dob,dob-terms,cardIssueInformation,fieldrowCSC','hide');
                                        toggleDisplayProperty('fieldrowCCNumber,fieldrowCCExpDate,fieldrowCCDOB','show');
                                        if (document.getElementById('country_code').value == 'IT' || document.getElementById('country_code').value == 'ES')
                                        {
                                                toggleDisabledAttribute('fieldrowCSC','input','remove');
                                                toggleDisplayProperty('fieldrowCSC','show');
                                        }
                                        break;
                                case "Q":
                                        toggleDisabledAttribute('fieldrowStartDate','select','set');
                                        toggleDisabledAttribute('fieldrowStartDate,fieldrowCSC','input','set');
                                        toggleDisabledAttribute('fieldrowCCExpDate,fieldrowCCDOB','input','remove');
                                        toggleDisplayProperty('bank-fields,field-dob,dob-terms,cardIssueInformation,fieldrowCSC','hide');
                                        toggleDisplayProperty('fieldrowCCNumber,fieldrowCCExpDate,fieldrowCCDOB','show');
                                        break;
                                case "L":
                                        toggleDisabledAttribute('fieldrowStartDate','select','set');
                                        toggleDisabledAttribute('fieldrowStartDate','input','set');
                                        toggleDisabledAttribute('fieldrowCSC,fieldrowCCExpDate','input','set');
                                        toggleDisabledAttribute('fieldrowCCDOB','input','remove');
                                        toggleDisplayProperty('bank-fields,field-dob,dob-terms,cardIssueInformation,fieldrowCSC,fieldrowCCExpDate','hide');
                                        toggleDisplayProperty('fieldrowCCNumber,fieldrowCCDOB','show');
                                        break;

                                default:
                                        toggleDisabledAttribute('fieldrowStartDate','select','set');
                                        toggleDisabledAttribute('fieldrowStartDate','input','set');
                                        toggleDisplayProperty('fieldrowCCNumber,fieldrowCCExpDate,cardIssueInformation,fieldrowCSC,fieldrowCCDOB','hide');
                                        if (obj.options[obj.selectedIndex].value == "")
                                                toggleDisplayProperty('bank-fields,field-dob,dob-terms','hide');
                                        else
                                                toggleDisplayProperty('bank-fields,field-dob,dob-terms','show');
                                        if( document.getElementById( 'cc_number_update_mode' )){
                                                toggleDisplayProperty('fieldrowCCNumber','show');
                                        }
				
			}
	} else if( obj && !isSelect ){
		//Firefox: will never detect this case
		//IE and OPERA: they see the INPUT fields
	   toggleDisplayProperty('fieldrowCCNumber,fieldrowCCExpDate,fieldrowCSC','show');
		toggleDisplayProperty('bank-fields,field-dob,dob-terms,cardIssueInformation,','hide');
	}
}

//clears the mm/yy fields onClick
function clearField(thefield){
	if ( thefield.defaultValue == thefield.value){
		thefield.value = "";
		YAHOO.util.Dom.setStyle(thefield, 'color', '#000');
	}
} 

//getCC.js
//sets opacity, checked status for cards
function getCC(sourceObj) {
	//credit cards
	var visa = document.getElementById("pm-visa");
	var electron = document.getElementById("pm-electron");
	var mastercard = document.getElementById("pm-mastercard");
	var discover = document.getElementById("pm-discover");
	var amex = document.getElementById("pm-amex");
	var jcb = document.getElementById("pm-jcb");

	// prefix for visa electron, new prefix need to be added here
	var isElectron = false;
	var electronPrefix4 = "4917,4913,4508,4844,";
	var electronPrefix6 = "417500,"	
	var electronPrefixArray4 = new Array();
	var electronPrefixArray6 = new Array();
	
	electronPrefixArray4 = electronPrefix4.split(",");
	electronPrefixArray6 = electronPrefix6.split(",");	
	
	// Get selected country; disable highlighting for Italy
	var selCountry = "";

	if(document.getElementById('country_code')){
	var country = document.getElementById('country_code');
		if (country.options) {
			selCountry = country.options[country.selectedIndex].value;
		}
		else {
			selCountry = country.value;
		}
	}
	var enableHighlight = (selCountry == 'IT') ? false : true;

	//In this instance the 'sourceObj' represent the input field object from which the function is invoked
	if (sourceObj) {
		
		var formObj;
		
		if(document.getElementById(sourceObj.form.id)){
			formObj = document.getElementById(sourceObj.form.id);
		}else{
			formObj = document.forms[sourceObj.form.name];
		}

		//toggling fx
		function amexOff() {
			if (enableHighlight) {
				amex.style.opacity="0.13"; 
				amex.style.filter="alpha(opacity=20)"; 
			}
		}
		function amexOn() {
			amex.style.opacity="1"; 
			amex.style.filter="alpha(opacity=100)"; 
			//set amex to checked
			formObj.amex.checked = true;
		}
	
		function discoverOff() {
			if (enableHighlight) {
				discover.style.opacity="0.13"; 
				discover.style.filter="alpha(opacity=20)"; 
			}
		}
		function discoverOn() {
			discover.style.opacity="1"; 
			discover.style.filter="alpha(opacity=100)"; 
			//set discover to checked
			formObj.discover.checked = true;
		}
	
		function mastercardOff() {
			if (enableHighlight) {
				mastercard.style.opacity="0.13"; 
				mastercard.style.filter="alpha(opacity=20)";
			}
		}
		function mastercardOn() {
			mastercard.style.opacity="1"; 
			mastercard.style.filter="alpha(opacity=100)"; 
			//set mastercard to checked
			formObj.mastercard.checked = true;
		}
	
		function visaOff() {
			if (enableHighlight) {
				visa.style.opacity="0.13"; 
				visa.style.filter="alpha(opacity=20)"; 
			}
		}
		function visaOn() {
			visa.style.opacity="1"; 
			visa.style.filter="alpha(opacity=100)"; 
			//set visa to checked
			formObj.visa.checked = true;
		}
		function electronOff() {
			if (enableHighlight) {
				electron.style.opacity="0.13"; 
				electron.style.filter="alpha(opacity=20)"; 
			}
		}
		function electronOn() {
			electron.style.opacity="1"; 
			electron.style.filter="alpha(opacity=100)"; 
			//set visa to checked
			formObj.visa.checked = true;
		}
		function jcbOff() {
			jcb.style.opacity="0.13";
			jcb.style.filter="alpha(opacity=20)";
		}
		function jcbOn() {
			jcb.style.opacity="1";
			jcb.style.filter="alpha(opacity=100)";
			//set jcb to checked
			formObj.jcb.checked = true;
		}
	
		//begin parsing logic
		if ( sourceObj.value.length > 1 ) {
	
		 switch (true) {
			case ( sourceObj.value.substring(0,1) == '3'): 
				//51 = 3 = Diners (Mastercard) (36) or Amex (34 or 37)  (7=55)
				if (sourceObj.value.substring(1,2) == '6') {
					if (visa) {
						visaOff();
					}
					if (electron) {
						electronOff();
						}
					if (amex) {
						amexOff();
					}
					if (discover) {
						discoverOff();
					}
					if (mastercard) {
						mastercardOn();
					}
					if (jcb) {
						jcbOff();
					}
				} else if ( sourceObj.value.substring(1,2) == '5') {
					if (visa) {
						visaOff();
					}
					if (electron) {
						electronOff();
					}
					if (amex) {
						amexOff();
					}
					if (discover) {
						discoverOff();
					}
					if (mastercard) {
						mastercardOff();
					}
					if (jcb) {
						jcbOn();
					}
				} else {
					if (visa) {
						visaOff();
					}
					if (electron) {
						electronOff();
					}
					if (mastercard) {
						mastercardOff();
					}
					if (discover) { 
						discoverOff();
					}
					if (amex) {
						amexOn();
					}
					if (jcb) {
						jcbOff();
					}
				}
				break;
			
			case (sourceObj.value.substring(0,1) == '4'): 
				//52 = 4 = visa
				if (mastercard) { 
					mastercardOff();
				}
				if (amex) {
					amexOff();
				}
				if (discover) { 
					discoverOff();
				}
				if (visa) {
					visaOn();
				}
					if (electron) {
						electronOn();
					}
					if (jcb) {
						jcbOff();
					}
					
					//check for visa electron
					if (sourceObj.value.length >= 4) {
						for (i=0; i<electronPrefixArray4.length; i++) {
							if (sourceObj.value.substring(0,4) == electronPrefixArray4[i]) {
								isElectron = true;
							}
						}						
						if (sourceObj.value.length >= 6 && !isElectron) {
							for (i=0; i<electronPrefixArray6.length; i++) {
								if (sourceObj.value.substring(0,6) == electronPrefixArray6[i]) {
									isElectron = true;
								}
							}
						}
						if (isElectron) {
							if (electron) {
								electronOn();
							}
							if (visa) {
								visaOff();
							}
						}
						else {
							if (electron) {
								electronOff();
							}
							if (visa) {
								visaOn();
							}
						}
	                        }
				break;
				
			case (sourceObj.value.substring(0,1) == '5'): 
				//53 = 5 = mastercard
				if (visa) { 
					visaOff();
				}
					if (electron) {
						electronOff();
					}
				if (amex) { 
					amexOff();
				}
				if (discover) { 
					discoverOff();
				}
				if (mastercard) { 
					mastercardOn();
				}
                  	     	if (jcb) {
                        	        jcbOff();
	                        }
				break;
			
			case (sourceObj.value.substring(0,1) == '6'): 
				//54 = 6 = discover
				if (visa) { 
					visaOff();
				}
					if (electron) {
						electronOff();
					}
				if (mastercard) { 
					mastercardOff();
				}
				if (amex) { 
					amexOff();
				}
				if (discover) { 
					discoverOn();
				}
                  	     	if (jcb) {
                        	        jcbOff();
	                        }
				break;
		 }
		 //end switch
		}
	//end if
	//else reset the images
		else {
			if( visa ){
				visa.style.opacity="1";
				visa.style.filter="alpha(opacity=100)";
			}
			if (electron) {
				electron.style.opacity="1";
				electron.style.filter="alpha(opacity=100)";
			}
			if( mastercard ){
				mastercard.style.opacity="1";
				mastercard.style.filter="alpha(opacity=100)";
			}
			if( amex ){
				amex.style.opacity="1";
				amex.style.filter="alpha(opacity=100)";
			}
			if( discover ){
				discover.style.opacity="1";
				discover.style.filter="alpha(opacity=100)";
			}
			if (jcb ){
				jcb.style.opacity="1";
				jcb.style.filter="alpha(opacity=100)";
			}
		}
	  //end else
	}
}
//end fx


//resets cc logo opacity on page reload. 
function initialize() {
	var cc_number = document.getElementById("cc_number");
	if (cc_number) { 
		getCC(cc_number);
	}

	var theMonthField = document.getElementById("expdate_month"); 
	if (theMonthField) {
		if (isNaN(theMonthField.value)) {
			theMonthField.style.color="#ccc";
		}
		else {
			theMonthField.style.color="#000";
		}
	}

	var theYearField = document.getElementById("expdate_year"); 
	if (theYearField) {
		if (isNaN(theYearField.value)) {
			theYearField.style.color="#ccc";
		}
		else {
			theYearField.style.color="#000";
		}
	}

}

