// JavaScript Document
/*NOTE: This js file includes only the javascript methods
		that are used in every page of the new design.
		I.E. ONLY ADD methods that are used by the primary
		elements of the new design. */


//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(allElements[i].type=='radio') {
			if(allElements[i].checked==1){
				if(i==0) { url = url + allElements[i].name + "=" + allElements[i].value; }
				else { url = url + "&" + allElements[i].name + "=" + allElements[i].value; }
			}
		} else {
			if(i==0) { url = url + allElements[i].name + "=" + allElements[i].value; }
			else { url = url + "&" + allElements[i].name + "=" + allElements[i].value; }
		}
	}
	return url;
}

//This Method is used for login operations
//It makes the login div to appear in the middle of the page.
function showLoginBox(boxID, contentID,w,h,eff_length){
	var url = "nd/includes_ajax/login.cfm?command=initialize";
	popupDiv(boxID,w,h,eff_length);
	ajax(url, contentID, "fadein", eff_length);
	Effect.ScrollTo('OuterContainerDIV',2);
}

//This Method is used for logout operations
function logout(divID, eff_length){
	var url = "nd/includes_ajax/logout.cfm";
	ajax(url, divID, "fadein", eff_length);
	window.location = 'index.cfm';
}

//Changes "Login" button in TopMenu to "Logout"
//Must be called only after login is successfully done.
function changeLogin(){
	var newString = "<a href='' onclick='javascript: logout(\"menuLogin\",500);return false;'>Logout</a>"
	document.getElementById('menuLogin').innerHTML = newString;
}

//This method makes a div with display=none appear
//with fade-in effect. w is width, and h is height.
//these values are used for centering the div in the window
function popupDiv(id,w,h,eff_length){
	var msgbox = document.getElementById(id); 
	var underlayer = document.getElementById('popupUnderlay'); 
	if(msgbox.style.display!='block'){
		msgbox.style.top = (document.body.clientHeight - h) / 2;  
		msgbox.style.left = (document.body.clientWidth - w) / 2;
		underlayer.style.width=document.body.clientWidth;
		underlayer.style.height=document.body.scrollHeight;
		underlayer.style.display='block';
		setTimeout("Effect.Appear('"+id+"', {duration: "+eff_length+"/1000})",300);
	}
}		

//Closes the div opened by function popupDiv
//If on a page you require to perform addtional Javascript functions
//define them in a function called "Proceed"
function closeDiv(id){
	Effect.DropOut(id);
	document.getElementById('popupUnderlay').style.display='none';
	//Checking to see if Proceed function is defined for the particular page
	//the user is on at the time of login.
	//if Proceed does exist, run it.
	if(typeof(window.Proceed) == 'function') {
		Proceed();
	}
}

//Automatically closes the div opened by function popupDiv
//after the given amount of waitTime.
function autoCloseDiv(id, waitTime){
	setTimeout("closeDiv('"+id+"')",waitTime);
}

//This method is used to open LIVE CHAT popup
function popupLiveChat() {
	var url="http://www.ravand.com/livehelp/UserPreChat.aspx?ref=http%3a%2f%2fwww.ravand.com%2f&d=1&u=&bypass=";
	newwindow=window.open(url,'Support','location=no,menubar=no,toolbar=no,height=325,width=480');
	if (window.focus) {newwindow.focus()}
	return false;
}

//This method is used for input verification purposes for
//the Quick Contact! form in the footer.
function checkFooterFrom(givenForm){
	if(givenForm.name.value == '' || givenForm.email.value == '' || givenForm.message.value == ''){
		if(givenForm.name.value == ''){givenForm.name.className = 'FooterFormInputError';}
		if(givenForm.email.value == ''){givenForm.email.className = 'FooterFormInputError';}
		if(givenForm.message.value == ''){givenForm.message.className = 'FooterFormTextareaError';}
	} else {
		var name= givenForm.name.value;
		var email = givenForm.email.value;
		var phone = givenForm.phone.value;
		var message = givenForm.message.value;
		var url = "http://www.ravand.com/nd/includes/footerFormSent.cfm?name="+name+"&email="+email+"&phone="+phone+"&message="+message;
		ajaxFadeOutIn(url,'footerform', 500);
	}
}

//These functions are used for tables with multiple rows
//They perform mouseover and mouseout actions
//requires the id of the row that is focused,
//and the name of the class that is assigned to the table.
function doFocus(id, className) { document.getElementById(id).className = className+"Focus"; }
function doUnfocus(id, className) { document.getElementById(id).className = className; }
function opentPage(page) { window.location.href=page; }


//Function used in Main Page for Newsletter Subscribers.
function checkEmail(email,divID) {
	var email=document.getElementById('Sub_email').value;
	if(email.search(/@/)==-1) {
		alert("Invalid Email!\nPlease make sure you enter your email address correctly!");
		return false;
	} else if(email.search(/\./)==-1) {
		alert("Invalid Email!\nPlease make sure you enter your email address correctly!");
		return false;
	} else { 
		var url = "http://www.ravand.com/nd/includes_ajax/subscribe_newsletter.cfm?e="+email+"&es="+document.subscribe.es.value; 
		ajaxFadeOutIn(url,divID, 500);
	}
}

//checks ALL input fields of a given form
//highlights all inputs of type=text for a value other than ''
//if input field is empty, it highlights it.
function checkAllInputs(formID) {
	var inputs = document.getElementById(formID).elements;
	var formVerified = true;
	for (i=0;i<inputs.length;i++){
		if(inputs[i].type=='text') {
			if (inputs[i].value=='') {
				inputs[i].style.border = "2px solid #38CB0E";
				inputs[i].style.backgroundColor = "#CFEABB";
				formVerified = false;
			}
		}
	}
	if(formVerified==true){ return true; }
	else{ alert("Please fill out the highlighted input fields.");return false; }
}