// JavaScript Document
/*General Guide:
  In order for these methods to work, you must follow these rules:
  1-all inputs must have an initial value=""
  2-inputs id must start from 1 and not 0.
  3-inputs must have the id=input#.. eg. id=input1, id=input2
  */

//Pops up the login box, but with "forgot password" page
//It makes the login div to appear in the middle of the page.
function showForgotPassBox(boxID, contentID,w,h,eff_length){
	var url = "includes_ajax/login.cfm?command=forgotpassword";
	popupDiv(boxID,w,h,eff_length);
	ajax(url, contentID, "fadein", eff_length);
}

//highlights the input field that is focused
function setFocus(id_num){
	for(i=1; document.getElementById('fc'+i)!=null;i++){
		if(i==id_num){highlight(i,'Focus');}//Focus must start with CAPITAL letter
		else {if(document.getElementById('fc'+i).className!='fieldContainerError'){highlight(i, '');}}
	}
}
//un-highlight all inputs (used when submit is clicked)
function setUnfocus(){
	for(i=1; document.getElementById('fc'+i)!=null;i++){highlight(i, '');}
}

//Checks if the content has the general syntax of an email.
//The input data must contain at least an @ sign and a dot (.). 
//Also, the @ must not be the first character of the email address, 
//and the last dot must at least be one character after the @ sign.
function validate_email(email_id){
	var input=document.getElementById('input'+email_id);
	apostion=input.value.indexOf("@");
	dotposition=input.value.lastIndexOf(".");
	if (apostion<1||dotposition-apostion<2){highlight(email_id,'Error'); return false;}
	else {return true;}
}

//Checks if password is at least 6 characters long
//Also checks for password re-type to be the same as password
function validate_pass(pass_id) {
	pass_id = parseInt(pass_id);
	var input = document.getElementById('input'+pass_id);
	if(input.name=='password'){ 
		var input_retype = document.getElementById('input'+(pass_id+1));
		if(input.value.length<6){highlight(pass_id,'Error'); return false;} 
		if(input_retype.value!=""){
			if(input.value!=input_retype.value){highlight(pass_id,'Error');highlight(pass_id+1,'Error'); return false;}
		}
	}
	else if(input.name=='password_retype'){
		if(input.value!=document.getElementById('input'+(pass_id-1)).value){
			highlight(pass_id,'Error');
			highlight(pass_id-1,'Error');
			document.getElementById('fih'+pass_id).innerHTML = "Passwords must match!";
		}
	}
	return true;
}

//Validates phone number
//The only acceptable format is xxx-xxx-xxxx
function validate_phone(phone_id){
	var input = document.getElementById('input'+phone_id);
	if(input.value.length!=12) { highlight(phone_id,'Error');return false; }
	for(i=0;i<input.value.length; i++) {
		if(i==3 || i==7) { if(input.value.charAt(i)!='-'){highlight(phone_id,'Error'); return false;} }
		else if(isNaN(input.value.charAt(i))){highlight(phone_id,'Error');return false;}
	}
	return true;
}

//Highlights a field accoding to the case provided
//requires the number that goes in the id of the element
//and the classNameCase that will be added to the end of the class name.
//NOTE: classNameCase must start with a CAPITAL LETTER, eg. Focus not focus.
function highlight(id_num,classNameCase){
	document.getElementById('fc'+id_num).className="fieldContainer"+classNameCase;
	if(document.getElementById('fn'+id_num)!=null){
		document.getElementById('fn'+id_num).className='fieldName'+classNameCase;
	}
	if(document.getElementById('fif'+id_num)!=null){
		document.getElementById('fif'+id_num).className="fieldInputField"+classNameCase;
	}
}

//Returns the actual element number of the input
//This works based on input ids which are id=input#
function getInputNum(element){
	for(i=0;;i++){
		if(element.id==('input'+i)) { return i; }
	}
}

//Retreives the form using form_id, and checks for validation
//only for inputs with type=text. if value=="" highlights them
//Note: inputs must have ids in the form of input#. eg. id=input1, id=input2...
//This method must be called only in onsubmit of <form> tag.
function validate_inputs(form_id){
	//var allInputs = document.getElementById(form_id).getElementsByTagName('input');
	var allInputs = document.getElementById(form_id).elements;
	var inputs = new Array();
	for(j=0, k=0;j<allInputs.length;j++){
		if(allInputs[j].name=='email'){var email_id=j+1;}
		if(pass_id==null && allInputs[j].type=='password'){var pass_id=j+1;}
		if(allInputs[j].name=='phone'){var phone_id=getInputNum(allInputs[j]);}
		if(allInputs[j].type=='checkbox' && allInputs[j].name=='i_agree'){
			if(!allInputs[j].checked){var iagree=false; iagree_id=allInputs[j].title;} else {var iagree=true;}
		}
		if(allInputs[j].type=='text' || allInputs[j].type=='password'){inputs[k]=allInputs[j].id;k++;}
	}
	var foundError = false; 
	for(i=0;i<inputs.length;i++){ 
		if(document.getElementById(inputs[i]).value==''){
			highlight(parseInt(inputs[i].replace("input","")),'Error');
			foundError = true;
		}
	}
	if(email_id!=null){ if(!validate_email(email_id)){highlight(email_id,'Error'); foundError=true;} }
	if(pass_id!=null){ if(!validate_pass(pass_id)){highlight(pass_id,'Error'); foundError=true;} }
	if(phone_id!=null){ if(!validate_phone(phone_id)){highlight(phone_id,'Error'); foundError=true;} }
	if(iagree!=null){ if(!iagree){
		document.getElementById('fih'+iagree_id).innerHTML = "Please read and agree with Ravand terms of service buy checking off this field.";highlight(iagree_id,'Error'); foundError=true;} }
	
	if (foundError) {Effect.ScrollTo('newCustomerDiv');return false;}
	else {return true;}
}


//Creates and returns to the AJAX caller a url string 
//based on all the elements in the givenForm
//Requires the form itself, and the pageLink to which append the 
//generated string of all form variables.
function formToString(givenForm, pageLink) {
	var url = pageLink + "?"; 
	var allElements = givenForm.elements;
	for(var i = 0; i < allElements.length; i++) {
		if(i==0) { url = url + allElements[i].name + "=" + allElements[i].value; }
		else { url = url + "&" + allElements[i].name + "=" + allElements[i].value; }
	}
	return url;
}
