////////////////////////////////////////////////////////////////////////////////////
/* GENERAL TEXT */

var gentext_yourname				= "Your Name";
var gentext_yourphone				= "Your Mobile No.";
var gentext_youremail				= "Your Email Address";
var gentext_yourquestion			= "Your Question";
var gentext_title					= "Title";
var gentext_forename				= "Forename";
var gentext_surname					= "Surname";
var gentext_address					= "Address";
var gentext_town					= "Town";
var gentext_postcode				= "Postcode";
var gentext_telephone				= "Mobile No.";
var gentext_email					= "Email";
var gentext_location				= "Location";
var gentext_wherehearaboutus		= "Where did you hear about us?";
var gentext_whencallyou				= "When would you like us to call you?";
var gentext_enterpostcode			= "Please enter your postcode";
var gentext_enterthefollowing		= "Please enter the following:";
var gentext_entervalidmemail		= "Please enter a valid email address";
var gentext_noOptionsEntered        = 'Please select a store from the list';
var gentext_treatment        = 'Please select a treatment from the list';


// Start of variables for the refer a friend
var gentext_refer_a_friend_referrer_email	= "Your Email";
var gentext_refer_a_friend_friends_email	= "Your friends email";
var gentext_refer_a_friend_comment			= "Message to your friend";
// End of variables for the refer a friend


//Start of variables for question and answer page
var gentext_q_fname		= "Forename";
var gentext_q_sname		= "Surname";
var gentext_q_greeting	= "Email Greeting";
var gentext_q_ending	= "Email Ending";
var gentext_q_title		= "Question Title";		
var gentext_q_category	= "Question Category";		
var gentext_question	= "Question";		
var gentext_answer		= "Answer";	
var	gentext_gender		= "Gender";
var gentext_clinic		= "Your nearest Clinic";
var email_confirmation1 = "Do you want to send an email to the person who asked this question?";
var email_confirmation2 = "A reply has already been sent regarding this question. Are you sure you want to send another email?";

//End of variables for question and answer page


////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////



function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function trim(str) {
	return( (""+str).replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') );
}

function checkEmail(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}
	 if (str.indexOf(at,(lat+1))!=-1){
		return false
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
		return false
	 }	
	 if (str.indexOf(" ")!=-1){
		return false
	 }
	 return true					
}

function storeLocatorPuffCheck(form) {
	with (form) {
		if ((trim(postcodeTown.value)).length < 2) { 			
			alert(gentext_enterpostcode);
			postcodeTown.focus();			
		    return false;
        }
	}
}

function storeLocator(postcode) {
	if (postcode == "") 
		var window_url = "http://www.opticalexpress.com/_sites/uk/inserts/uk_store.html";	
	else
		var window_url = "http://www.viamichelin.com/b2b2c/gbr/dyn/controller/DLAmbiguous?withCriteria=false&wnoss=true&productId=66593&strCP="+postcode;	
	var window_name = "storeLocator";		
	var window_options = "toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,top=60,left=30,width=530,height=700";
	if (window.window_name) {			
		if (window_name.closed) {
			window_name = window.open(window_url,window_name,window_options);	
			window_name.focus();
		}
		else 
			window_name.focus();
	}	
	else {
		window_name = window.open(window_url,window_name,window_options);	
		window_name.focus();
	}		
}

function checkStoreLocatorForm() {
    // lets validate first
    isSpecsContacts = document.getElementById('isSpecsContacts');
    isLaser = document.getElementById('isLaser');
    isIOL = document.getElementById('isIOL');
    targ = document.getElementById('postcodeTown');
    
    if(targ.value == '' || (isSpecsContacts.checked == false && isLaser.checked == false && isIOL.checked == false))
    {
        alert(gentext_noOptionsEntered);
        return false;
    } 
    return true;
}

function changeSite() {
	choice = document.countryform.countryselect.value;
	top.location.href = "http://www.opticalexpress.com/" + choice;	
}

function enquiryFormCheck(form) {
	with (form) {
		fields = new Array;			
		labels = new Array;
			
			fields[0] = Title;			labels[0] = gentext_title;
			fields[1] = Forename;		labels[1] = gentext_forename;
			fields[2] = Surname;		labels[2] = gentext_surname;
			fields[3] = Address1;		labels[3] = gentext_address;
			fields[4] = Town;			labels[4] = gentext_town;
			
			fields[5] = Postcode;		labels[5] = gentext_postcode;	
			fields[6] = Telno;			labels[6] = gentext_telephone;	
			fields[7] = Email;			labels[7] = gentext_email;
			fields[8] = Treatment;		labels[8] = gentext_treatment;
			fields[9] = NearestClinic;			labels[9] = gentext_clinic;
			fields[10] = Marketing;		labels[10] = gentext_wherehearaboutus;		
			
		required = "";
		count = 0;					
		for (i=0; i<fields.length; i++) {							
			if (trim(fields[i].value).length < 1) {
				count++;				
				required += "\n- " + labels[i];
				
				if (count == 1)
				{
					first = fields[i];
				}
			}
		}	
		
		
		
							
		if (count == 0) {
			if (checkEmail(Email.value)) 
				return true;
			else {
				alert(gentext_entervalidmemail);
				Email.focus();
			}
		}
		else {							
			alert(gentext_enterthefollowing + required);
			first.focus();
		}	
		return false;
	}
}
		
function callBackCheck(form) {
	with (form) {
		fields = new Array;			
		labels = new Array;				
		fields[0] = callback_name;		labels[0] = gentext_yourname;
		fields[1] = callback_phone;		labels[1] = gentext_yourphone;
		fields[2] = callback_time;		labels[2] = gentext_whencallyou;		
		required = "";
		count = 0;					
		for (i=0; i<fields.length; i++) {							
			if (trim(fields[i].value).length < 1) {
				count++;				
				required += "\n- " + labels[i];
				if (count == 1)
					first = fields[i];
			}
		}						
		if (count == 0) 			
			return true;
		else {							
			alert(gentext_enterthefollowing + required);
			first.focus();
		}	
		return false;
	}
}		

function askQuestionCheck(form) {
	with (form) {
		fields = new Array;			
		labels = new Array;				
		fields[0] = question_name;		labels[0] = gentext_yourname;
		fields[1] = question_email;		labels[1] = gentext_youremail;
		fields[2] = question;			labels[2] = gentext_yourquestion;		
		required = "";
		count = 0;					
		for (i=0; i<fields.length; i++) {							
			if (trim(fields[i].value).length < 1) {
				count++;				
				required += "\n- " + labels[i];
				if (count == 1)
					first = fields[i];
			}
		}						
		if (count == 0) {
			if (checkEmail(question_email.value)) 
				return true;
			else {
				alert(gentext_entervalidmemail);
				question_email.focus();
			}
		}
		else {							
			alert(gentext_enterthefollowing + required);
			first.focus();
		}	
		return false;
	}
}

function send_qanda_confirmation_email(countrycode,qid,question,answer,forename,surname,email,greeting,ending)
{
	var xmlHttp;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
			// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	
	xmlHttp.open("POST","/dc"+countrycode+"_admin/question_and_answer.php",true);
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	
	
	//replace "" and ; with temp characters as they are causing problems with the ajax post call
	//characters are replaced with the original characters in the admin question and answer php file
	var replace_with_this 	= "_and_";
	var answer_formatted 	= answer.replace(/&/g, replace_with_this);
	var question_formatted 	= question.replace(/&/g, replace_with_this);
	var greeting_formatted 	= greeting.replace(/&/g, replace_with_this);
	var ending_formatted 	= ending.replace(/&/g, replace_with_this);
	
	var forename_formatted = forename.value
	var surname_formatted = surname.value 	
	
	forename_formatted 	= forename_formatted.replace(/&/g, replace_with_this);
	surname_formatted 	= surname_formatted.replace(/&/g, replace_with_this);
	
	
	var replace_with_this 	= "_semicolon_";
	answer_formatted 	= answer_formatted.replace(/;/g, replace_with_this);
	question_formatted 	= question_formatted.replace(/;/g, replace_with_this);
	forename_formatted 	= forename_formatted.replace(/;/g, replace_with_this);
	surname_formatted 	= surname_formatted.replace(/;/g, replace_with_this);
	greeting_formatted 	= greeting_formatted.replace(/;/g, replace_with_this);
	ending_formatted 	= ending_formatted.replace(/;/g, replace_with_this);

	var replace_with_this 	= "_space_";
	forename_formatted 	= forename_formatted.replace(/ /g, replace_with_this);
	surname_formatted 	= surname_formatted.replace(/ /g, replace_with_this);

	xmlHttp.send('switchh=EMAIL&amp;qid='+qid.value+'&amp;fname='+forename_formatted+'&amp;sname='+surname_formatted+'&amp;question='+question_formatted+'&amp;answer='+answer_formatted+'&amp;email='+email.value+'&amp;greeting='+greeting_formatted+'&amp;ending='+ending_formatted);
}

function questionFormCheck(form,countrycode,qid,emailsent_prev) {
	with (form) {
			
		if ( qid === undefined ) {
	      qid = "";
	   }
	   if ( emailsent_prev === undefined ) {
	      emailsent_prev = 0;
	   }
	   
	   try
	   {
	   	active_value = active.checked;
	   }
	   catch(err)
	   {
	   	active_value = 'x';
	   }
	   
	
		//fields = new Array;			
		//labels = new Array;	
					
		//fields[0] = forename;			labels[0] = gentext_q_fname;
		//fields[1] = surname;			labels[1] = gentext_q_sname;
		
		required = "";
		
		count = 0;
		counter = 0;			
			
		tinyMCE.init({
			mode : "textareas",
			theme : "advanced"
		});
		
		var ed = tinyMCE.getContent('question');
		var ed2 = tinyMCE.getContent('answer');
		var ed3 = tinyMCE.getContent('email_greeting');
		var ed4 = tinyMCE.getContent('email_ending');
						
		/*for (i=0; i<fields.length; i++) 
		{

			if (trim(fields[i].value).length < 1) {
				count++;				
				required += "\n- " + labels[i];
				if (count == 1)
					first = fields[i];
			}
		}*/
		
		if(active_value == true)
		{
			if(category.value==0)
			{
				count++;				
				required += "\n- " + gentext_q_category;
				if (count == 1)
					first = category;
			}
			
			if(question_title.value.length < 1)
			{
				count++;				
				required += "\n- " + gentext_q_title;
				if (count == 1)
					first = category;
			}
		}
		
		
		if(email.value.length>0)
		{

			if (!checkEmail(email.value)) 
			{
				count++;				
				required += "\n- " + gentext_entervalidmemail;
				if (count == 1)
					first = email;
			}
		}
				
		if(trim(ed).length < 1 || trim(ed) == "<p></p>" )
		{
			required += "\n- " + gentext_question;
			counter++;
		}
		
		if(active_value == true)
		{
			if(trim(ed2).length < 1 || trim(ed2) == "<p></p>" )
			{
				required += "\n- " + gentext_answer;
				counter++;
			}
		}
		
		
		if(trim(email.value).length > 1)
		{
			if(trim(ed).length < 1 || trim(ed) == "<p></p>" )
			{
				required += "\n- " + gentext_question;
				counter++;
			}
			
			if(trim(ed2).length < 1 || trim(ed2) == "<p></p>" )
			{
				if(trim(ed2).length < 1 || trim(ed2) == "<p></p>" )
				{
					required += "\n- " + gentext_answer;
					counter++;
				}
			}
		
			if(trim(ed3).length < 1 || trim(ed3) == "<p></p>" )
			{
				required += "\n- " + gentext_q_greeting;
				counter++;
			}
			
			if(trim(ed4).length < 1 || trim(ed4) == "<p></p>" )
			{
				required += "\n- " + gentext_q_ending;
				counter++;
			}
		}
		
		
				
		if ((count == 0)&&(counter==0)) 
		{
			if(email.value.length>0 && qid.value !="")
			{
				if(confirm(email_confirmation1))
				{
					if(emailsent_prev)
					{
						if(confirm(email_confirmation2))
						{
							 send_qanda_confirmation_email(countrycode,qid,ed,ed2,forename,surname,email,ed3,ed4);
						}
					}
					else
					{
						 send_qanda_confirmation_email(countrycode,qid,ed,ed2,forename,surname,email,ed3,ed4);
					}
				}
			}
				
			return true;
		}
		else {							
			alert(gentext_enterthefollowing + required);
			
			if(count > 1)
			{
				first.focus();
				return false;
			}
			else if(counter > 1)
			{
			
				if(trim(ed).length < 1 || trim(ed) == "<p></p>" )
				{
					tinyMCE.execCommand('mceFocus', false, 'question');
					return false;
				}
				else if(trim(ed2).length < 1 || trim(ed2) == "<p></p>" )
				{
					tinyMCE.execCommand('mceFocus', false, 'answer');
					return false;
				}
				
				else if(trim(email.value).length > 1)
				{
					if(trim(ed3).length < 1 || trim(ed3) == "<p></p>" )
					{
						tinyMCE.execCommand('mceFocus', false, 'email_greeting');
						return false;
					}
					else if(trim(ed4).length < 1 || trim(ed4) == "<p></p>" )
					{
						tinyMCE.execCommand('mceFocus', false, 'email_ending');
						return false;
					}
				}
			
			}
			return false;
		}	
	}
}


function referFriendCheck(form) {
	with (form) {
		fields = new Array;			
		labels = new Array;				
		fields[0] = fromName;	labels[0] = gentext_refer_a_friend_referrer_email;
		fields[1] = toAddress;			labels[1] = gentext_refer_a_friend_friends_email;
		//fields[2] = personalMsg;			labels[2] = gentext_refer_a_friend_comment;		
		required = "";
		count = 0;					
		for (i=0; i<fields.length; i++) {							
			if (trim(fields[i].value).length < 1) {
				count++;				
				required += "\n- " + labels[i];
				if (count == 1)
					first = fields[i];
			}
		}						
		if (count == 0) {
			if (checkEmail(fromName.value)) 
				return true;
			else {
				alert(gentext_entervalidmemail);
				fromName.focus();
			}
			
			if (checkEmail(toAddress.value)) 
				return true;
			else {
				alert(gentext_entervalidmemail);
				toAddress.focus();
			}
			
		}
		else {							
			alert(gentext_enterthefollowing + required);
			first.focus();
		}	
		return false;
	}
}

function moderatedQandACheck(form) {
	with (form) {
		fields = new Array;			
		labels = new Array;				
		fields[0] = Title;				labels[0] = gentext_title;
		fields[1] = question_forename;	labels[1] = gentext_forename;
		fields[2] = question_surname;	labels[2] = gentext_surname;
		fields[3] = question_email;		labels[3] = gentext_email;
		fields[4] = question;			labels[4] = gentext_yourquestion;
		
		required = "";
		count = 0;					
		for (i=0; i<fields.length; i++) {							
			if (trim(fields[i].value).length < 1) {
				count++;				
				required += "\n- " + labels[i];
				if (count == 1)
					first = fields[i];
			}
		}						
		if (count == 0) {
			if (checkEmail(question_email.value)) 
				return true;
			else {
				alert(gentext_entervalidmemail);
				question_email.focus();
			}			
		}
		else {							
			alert(gentext_enterthefollowing + required);
			first.focus();
		}	
		return false;
	}
}

function goto_url(form)
{
	if(form.clinics.value=="")
	{
		alert(gentext_noOptionsEntered);
		return false;
	}
	else
	{
		form.action = form.clinics.value;
	}
}


function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addUnLoadEvent(func) {
// we need this to avoid memory leaks
var oldonunload = window.onunload;
if (typeof window.onunload != 'function') {
  window.onunload = func;
  } else {
    window.onunload = function() {
        oldonunload();
            func();
              }
              }
}

function getDirectionsToStore( postcode )
{
    var start = document.getElementById('start').value;
    window.open("http://maps.google.co.uk/maps?saddr=" + escape(start) + "&daddr=" + postcode);
}

function getDirectionsFromStore( postcode )
{
    var end = document.getElementById('end').value;
    window.open("http://maps.google.co.uk/maps?daddr=" + escape(end) + "&saddr=" + postcode);
}
