function randomPassword(length)
{
  chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  pass = "";
  for(x=0;x<length;x++)
  {
    i = Math.floor(Math.random() * 62);
    pass += chars.charAt(i);
  }
  return pass;
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
}


function ColorSearch(color) {
  ReplaceInputfieldValue(opener.document.FormSearch.SearchInput, color);
  opener.document.forms['FormSearch'].submit();
  opener.focus();
}

function autoCompOff(id) {
    document.getElementById('SearchInput').setAttribute("autocomplete","off");
}


//returns a random 8 digit alphanumeric string
function GetRandomString() {
  var intLength = 8;
  var strCharList = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
  var strRandom = "";
  
  
  for (i=1; i<=intLength; i++) {
    strRandom = strRandom + strCharList.substr(Math.round(Math.random() * strCharList.length +1), 1);
  }
  
  return strRandom;
}


//submit form and disable submit button
function FormSubmit(target) {
  var objButton = document.getElementsByName('Submit');
  objButton.disabled = true;
  return true;
}


function ChatStartTimer() {
  setTimeout("ChatCheckRefresh()",5000)
}

function ChatCheckRefresh() {
  if(document.ChatForm.Message.value == '') {
   document.location.href = document.location.href;
  }
  setTimeout("ChatCheckRefresh()",5000)
}

function KeywordLoadAssignKeywords() {
  parent.opener.document.Form.Keywords.value = document.Form.Keywords.value;
  parent.opener.document.Form.ConceptKeywords.value = document.Form.ConceptKeywords.value;
  parent.opener.document.Form.TechnicalKeywords.value = document.Form.TechnicalKeywords.value;
  
  parent.opener.focus();
}



function KeywordassistentAddMoreKeywords(strFormAction) {
	document.Form.action = strFormAction;
	document.Form.submit();
	
	return true;
}

function KeywordassistentPassOverKeywords() {
  objKeywordfield = parent.opener.document.Form.Keywords
  
  strCurrentKeywords = trim(objKeywordfield.value);
  strNewKeywords = trim(document.Form.Keywords.value);
  
  if (isEmpty(strCurrentKeywords)) {
  	strCurrentKeywords = strNewKeywords;
  } else {
  	strCurrentKeywords = strCurrentKeywords + ', ' + strNewKeywords;
  }
  
	objKeywordfield.value = strCurrentKeywords;	
	parent.opener.focus();
}

function PassOver(objFormField, strValue, bolSetFocus) {
  objFormField.value = strValue
	if (bolSetFocus) parent.opener.focus();
}



function ShowUploadStatus(intUploadID) {
  window.open('progressbar.asp?ID=' + intUploadID, 'win' + intUploadID, 'width=500,height=140');
  return true;
}

function PreviewImg(el) {
	var filename = el.value;		
	var Img = new Image();
	
	Img.src = filename;
	Img.onload = function() {
	  if(this.width >= this.height) {
  	  this.height *= 164 / this.width;
  	  this.width = 164;
  	}else{
  	  this.width *= 164 / this.height;
  	  this.height = 164;
    }
    
  	document.PhotoPreview.src = this.src;
  	document.PhotoPreview.height = this.height;
  	document.PhotoPreview.width = this.width;
	}
	
  Img = null;
}

function ShowUploadPreview() {
  var ie = (navigator.userAgent.indexOf('MSIE') > -1) ? 1 : 0;
  
  if(ie) {
    var myElem = document.getElementById("UploadPreview");
    myElem.style.display = 'block'
    
    var myElem = document.getElementById("UploadPreviewForm");
    myElem.className = 'boxSubcolumnRight'
  }
}


function OpenExifViewer(strURL) {
  winExifViewer = window.open(strURL, 'winExifViewer', 'location=yes,status=yes,toolbar=yes,menubar=yes,scrollbars=yes,width=400,height=530');
  winExifViewer.focus();
}

function OpenVoteViewer(strURL) {
  winExifViewer = window.open(strURL, 'winVoteViewer', 'location=yes,status=yes,toolbar=yes,menubar=yes,scrollbars=yes,width=400,height=530');
  winExifViewer.focus();
}

function OpenSmilies() {
  winExifViewer = window.open('smilies.asp', 'winExifViewer', 'location=yes,status=yes,toolbar=yes,menubar=yes,scrollbars=yes,width=200,height=300');
  winExifViewer.focus();
}

function OpenFileUpload(strURL) {
  winFileUpload = window.open(strURL, 'winFileUpload', 'location=yes,status=yes,toolbar=yes,menubar=yes,scrollbars=yes,width=320,height=240');
  winFileUpload.focus();
}

function OpenLightbox(strURL) {
  winLightbox = window.open(strURL, 'winLightbox', 'location=yes,resizable=yes,status=yes,toolbar=yes,menubar=yes,scrollbars=yes,width=640,height=530');
  winLightbox.focus();
}

function OpenChat(strURL, strWindowname) {
  winChat = window.open(strURL, strWindowname, 'location=yes,resizable=yes,status=yes,toolbar=yes,menubar=yes,scrollbars=yes,width=640,height=530');
  winChat.focus();
}

function OpenKeywordAssistent(strURL) {
  winKA = window.open(strURL, 'winKA', 'location=no,status=yes,toolbar=no,menubar=no,scrollbars=yes,width=640,height=530');
  winKA.focus();
}

function OpenInputAssistent(strURL, objInput) {
  IASetFocus(objInput);
  winIA = window.open(strURL, 'winIA', 'location=no,status=yes,toolbar=no,menubar=no,scrollbars=yes,width=370,height=500');
  winIA.focus();
}

function OpenKeywordViewer(strURL) {
  winKV = window.open(strURL, 'winKV', 'location=yes,status=yes,toolbar=yes,menubar=yes,scrollbars=yes,width=400,height=530');
  winKV.focus();
}

function OpenKeywordLoader(strURL) {
  winKL = window.open(strURL, 'winKL', 'location=yes,status=yes,toolbar=yes,menubar=yes,scrollbars=yes,width=640,height=530');
  winKL.focus();
}


function OpenHelp(strURL) {
  winHelp = window.open(strURL, 'winHelp', 'location=no,resizable=yes,status=yes,toolbar=no,menubar=no,scrollbars=yes,width=640,height=530');
  winHelp.focus();
}

function OpenVoteWindow(strURL) {
  winVote = window.open(strURL, 'winVote', 'location=no,status=yes,toolbar=no,menubar=no,scrollbars=yes,width=640,height=530');
  winVote.focus();
}

function OpenUserselect(strURL) {
  winKV = window.open(strURL, 'winKV', 'location=yes,status=yes,toolbar=yes,menubar=yes,scrollbars=yes,width=400,height=530');
  winKV.focus();
}

function OpenColorsearch() {
  winCS = window.open('colorsearch.html', 'winCS', 'location=no,status=no,toolbar=no,menubar=no,scrollbars=yes,width=435,height=530');
  winCS.focus();
}


function OpenerLink(strURL) {
  if(parent.opener){
    parent.opener.location.href = strURL;
    parent.opener.focus();
  }else{
    location.href = strURL;
  }
}


//lightbox stuff

function SelectAllPhotos() {
  var theForm = document.FormLightboxPhotos;
  
  for (i=0,n=theForm.elements.length;i<n;i++) {
    if (theForm.elements[i].name.indexOf('chk') == 0) theForm.elements[i].checked = true;
  }
}

function DeSelectAllPhotos() {
  var theForm = document.FormLightboxPhotos;
  
  for (i=0,n=theForm.elements.length;i<n;i++) {
    if (theForm.elements[i].name.indexOf('chk') == 0) theForm.elements[i].checked = false;
  }
}

function ReversePhotoSelection() {
  var theForm = document.FormLightboxPhotos;
  
  for (i=0,n=theForm.elements.length;i<n;i++) {
    if (theForm.elements[i].name.indexOf('chk') == 0) {
      if (theForm.elements[i].checked == false) {
        theForm.elements[i].checked = true;
      } else {
        theForm.elements[i].checked = false;
      }
    }
  }
}

function CountSelectedPhotos() {
		var theForm = document.FormLightboxPhotos;
		var selectedPhotos = 0;
		
		for (i=0,n=theForm.elements.length;i<n;i++) {
      if (theForm.elements[i].name.indexOf('chk') == 0 && theForm.elements[i].checked == true) selectedPhotos++;
    }
    
		return selectedPhotos;
}

function GetSelectedPhotoIDs() {
  var theForm = document.FormLightboxPhotos;
  var strPhotoIDs = "";
  
  for (i=0,n=theForm.elements.length;i<n;i++) {
  	if (theForm.elements[i].name.indexOf('chk') == 0 && theForm.elements[i].checked == true) strPhotoIDs += theForm.elements[i].value + ", ";
  }
  
  if (strPhotoIDs != "") {
     strPhotoIDs = String(strPhotoIDs).substring(0,String(strPhotoIDs).length-2);
  }
  		
  return strPhotoIDs;
}

function DownloadSelectedPhotos(strFormAction) {
  var theForm = document.FormLightboxPhotos;
  var intPhotoCount = CountSelectedPhotos();
  
  if (intPhotoCount > 0) {
    theForm.SelectedPhotoIDs.value = GetSelectedPhotoIDs();
    theForm.target = "main";
    theForm.action = strFormAction;
    theForm.submit();
  }
}

function RemoveSelectedPhotos(strFormAction) {
  var theForm = document.FormLightboxPhotos;
  var intPhotoCount = CountSelectedPhotos();
  
  if (intPhotoCount > 0) {
    theForm.SelectedPhotoIDs.value = GetSelectedPhotoIDs();
    theForm.action = strFormAction;
    theForm.submit();
  }
}

function CopySelectedPhotos(strFormAction) {
  var theForm = document.FormLightboxPhotos;
  var intPhotoCount = CountSelectedPhotos();
  
  if (intPhotoCount > 0) {
    theForm.SelectedPhotoIDs.value = GetSelectedPhotoIDs();
    theForm.action = strFormAction;
    theForm.submit();
  }
}

function MoveSelectedPhotos(strFormAction) {
  var theForm = document.FormLightboxPhotos;
  var intPhotoCount = CountSelectedPhotos();
  
  if (intPhotoCount > 0) {
    theForm.SelectedPhotoIDs.value = GetSelectedPhotoIDs();
    theForm.action = strFormAction;
    theForm.submit();
  }
}


function NotificationRemoveSelected(strFormAction) {
  var theForm = document.FormNotification;
  
  theForm.action = strFormAction;
  theForm.submit();
}

function NotificationMoveSelected(strFormAction) {
  var theForm = document.FormNotification;
  
  theForm.action = strFormAction;
  theForm.submit();
}

function NotificationToggle(theForm) {
  for (i=0,n=theForm.elements.length;i<n;i++) {
    if (theForm.elements[i].name.indexOf('chk') == 0) {
      theForm.elements[i].checked = theForm.ToggleNotifications.checked
    }
  }
}

function ReplaceInputfieldValue(objInputfield, strNewValue) {
  objInputfield.value = strNewValue
}

function AppendInputfieldValue(objInputfield, strNewValue) {
  objInputfield.value = trim(objInputfield.value);
  
  //enclose the current keyword with quotation marks if the keyword is a term
  if (strNewValue.indexOf(' ') != -1) strNewValue = '"' + strNewValue + '"';
  
  if (isEmpty(objInputfield.value)) {
  	objInputfield.value = strNewValue;
  } else {
  	objInputfield.value = objInputfield.value + ', ' + strNewValue;
  }
}


function trim(str) {
   return str.replace(/^\s*|\s*$/g,"");
}

function isEmpty(s) {
	return ((s == null) || (s.length == 0));
}


function checkPasswordSecurity(target) {
	var TestWords = false;
	var TestNumberOfChars = false;
	var TestUppercaseChars = false;
	var TestLowercaseChars = false;
	var TestDigits = false;
	var TestSpecialChars = false;
	var TestCounter = false;
	var TestWhiteSpaces = false;
	var TestSecondPassword = false;
	
	var counter = 0;
	strInput = target.value;
	objImage = document.images.PasswordSecurity
	
	
	/* Check if length of password is greather then 8 */
	
	if(strInput.length >= 8) {
		
		if(!TestNumberOfChars) {		// do it only if the counter is 8, if it's greather, the TestNumberOfChars will be true and the right image is shown
	
			TestNumberOfChars = true;
		
		}
	} else {
	
		TestNumberOfChars = false;
	
	}
	
	
	/* check uppercase Letters */
	
	if(strInput.match(/[A-Z]/)) {
	
		TestUppercaseChars = true;
	
	} else {
	
		TestUppercaseChars = false;
	
	}
	
	/* check lowercase letters */
	
	if(strInput.match(/[a-z]/)) {
	
		TestLowercaseChars = true;
	
	} else {
	
		TestLowercaseChars = false;
	
	}
	
	
	/* check for digits */
	
	if(strInput.match(/[0-9]/)) {
	
		TestDigits = true;
	
	} else {
	
		TestDigits = false;
	
	}
	
	
	/* check special chars */
	
	if(strInput.match(/[^a-zA-Z0-9]/)) {
	
		TestSpecialChars = true;
	
	} else {
	
		TestSpecialChars = false;
	
	}
	
	
	/* Check for whitespaces and ESC-Sequences */
	
	if(strInput.match(/\s/)) {
	
		TestWhiteSpaces = true;
	
	} else {
	
		TestWhiteSpaces = false;
	
	}
	
	
	if(TestUppercaseChars) { counter++; }
	if(TestLowercaseChars) { counter++; }
	if(TestDigits)         { counter++; }
	if(TestSpecialChars)   { counter++; }
	if(TestWhiteSpaces)   { counter++; }
	if(TestNumberOfChars)   { counter++; }	
	
	if(counter == 0) { objImage.src = "images/pwdsec0.gif"; }
  if(counter == 1) { objImage.src = "images/pwdsec1.gif"; }
  if(counter == 2) { objImage.src = "images/pwdsec2.gif"; }
  if(counter == 3) { objImage.src = "images/pwdsec3.gif"; }
  if(counter == 4) { objImage.src = "images/pwdsec4.gif"; }
  if(counter == 5) { objImage.src = "images/pwdsec5.gif"; }
  if(counter == 6) { objImage.src = "images/pwdsec6.gif"; }
	
}

function ToggleBlock(obj)
{
  if (document.getElementById(obj + 'On').style.display == "none" || document.getElementById(obj + 'On').style.display == "")
  {
    document.getElementById(obj + 'On').style.display = "block";
    document.getElementById(obj + 'Off').style.display = "none";
  }
  else
  {
    document.getElementById(obj + 'On').style.display = "none";
    document.getElementById(obj + 'Off').style.display = "block";
  }
}

function InputSet(obj, text)
{
  document.getElementById(obj).value = text
}



/* popup windows */
function NewWindow(mypage, myname, w, h, scroll) {
winprops = 'height='+h+',width='+w+',scrollbars='+scroll+',resizable=no, top=20, left=20'
win = window.open(mypage, myname, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
} 

 var Behaviour = {
    register: function(rules){
      this.rules = rules;
      for(rule in this.rules){
        this.reload(rule);
      }
    },

    reload: function(rule){
      elements = $$(rule);
      for(y=0;y<elements.length;y++){
        this.rules[rule](elements[y]);
      }
    }

  }


/**
 set your behaviour rules here
*/
myrules = { 
        'a.pop435-530':function(elmt) {
    	elmt.onclick = function() {
    		NewWindow(this.href,'435x530','435','530','yes');return false;
    	}
    },
        'a.pop640-530':function(elmt) {
    	elmt.onclick = function() {
    		NewWindow(this.href,'640x500','640','530','yes');return false;
    	}
    },
        'a.pop200-300':function(elmt) {
    	elmt.onclick = function() {
    		NewWindow(this.href,'200x300','200','300','yes');return false;
    	}
    },
        'a.pop320-240':function(elmt) {
    	elmt.onclick = function() {
    		NewWindow(this.href,'320x240','320','240','yes');return false;
    	}
    },

        'a.pop370-500':function(elmt) {
    	elmt.onclick = function() {
    		NewWindow(this.href,'370x500','370','500','yes');return false;
    	}
    },

        'a.pop400-530':function(elmt) {
    	elmt.onclick = function() {
    		NewWindow(this.href,'400x530','400','530','yes');return false;
    	}
    }

};
window.onload = function(){ Behaviour.register(myrules);};
/*** Temporary text filler function. Remove when deploying template. ***/
var gibberish=["This is just some filler text", "Welcome to Dynamic Drive CSS Library", "Demo content nothing to read here"]
function filltext(words){
for (var i=0; i<words; i++)
document.write(gibberish[Math.floor(Math.random()*3)]+" ")
}