// js/support.js

// labelID, inputID, inputType, errorCode
var loginFormData = [
      ['emailLoginLbl', 'emailLoginField', 'emailLoginImg', 'required', 'errorRegister', 'errorRegister'],
      ['emailPasswordLbl', 'emailPasswordField', 'emailPasswordImg', 'required', 'errorRegister', 'errorRegister']
];

// labelID, inputID, inputType, errorCode
var contactFormData = [
      ['contactName', 'txtName', 'contactNameImg', 'required', 'errorRegister', 'errorContact'],
      ['contactEmail', 'txtEmail', 'contactemail_img', 'email', 'errorRegister', 'errorContact'],
      ['contactMessage', 'txtMessage', 'contactMessageImg', 'required', 'errorRegister', 'errorContact']
];


var isCard = false;


function submitRegForm() {
	// labelID, inputID, inputType, errorCode
	var registerFormData = [
		  ['first_name_lbl', 'first_name', 'first_name_img', 'required', 'errorRegister', 'errorRegister'],
		  ['last_name_lbl', 'last_name', 'last_name_img', 'required', 'errorRegister', 'errorRegister'],
		  ['email_lbl', 'email', 'email_img', 'email', 'errorRegister', 'errorRegister'],
		  ['password_lbl', 'password', 'password_img', 'required', 'errorRegister', 'errorRegister'],
		  ['password2_lbl', 'password2', 'password2_img', 'required', 'errorRegister', 'errorRegister'],
		  ['company_lbl', 'company', 'company_img', 'required', 'errorRegister', 'errorRegister'],
		  ['address_lbl', 'address', 'address_img', 'required', 'errorRegister', 'errorRegister'],
		  ['city_lbl', 'city', 'city_img', 'required', 'errorRegister', 'errorRegister'],
		  ['zip_lbl', 'zip', 'zip_img', 'required', 'errorRegister', 'errorRegister']
		  //,['cardnumberLbl', 'cardnumber', 'cardnumberImg', '', 'errorRegister', 'errorRegister']
	];
	
	if(validateForm(registerFormData)) {
		if ($('password').value == $('password2').value) {
			$('errorRegister').update();
			//checkCardNumber();
			checkEmail();
		}else{
			elmInput = 'password';
	    elmLabel = 'password_lbl';
	    elmImg = 'password_img';
	    
	    setInput(elmInput,1);
	    setLabel(elmLabel,1);
	    setIcon(elmImg,1);
	    
	    elmInput = 'password2';
	    elmLabel = 'password2_lbl';
	    elmImg = 'password2_img';
	    
	    setInput(elmInput,1);
	    setLabel(elmLabel,1);
	    setIcon(elmImg,1);
	    
	    $('errorRegister').addClassName('errorLabel').update('Passwords must match.');
		}
	}
}

function checkCardNumber() {
	var sn = $('cardnumber').value;
	params = 'sn='+sn;
	new Ajax.Request(
		'/support/ajaxCheckCardNumber/', {
			method: 'get',
			parameters: params,
			onSuccess: checkCardHandler,
			onFailure: function() {
				alert("There was an error with the connection(1)");
			}
		}
	);
}

function checkCardHandler(transport) {
	var result = transport.responseText.evalJSON();
	if(result.rcode == 0){
		elmInput = 'cardnumber';
		elmLabel = 'cardnumberLbl';
		elmImg = 'cardnumberImg';
		
		setInput(elmInput,1);
		setLabel(elmLabel,1);
		setIcon(elmImg,1);
		
		$('errorRegister').addClassName('errorLabel').update('Card Serial Number is Invalid.');
		
	} else {
		elmInput = 'cardnumber';
		elmLabel = 'cardnumberLbl';
		elmImg = 'cardnumberImg';
		
		setInput(elmInput,0);
		setLabel(elmLabel,0);
		setIcon(elmImg,0);
		
		checkEmail();
	}
}

function checkEmail() {
	var email = $('email').value
	params = 'email='+encodeURIComponent(email);
	new Ajax.Request(
		'/support/ajaxCheckEmail/', {
			method: 'get',
			parameters: params,
			onSuccess: checkEmailHandler,
			onFailure: function() {
				alert("There was an error with the connection(2)");
			}
		}
  	);
}
function checkEmailHandler(transport) {
	var result = transport.responseText.evalJSON();
	if(result.rcode == 0){
		checkPassword();
		//registerUser();
	} else {
		elmInput = 'email';
		elmLabel = 'email_lbl';
		elmImg = 'email_img';
		
		setInput(elmInput,1);
		setLabel(elmLabel,1);
		setIcon(elmImg,1);
		
		$('errorRegister').addClassName('errorLabel').update('Email Address already in use.');
	}
}

function checkPassword(){
	var email = $('email').value
	var pass = $('password').value
	params = 'email='+encodeURIComponent(email)+'&pass='+pass;
	new Ajax.Request(
		'/support/ajaxCheckPass/', {
			method: 'get',
			parameters: params,
			onSuccess: checkPasswordHandler,
			onFailure: function() {
				alert("There was an error with the connection(3)");
			}
		}
	);
}
function checkPasswordHandler(transport){
	var result = transport.responseText.evalJSON();
	if(result.rcode == 1){
		registerUser();
	} else {
		elmInput = 'password';
		elmLabel = 'password_lbl';
		elmImg = 'password_img';
		
		setInput(elmInput,1);
		setLabel(elmLabel,1);
		setIcon(elmImg,1);
		
		$('errorRegister').addClassName('errorLabel').update(result.rmsg);
	}
}

function registerUser() {
  new Ajax.Request(
	'/register/', {
		method: 'post',
		parameters: $('registerForm').serialize(true),
		onSuccess: registerHandler,
		onFailure: function() {
			alert("There was an error with the connection(4)");
		}
  	}
  );
}

function registerHandler(transport) {
	var result = transport.responseText.evalJSON();
	var content;
	
	if(result.rcode > 0){
	      content = '<div style="width:600px; text-align:center"><span style="font-family:Arial">';
		content += '<strong>Registration Success!</strong><br />Thank You, Your account has been successfully created. An email has been sent to your email account with the account details.<br /><br >';
		content += '</span></div>';
	}else{
	      content = '<div style="width:600px; text-align:center"><span style="font-family:Arial">';
		content += '<strong>'+result.rmsg+'</strong><br /><br ><a href="/register/"><input type="button" value="Try Again"></a>';
		content += '</span></div>';
	}
	
	var elmWin = $('winContainer');
	elmWin.innerHTML = content;
	//Modalbox.resize('-100','-340');
}

function passwordReminder() {
	
	if($('prEmail').value.length == 0){
		$('errorContainer').update('<span style="font-family:Arial; color:#990000; font-weight:bold;">Email address required</span>');
		return false;
	}
	
	var params = 'email=' + $('prEmail').value;
	new Ajax.Request(
		'/forgot/', {
			method: 'post',
			parameters: params,
			onSuccess: passReminderHandler,
			onFailure: function() {
				alert("There was an error with the connection(4)");
			}
		}
	);
}

function passReminderHandler(transport) {
	var result = transport.responseText.evalJSON();
	var content;
	var elmWin;
	console.log(result.rcode);
	if(result.rcode == 0){
		elmWin = $('errorContainer');
		content = '<div style="width:200px; height:20px"><span style="font-family:Arial; color:#990000">';
		content += '<strong>Email Address Not Found!</strong>';
		content += '</span></div>';
	} else {
		elmWin = $('winContainer');
		content = '<div style="width:470px; text-align:center"><span style="font-family:Arial">';
		content += '<strong>Password Sent!</strong><br />Thank You, Your password has been sent to the email address associated with the password. ';
		content += '</span></div>';
	}
	
	elmWin.innerHTML = content;
}

function initLoad(){
	switch(window.location.hash){
  	case '#register':
  		//Modalbox.show('/register/', {title: 'Register An Account', width: 655, height: 550});
  		window.location.href = '/register/';
  	break;
  	case '#forgot_pass':
  		//Modalbox.show('/forgot/', {title: 'Password Reminder', width: 500, height: 190});
  		window.location.href = '/forgot/';
  	break;
  }
}

