var preObj;

function focusIN(obj){
	if(preObj){	focusOUT(preObj); }
	MM_changeProp(obj,'','style.border','2px solid #5780E0','INPUT/TEXT');
	preObj=obj;
}
function focusOUT(obj){
	MM_changeProp(obj,'','style.border','2px solid #CCCCCC','INPUT/TEXT');
	preObj=false;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}

function checkStep1(){
	var errors='';
	var merchant_name = MM_findObj('merchant_name');
	var display_name = MM_findObj('display_name');
	var address1 = MM_findObj('address1');
	var contact_name = MM_findObj('contact_name');
	var contact_email = MM_findObj('contact_email');
	var city = MM_findObj('city');
	var state = MM_findObj('state');
	var zip = MM_findObj('zip');
	var phone = MM_findObj('phone');
	var website = MM_findObj('website');
	var chkagreement = MM_findObj('chkagreement');
	var chkRateCard = MM_findObj('chkRateCard');
	if(!merchant_name.value){
		MM_changeProp('merchant_name','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Company name is required\n";
	}
	if(!display_name.value){
		MM_changeProp('display_name','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Display name is required\n";
	}
	if(!address1.value){
		MM_changeProp('address1','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Address on line 1 is required\n";
	}
	if(!contact_name.value){
		MM_changeProp('contact_name','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Contact name is required\n";
	}
	if(!contact_email.value){
		MM_changeProp('contact_email','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Contact email is required\n";
	}
	if(!city.value){
		MM_changeProp('city','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="City is required\n";
	}
	if(!state.value){
		MM_changeProp('state','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="State is required\n";
	}
	if(!zip.value){
		MM_changeProp('zip','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Zip Code is required\n";
	}
	if(!phone.value){
		MM_changeProp('phone','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Phone is required, formatted xxx-xxx-xxxx\n";
	}
	if(!website.value){
		MM_changeProp('website','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Website URL is required\n";
	}
	if(!chkagreement.checked){
		errors +="Please read and accept the Merchant Agreement\n";
	}
	if(!chkRateCard.checked){
		errors +="Please read and accept the Rate Card\n";
	}
	
	if (errors) 
	alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function checkStep2(){
	var errors='';
	var expMonth = MM_findObj('expMonth');
	var expYear = MM_findObj('expYear');
	var creditCardType = MM_findObj('creditCardType');
	var x_card_num = MM_findObj('x_card_num');
	var x_first_name = MM_findObj('x_first_name');
	var x_last_name = MM_findObj('x_last_name');
	var x_address1 = MM_findObj('x_address1');
	var x_city = MM_findObj('x_city');
	var x_state = MM_findObj('x_state');
	var x_zip = MM_findObj('x_zip');
	var chkAuthorize = MM_findObj('chkAuthorize');
	var x_amount =MM_findObj('x_amount');
	
	if(!creditCardType.value){
		MM_changeProp('creditCardType','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Credit card type is required\n";
	}
	if(!x_card_num.value || !isValidCardNumber(x_card_num.value)){
			MM_changeProp('x_card_num','','style.border','2px solid #FF3300','INPUT/TEXT');
			errors +="Please enter a valid credit card number, formatted xxxx-xxxx-xxxx-xxxx or xxxxxxxxxxxxxxxx\n";
	}
	if(!x_first_name.value){
		MM_changeProp('x_first_name','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="First name is required\n";
	}
	if(!x_last_name.value){
		MM_changeProp('x_last_name','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Last name is required\n";
	}
	if(!expMonth.value){
		MM_changeProp('expMonth','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Expiration month is required\n";
	}
	if(!expYear.value){
		MM_changeProp('expYear','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Expiration year is required\n";
	}
	if(!x_address1.value){
		MM_changeProp('x_address1','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Billing address on line 1 is required\n";
	}
	if(!x_city.value){
		MM_changeProp('x_city','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Billing city is required\n";
	}
	if(!x_state.value){
		MM_changeProp('x_state','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Billing state is required\n";
	}
	if(!x_zip.value){
		MM_changeProp('x_zip','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Billing zip code is required\n";
	}
	if(!chkAuthorize.checked){
		errors +="Please Check the authorize payment box to proceed\n";
	}
	if(x_amount.value < 250){
		errors +="Initial Amount is a minimum of $250.00\n";
		MM_changeProp('x_amount','','value','250.00','INPUT/TEXT');
	}
	
	if (errors) 
	alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function checkStep4(){
	var errors='';
	var username = MM_findObj('username');
	var password = MM_findObj('password');
	if(!username.value){
		MM_changeProp('username','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="User Name is required\n";
	}
	if(!password.value){
		MM_changeProp('password','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Password is required\n";
	}
	if (errors) 
	alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}


function checkAmount(){
	var errors='';
	var chkAuthorize = MM_findObj('chkAuthorize');
	var x_amount =MM_findObj('x_amount');
	
	if(!chkAuthorize.checked){
		errors +="Please Check the authorize payment box to proceed\n";
	}
	if(x_amount.value < 250){
		errors +="Initial Amount is a minimum of $250.00\n";
		MM_changeProp('x_amount','','value','250.00','INPUT/TEXT');
	}
	
	if (errors) 
	alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function checkUpBillInfo(){
	var errors='';
	var expMonth = MM_findObj('expMonth');
	var expYear = MM_findObj('expYear');
	var creditCardType = MM_findObj('creditCardType');
	var x_card_num = MM_findObj('x_card_num');
	var x_card_code = MM_findObj('x_card_code');
	var x_first_name = MM_findObj('x_first_name');
	var x_last_name = MM_findObj('x_last_name');
	var x_address1 = MM_findObj('x_address1');
	var x_city = MM_findObj('x_city');
	var x_state = MM_findObj('x_state');
	var x_zip = MM_findObj('x_zip');

	if(!creditCardType.value){
		MM_changeProp('creditCardType','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Credit card type is required\n";
	}
	if(!x_card_num.value || !isValidCardNumber(x_card_num.value)){
			MM_changeProp('x_card_num','','style.border','2px solid #FF3300','INPUT/TEXT');
			errors +="Please enter a valid credit card number, formatted xxxx-xxxx-xxxx-xxxx or xxxxxxxxxxxxxxxx\n";
	}
	if(!x_first_name.value){
		MM_changeProp('x_first_name','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="First name is required\n";
	}
	if(!x_last_name.value){
		MM_changeProp('x_last_name','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Last name is required\n";
	}
	if(!expMonth.value){
		MM_changeProp('expMonth','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Expiration month is required\n";
	}
	if(!expYear.value){
		MM_changeProp('expYear','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Expiration year is required\n";
	}
	if(!x_card_code.value){
		MM_changeProp('x_card_code','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="CVS is required\n";
	}
	if(!x_address1.value){
		MM_changeProp('x_address1','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Street address is required\n";
	}
	if(!x_city.value){
		MM_changeProp('x_city','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Billing city is required\n";
	}
	if(!x_state.value){
		MM_changeProp('x_state','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Billing state is required\n";
	}
	if(!x_zip.value){
		MM_changeProp('x_zip','','style.border','2px solid #FF3300','INPUT/TEXT');
		errors +="Billing zip code is required\n";
	}
		
	if (errors) 
	alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function trimMyString(s) {
	s = s.replace( /-^\s+/g, "" );
	return s.replace( /\s+$/g, "" );
}

function isValidCardNumber(strNum) {
   var nCheck = 0;
   var nDigit = 0;
   var bEven  = false;
   
   for (n = strNum.length - 1; n >= 0; n--) {
      var cDigit = strNum.charAt (n);
      if (isDigit (cDigit)) {
         var nDigit = parseInt(cDigit, 10);
         if (bEven) {
            if ((nDigit *= 2) > 9)
               nDigit -= 9;
         }
         nCheck += nDigit;
         bEven = ! bEven;
      }
      else if (cDigit != ' ' && cDigit != '.' && cDigit != '-') {
         return false;
      }
   }
   return (nCheck % 10) == 0;
}

function isDigit(c) {
   var strAllowed = "1234567890";
   return (strAllowed.indexOf (c) != -1);
}

function isCardTypeCorrect (strNum, type) {
   var nLen = 0;
   for (n = 0; n < strNum.length; n++) {
      if (isDigit (strNum.substring (n,n+1)))
         ++nLen;
   }
   
   if (type == 'Visa')
      return ((strNum.substring(0,1) == '4') && (nLen == 13 || nLen == 16));
   else if (type == 'Amex')
      return ((strNum.substring(0,2) == '34' || strNum.substring(0,2) == '37') && (nLen == 15));
   else if (type == 'Master Card')
      return ((strNum.substring(0,2) == '51' || strNum.substring(0,2) == '52'
              || strNum.substring(0,2) == '53' || strNum.substring(0,2) == '54'
              || strNum.substring(0,2) == '55') && (nLen == 16));
   else
      return false;  
}

function window_open(Width, Height, URL){
		var opt1 = "toolbar=0,directories=0,status=1,menubar=0,scrollbars=1,resizeable=1,width="+Width+",height="+Height;
		var win1 = window.open("", "Pricefish", opt1);
		var win2 = window.open(URL, "Pricefish");
}