// JavaScript Document

function openNewWindow(windowType, category_type){
	if (screen) {
		leftPos = (screen.width - 100)/2;
 		topPos =  (screen.height - 500)/2;
	}
	var url = "";
	switch(windowType){
		case 'Pictures':
					url = 'ShowPicture.php?category='+category_type;
					break;
		default:
					break;
	}
	if(url != ""){	
		var feature = "width=1000,height=600,resizable=yes,scrollbars=yes,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,copyhistory=yes,";
		var position = "left="+leftPos+",top="+topPos;
		feature += position;
		newwindow = window.open(url,category_type,feature);	
		newwindow.focus();
	}
}

function privacyPolicy(){
	if (screen) {
		leftPos = (screen.width - 800)/2;
 		topPos =  (screen.height - 500)/2;
	}
	var url = "privacyPolicy.htm";
	if(url != ""){	
		var feature = "width=600,height=400,resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=yes,menubar=no,copyhistory=no,";
		var position = "left="+leftPos+",top="+topPos;
		feature += position;
		newwindow = window.open(url,"",feature);	
		newwindow.focus();
	}
}


function isEMailAddr(elem) {
    var str = elem.value;
    //var re = /^([\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	//var re = /^.+@.+\..{2,3}$/;
	var re =  /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    if (!str.match(re)) {
        alert("Wrong Email Address Format!\n Format is: user@domain.ext");
        return false;
    } else {
        return true;
    }
}


function checkPictureForm(){
	
	var caption = document.forms['frmAdd_Picture'].elements['txtPictureCaption'];
	var email = document.forms['frmAdd_Picture'].elements['txtUploaderEmail'];
	var fname = document.forms['frmAdd_Picture'].elements['txtUploaderFirstName'];
	var mname = document.forms['frmAdd_Picture'].elements['txtUploaderMiddleName'];
	var lname = document.forms['frmAdd_Picture'].elements['txtUploaderLastName'];
	
	if(email.value == "" || email.value == 0){
		alert("Please Provide Your Email!\nRead our privacy policy regarding your Emails.");
		email.focus();
		return false;
	}
	else{
		var answer = isEMailAddr(email);
		if(!answer){
			email.focus();
			return false;
		}
	}
	if(fname.value == "" || fname.value == 0){
		alert("Please Provide Your First Name!\nRead our privacy policy regarding your Names.");
		fname.focus();
		return false;
	}
	if(mname.value == "" || mname.value == 0){
		alert("Please Provide Your Middle Name!\nRead our privacy policy regarding your Names.");
		mname.focus();
		return false;
	}
	if(lname.value == "" || lname.value == 0){
		alert("Please Provide Your Last Name!\nRead our privacy policy regarding your Names.");
		lname.focus();
		return false;
	}
	if(caption.value == "" || caption.value == 0){
		var ans = confirm("This picture will be uploaded without any captions.\nContinue upload without picture caption?");
		if(!ans)
			caption.focus();
		return(ans);
	}
	return true;
}
/*
function deleteSelected(type, listText, get_ID, category_ID){
	if(type == 'category' || type == 'link'){
		list = document.getElementById(listText);
		if(list.value != 0 || list.value != ""){			
			var url = 'delete_'+type+'.php?action=DELETE THESE '+type.toUpperCase()+'&'+get_ID+'='+list.value;
			if(category_ID != "" || category_ID != 0)
				url += '&category_ID='+category_ID;
			var ans = confirm("Are you sure you want to delete the selected Items?");
			if(ans)
				self.location.replace(url);
		}
		else
			alert("Nothing Selected!");
	}	
}
*/

function hideShow(hide, show){
	hide_me = document.getElementById(hide);
	show_me = document.getElementById(show);
	
			hide_me.style.visibility = 'hidden';
			show_me.style.visibility = 'visible';
			hide_me.style.position = 'absolute';
			show_me.style.position = 'static';
}

function switchListOn(){
	list = document.getElementById('list_on');
	single = document.getElementById('single_on');
	
	hideShow('pic_Single_on', 'pic_List_on');
	
	single.style.visibility = "hidden";
	list.style.visibility = "visible";
	
	single.style.position = "absolute";
	list.style.position = "static";
	
}

function switchSingleOn(){
	list = document.getElementById('list_on');
	single = document.getElementById('single_on');
	
	hideShow('pic_List_on', 'pic_Single_on');
	
	list.style.visibility = "hidden";
	single.style.visibility = "visible";
	
	list.style.position = "absolute";
	single.style.position = "static";
}

function showThisPicture(category, picNum){
	var iframe = document.getElementById('singlePicFrame');
	var url = "http://satellitebeta/works/Olympics/showSinglePic.php?category="+category+"&pic_number="+picNum; 
	
	iframe.src = url;
	
	switchSingleOn();
}

function popwin(url){
	if (screen) {
		leftPos = (screen.width - 730)/2;
 		topPos =  (screen.height - 650)/2;
	}
	var feature = "width=700,height=600,resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=yes,menubar=no,copyhistory=no,";
	var position = "left="+leftPos+",top="+topPos;
	feature += position;
	newwindow = window.open(url,"Information",feature);	
	newwindow.focus();
}

function checkSearch(frmname){
	if(document.forms[frmname].elements['txtSearchKey'].value == "" || document.forms[frmname].elements['txtSearchKey'].value == 0){
		alert('Please Provide a word or text to be searched!');
		document.forms[frmname].elements['txtSearchKey'].focus();
		return false
	}
	return true;
}

function selectTarget(frmname, option){
	var Form = document.getElementById(frmname);
	var url = "";
	switch(option){
		case 'Categories':
			url = 'processSearch.php';
			break;
		case 'Pictures':
			url = 'pictureSearch.php';
			break;
		case 'Complaints and Compliments':
			url = 'query.php';
			break;
		case 'Experiences':
			url = 'query.php';
			break;
		default:
			url = 'processSearch.php';
			break;
			
	}
	Form.action = url;
}
function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) 
{
  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {
    highlightStartTag = "<font style='font-weight:bold; color:#0000FF;'>";
    highlightEndTag = "</font>";
  }
  
  // find all occurences of the search term in the given text,
  // and add some "highlight" tags to them (we're not using a
  // regular expression search, because we want to filter out
  // matches that occur within HTML tags and script blocks, so
  // we have to do a little extra validation)
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();
    
  while (bodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {
      // skip anything inside an HTML tag
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        // skip anything inside a <script> block
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
          newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
          bodyText = bodyText.substr(i + searchTerm.length);
          lcBodyText = bodyText.toLowerCase();
          i = -1;
        }
      }
    }
  }
  
  return newText;
}


/*
 * This is sort of a wrapper function to the doHighlight function.
 * It takes the searchText that you pass, optionally splits it into
 * separate words, and transforms the text on the current web page.
 * Only the "searchText" parameter is required; all other parameters
 * are optional and can be omitted.
 */
function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
{
  // if the treatAsPhrase parameter is true, then we should search for 
  // the entire phrase that was entered; otherwise, we will split the
  // search string so that each word is searched for and highlighted
  // individually
    searchArray = searchText.split(" ");
  
  
  var bodyText = document.getElementById('SearchResults').innerHTML;
  for (var i = 0; i < searchArray.length; i++) {
    bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
  }
  
  document.getElementById('SearchResults').innerHTML = bodyText;
  return true;
}

function checkEmailForm(lang){
	var msg;
	switch(lang){
		case 'en':
				if(document.email_form.full_name.value == 0){
				   alert("Enter Name!");
				   document.email_form.full_name.focus();
				   return false;
				   }
				if(document.email_form.email.value == 0 || isEMailAddr(document.email_form.email, lang)== false){
					alert("Provide Your Email!");
					document.email_form.email.focus();
					return false;
					}
				if(document.email_form.subject.value == 0){
					alert("Subject?");
					document.email_form.subject.focus();
					return false;
				   }
			break;
		case 'cn':
				if(document.email_form.full_name.value == 0){
				   alert("名字!");
				   document.email_form.full_name.focus();
				   return false;
				   }
				if(document.email_form.email.value == 0 || isEMailAddr(document.email_form.email, lang)== false){
					alert("电子邮件!");
					document.email_form.email.focus();
					return false;
					}
				if(document.email_form.subject.value == 0){
					alert("主题?");
					document.email_form.subject.focus();
					return false;
				   }
			break;
	}
	updateRTEs();
	
}
function openEmail(lang){
	var url = "mail/contact_us"+lang+".php";
	if (screen) {
		leftPos = (screen.width - 700)/2;
		topPos =  (screen.height - 600)/2;
	}
	if(url != ""){
		var feature = 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=no,width=620px,height=520px,left='+leftPos+',top='+topPos;
		var newwindow = window.open(url,"Contact Us",feature);	
		newwindow.focus();
	}
	return false;
}

/*
function bookmarkPageFunction(title,url){
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, url);
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
	else if(document.all)// ie
		window.external.AddFavorite(url, title);
}



function bookmarkSites(){
	var TITLE = Array('All About Golf', 'All About Olympics', 'All About Airlines', 'All About World-Expo');
	var URL = Array('http://www.world-golf-courses.com', 'http://www.olympic-paralympic-games.com', 'http://www.airways-airlines-airports.com','http://www.the-world-expo.com');
	var linkTotal = URL.length;
	var ictr = 0;
	
	for(; ictr < linkTotal; ictr++)
		{
			bookmarkPageFunction(TITLE[ictr], URL[ictr]);
		}
}*/