//hide movie splash and start movie ---------------

movieclip.addEventListener('ended', show('moviesplash'), false);


function startmovie(){
	document.getElementById("moviesplash").style.display="none";
	//document.getElementById("loading").style.display="block";
	//t=setTimeout("playmovie()",[2000]);
	playmovie();
}

function playmovie(){
	//document.getElementById("loading").style.display="none";
	if(document.getElementById("moviesplash").style.display!=="none"){
		document.getElementById("moviesplash").style.display="none";
	}
	if(document.getElementById("movieclip").style.marginLeft != "-50px"){
		document.getElementById("movieclip").style.marginLeft = "-50px";
	}
	
	if(document.getElementById("html5movie"))movieclip.play();
	else document.movieclip.Play();
}
function stopmovie(){
	if(document.getElementById("html5movie"))movieclip.pause();
	else document.movieclip.Stop();
	
}

vUp.addEventListener('click',changevolume,false);

function changevolume(amt){
	vol= movieclip.volume;
	if(amt=="+")vol=vol+10;else vol=vol-10;
	volume.firstChild.nodeValue=vol;
}


// Choose already uploaded photo for postentry form----------
function choosephoto(img,id,cap,uid){
	if(img){
		if(uid){
			document.getElementById("uid"+uid).selected="selected";
		}
		if(document.getElementById("deletebox"))
			document.getElementById("deletebox").style.display="";
		document.getElementById('currentphoto').value=img;
		document.getElementById('photoid').value= id;
		document.getElementById('thephoto').src= "thumbs/"+img;
		if(cap)document.getElementById('caption').value= cap;
		else document.getElementById('caption').value= "";
		document.getElementById("photolabel").innerHTML="Chosen Photo:";
		document.getElementById("pname").innerHTML=img;
		resizeform("thumbnails");
	}
}

//check main email form------

function checkemailform(){
	if (document.emailform.subject.value=="" || document.emailform.message.value==""){
		alert("Both Subject and Message must be filled in");
		return false;
	}else if(document.entryform.emailto[0].checked && !isEmail(document.entryform.emailaddress.value)){
		alert("You have chosen the Individual Email option. The individual email address is missing or not properly formatted.");
		return false;
	}else if(document.entryform.emailto[5] && document.entryform.emailto[5].checked && !document.entryform.newmem.checked && !document.entryform.paid.checked && !document.entryform.grace.checked && !document.entryform.overdue.checked && !document.entryform.lapse1.checked && !document.entryform.lapse2.checked && !document.entryform.notpay.checked && !document.entryform.emailonly.checked && !document.entryform.newsonly.checked){
		alert("You have chosen the Member Group(s) option. At least one member group checkbox must be checked");
		return false;
	}
}

// Show/check modal email form----------

function checkmodalemailform(){
	 if (document.modalentryform.modalsubject.value=="" || document.modalentryform.modalmessage.value==""){
		document.getElementById("entrymessage").innerHTML="All of the boxes must be filled in<br>";
		return false;
	}else if(!isEmail(document.modalentryform.modalreplyto.value)){
		document.getElementById("entrymessage").innerHTML="Your email address is missing or not properly formatted.<br>";
	return false;
	}
}


//Check values of Postentry form -----------

function checkpostentry(){
	if(document.entryform.doc.value){
		if(document.entryform.doc.value.search(/.pdf/i)==-1) {
   			alert("A PDF upload must be a PDF file with a \".pdf\" file extension and <2MB filesize");
   			document.entryform.doc.value="";
   			return false;
		}
	}
	if(document.entryform.photo.value){
		if(document.entryform.photo.value.search(/.jpg/i)==-1 && document.entryform.photo.value.search(/.gif/i)==-1  && document.entryform.photo.value.search(/.png/i)==-1 ){
   			alert("A photo upload must be a GIF,JPEG or PNG file with the appropriate \".gif\",\".jpg\" or \".png\" file extension and < 2MB filesize");
   			document.entryform.photo.value="";
   			return false;
		}
	}
	if(!document.entryform.title.value && document.entryform.type.value=="p" &&document.entryform.position.value==1){
		alert("item number 1 in a project file must have a title");
   		return false;
	}
	if(document.entryform.date.value){
		var date=Date.parse(document.entryform.date.value);
		if(date=='NaN'){
			alert(date);
   			return false;
   		}
   	}
	
}

function changemeta(){
	var meta;
	if (document.getElementsByById("vport")) {
		meta = document.getElementById("vport");
		if (meta) {
			if(meta.content == "width=620,initial-scale=0.51"){
				meta.content = "width=620,initial-scale=1.7";
				window.scroll(0,156);
			}else{
				meta.content = "width=620,initial-scale=0.51";
				window.scroll(0,0);
			}
		}
	}
}

// parse modal email-----------

function mailto(uid,subject){
	var scname=document.getElementById("name"+uid);
	var nameplace=document.getElementById("emailname");
	nameplace.innerHTML=scname.innerHTML;
	document.getElementById("touid").value=uid;
	if(subject)document.getElementById("modalsubject").value=subject;
	else document.getElementById("modalsubject").value="";
	openModalWindow('m');
}


// generate reverse email address -------------

function revmail(str){
	var i=str.length;
	var revstr="";
	i=i-1;
	for (var x = i; x >=0; x--){
		revstr=revstr+str.charAt(x);
	}
	revstr=revstr.replace(/]/g,"@");
	window.location.href="mailto:"+revstr;
}

//Open modal entry form --------------------

function openModalWindow(type,x) {
   // w=436;
	var mainwidth=document.getElementById("mainwrap").offsetWidth;
	if(x=="x"){
		document.getElementById(type+"formshadow").style.display = "none";
		document.getElementById(type+"form").style.display = "none";
		document.getElementById("entrymessage").innerHTML="";
		document.getElementById("linktype").innerHTML="";
		//document.entryform.resizeformfor.value="";
	}else{
		//var w=455;
		document.getElementById(type+"form").style.display = "";
		var h=document.getElementById(type+"form").offsetHeight;
		var w=document.getElementById(type+"form").offsetWidth;
		if(x=="menu"){
			var formleft=0;
			var t=50;
			
		}else{
			var formleft= ((mainwidth-w)/2); //+67;
			var t=window.pageYOffset+70;
		}
		var shadowleft=(formleft-10);
		
  		 document.getElementById(type+"form").style.top = t+"px";
		document.getElementById(type+"form").style.left = formleft+"px";
		document.getElementById(type+"formshadow").style.top = (t-10)+"px";
		document.getElementById(type+"formshadow").style.left = shadowleft+"px";
		document.getElementById(type+"formshadow").style.width = w+20+"px";
		document.getElementById(type+"formshadow").style.height = (h+20)+"px";
		document.getElementById(type+"formshadow").style.display = "";
	}
}

// Resize modal form WITH EXPANDING CONTENT -------------

function resizeform(type){
	//document.entryform.resizeformfor.value=type;
	if(document.getElementById("linktype"))document.getElementById("linktype").innerHTML=type;
	if(type=="author"){showhide("authorlist");
	}else if(type=="email" || type=="profile") {showhide("memberlist"); 
	}else if(type=="uploadpdf") {showhide("uploadpdf");
	}else if(type=="uploadphoto"){showhide("uploadphoto");
	}else if(type=="calendar"){showhide("calendar");
	}else if(type=="thumbnails"){showhide("thumbnails");
	}
	var h = document.getElementById("rform").offsetHeight;
	document.getElementById("rformshadow").style.height=(h+20)+"px";
	
}


function contact(name,subject) {
	address="mailto:"+name+"@owa-usa.org";
	if(subject) address=address+"?subject="+subject;
	window.location.href=address; 
}

//ADD LINK TO SPLASH PHOTO -------

function splashlink(uid,name){
	document.getElementById("memberuid").value=uid;
}




//ADD LINK TO AUTHOR FIELD OR TEXT AREA -------

function memberlink(uid,name){
	if(document.getElementById("linktype").innerHTML=="author"){
		document.entryform.author.value="<a href=\"member.php?uid="+uid+"\">"+name+"</a>";
		hide("authorlist") ;
	}
	else if(document.getElementById("linktype").innerHTML=="email"){
		addMemberlink(uid,name);
	}
	else if(document.getElementById("linktype").innerHTML=="profile"){
		addMemberlink(uid,name,"profile");
	}
	hide("memberlist");
	
	var h = document.getElementById("rform").offsetHeight;
	document.getElementById("rformshadow").style.height=(h+20)+"px";
}


// insert owa member email link into textarea----------

function addMemberlink(uid,name,type){
	if(type=='profile'){
		tag1="<a href=\"member.php?uid="+uid+"\">"+name;tag2="</a>";
	}else{
		tag1="<a href=\"javascript:mailto(\'"+uid+"\')\"><span id=\"name"+uid+"\">"+name;tag2="</span></a>";
	}
	if(window.getSelection){
		var start=  document.entryform.message.selectionStart;
		var end = document.entryform.message.selectionEnd;
		var Text = document.entryform.message.value;
		var Textstart=Text.slice(0,start);
		var Textend=Text.slice(end);
		var seltext="";
		Text=Textstart+tag1+seltext+tag2+Textend;
		document.entryform.message.value=Text;
	}else{
		var seltext= document.selection.createRange().text;
		if(seltext!=""){
			Text=tag1+seltext+tag2;
			document.selection.createRange().text=Text;
		}
	}
}	

// HTML formatting inserts---------------

function addtags(type,uid,name){
 	
 	if(type=="b"){tag1="<b>";tag2="</b>";}
	if(type=="i"){tag1="<i>";tag2="</i>";}
	if(type.indexOf("h")!== -1){
		var URL = prompt("Enter the URL" ,"http://");
		if (URL=="http://" || !URL)return;
		if(type=="h"){tag1="<a target=\"_blank\" href=\""+URL+"\">";
		}else{ tag1="<a style=\"color:#807050\" href=\""+URL+"\">";}
		tag2="</a>";
	}
	if(type.indexOf("m")>=0){
		var URL = prompt("Enter the email address" );
		if (!URL)return;
		var i=URL.length;
		if(type=="m"){
			var revstr="";
			i=i-1;
			for (var x = i; x >=0; x--){
				revstr=revstr+URL.charAt(x);
			}
			revstr=revstr.replace(/]/g,"@");
			tag1="<a href=\"javascript:revmail(\'"+revstr+"\')\">";
		}else{
			tag1="<a style=\"color:#807050;\" href=\"mailto:"+URL+"\">";
		}
		tag2="</a>";
	}
	if(type=="d"){tag1="<div id=\"indent\">";tag2="</div>";}
	
	if(window.getSelection){
		var start=  document.entryform.message.selectionStart;
		var end = document.entryform.message.selectionEnd;
		var Text = document.entryform.message.value;
		var Textstart=Text.slice(0,start);
		var Textend=Text.slice(end);
		if(start==end){var seltext=URL;}
		else{var seltext=Text.slice(start,end);}
		Text=Textstart+tag1+seltext+tag2+Textend;
		document.entryform.message.value=Text;
	}else{
		var seltext= document.selection.createRange().text;
		if(seltext!=""){
			Text=tag1+seltext+tag2;
			document.selection.createRange().text=Text;
		}
	}
	
}

// Function to show or hide a given object ---------
function showhide(id) {
	if(document.getElementById) {
		var obj = document.getElementById(id);
		if(obj.style.display == "none"){
			obj.style.display = "block";
		}else{
			obj.style.display = "none";
		}
	}
}

// Function to hide a given object
function hide(id) {
	if(document.getElementById) {
		obj = document.getElementById(id);
		obj.style.display = "none";
	}
}

// Function to show a given object
function show(id) {
	if(document.getElementById) {
		obj = document.getElementById(id);
		obj.style.display = "block";
	}
}


// show or hide a block of text --------------

function toggle(uid) {
	var ele = document.getElementById("toggleText"+uid);
	var text = document.getElementById("displayText"+uid);
	if(ele.style.display == "block") {
    		ele.style.display = "none";
	}else{ele.style.display = "block";}
} 


// Spawn a popup window
function openPopup(url,winname,w,h,parent) {
	window.name=parent;
	var newWindow = window.open(url, winname,'width=' + w + ',height=' + h + ',toolbar=no,resizable=yes,scrollbars=yes,status=yes,left=0');
	newWindow.focus();
}



// Function to change the class of an object
function changeClass(id,new_class) {
	if(document.getElementById) {
		obj = document.getElementById(id);
		if(obj) {
			obj.className = new_class;
			return true;
		}
	}
	return false;
}

// Wrapper function to change the classes of several objects
function changeClasses(elmList,color) {
	if (document.getElementById) {
		for (var i = 0; i < elmList.length; i++) {
			changeClass(elmList[i],color);
		}
	}
}

// Function to show the properties of an object in an alert window
function showProps(obj) {
	var str = '';
	for(prop in obj) {
		str += prop + ' : ' + obj[prop] + "\n";
	}
	alert(str);
}

// MM Image swapping functions
function preloadImages() { 
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
	var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}
function findObj(n, d) { 
  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=findObj(n,d.layers[i].document); return x;
}
function swapImage() { 
  var i,j=0,x,a=swapImage.arguments; document.sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=findObj(a[i]))!=null){document.sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

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

// GENERIC CODE FOR CLIENT_SIDE ERROR VALIDATION
//  Core error checking functions taken from Netscape developer site

// VARIABLE DECLARATIONS
var errColor = "red";
var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var whitespace = " \t\n\r";
var decimalPointDelimiter = ".";
var phoneNumberDelimiters = ".()- ";
var validUSPhoneChars = digits + phoneNumberDelimiters;
var digitsInUSPhoneNumber = 10;
var ZIPCodeDelimiters = "-";
var validZIPCodeChars = digits + ZIPCodeDelimiters;
var digitsInZIPCode1 = 5;
var digitsInZIPCode2 = 9;
var creditCardDelimiters = "- ";


// ********************************************************
// BEGIN CORE ERROR CHECKING FUNCTIONS
// ********************************************************

// Check whether string s is empty.
function isEmpty(s) {   
	return ((s == null) || (s.length == 0));
}

// Returns true if string s is empty or whitespace characters only.
function isWhitespace (s) {   
	var i;
    if (isEmpty(s)) return true;

    for (i = 0; i < s.length; i++) {   
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// Removes all characters which appear in string bag from string s.
function stripCharsInBag (s, bag) {
	var i;
    var returnString = "";
    for (i = 0; i < s.length; i++) {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

// Removes all characters which do NOT appear in string bag 
// from string s.
function stripCharsNotInBag (s, bag) {
	var i;
    var returnString = "";
    for (i = 0; i < s.length; i++) {   
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }
    return returnString;
}

// Removes all whitespace characters from s.
function stripWhitespace (s) {
	return stripCharsInBag (s, whitespace)
}

// Removes initial (leading) whitespace characters from s.
function stripInitialWhitespace (s) {
	var i = 0;
    while ((i < s.length) && charInString (s.charAt(i), whitespace))
       i++;    
    return s.substring (i, s.length);
}

// Returns true if character c is an English letter 
// (A .. Z, a..z).
function isLetter (c) {
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

// Returns true if character c is a digit 
// (0 .. 9).
function isDigit (c) {
	return ((c >= "0") && (c <= "9"))
}

// Returns true if character c is a letter or digit.
function isLetterOrDigit (c) {
	return (isLetter(c) || isDigit(c))
}

// isInteger (STRING s [, BOOLEAN emptyOK])
// Returns true if all characters in string s are numbers.
function isInteger (s) {
	var i;
    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    for (i = 0; i < s.length; i++) {   
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}


// isAlphabetic (STRING s [, BOOLEAN emptyOK])
// Returns true if string s is English letters (A .. Z, a..z) only.
function isAlphabetic (s) {
	var i;
    if (isEmpty(s)) 
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);

    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (!isLetter(c))
        return false;
    }

    // All characters are letters.
    return true;
}

// isAlphanumeric (STRING s [, BOOLEAN emptyOK])
// Returns true if string s is English letters (A .. Z, a..z) and numbers only.
function isAlphanumeric (s) {
	var i;
    if (isEmpty(s)) 
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (! (isLetter(c) || isDigit(c) ) )
        return false;
    }

    // All characters are numbers or letters.
    return true;
}


// isEmail (STRING s [, BOOLEAN emptyOK])
function isEmail2 (s) {
	if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    if (isWhitespace(s)) return false;
    
	var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@")) {
		i++;
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != ".")) {
		i++;
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}


function isEmail(emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	
	if (matchArray==null) {
		return false;
		}
	var user=matchArray[1];
	var domain=matchArray[2];
	
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			return false;
			}
		}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			return false;
			}
		}
	if (user.match(userPat)==null) {
		return false;
		}
	
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false;
				}
			}
		return true;
		}
	
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			return false;
			}
		}
	
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		return false;
		}
	
	if (len<2) {
		return false;
		}
	
	return true;
}

// Get checked value from radio button.
function getRadioButtonValue (radio) {
	for (var i = 0; i < radio.length; i++) {
		if (radio[i].checked) { break }
    }
    return radio[i].value
}



// Determines if a SELECT menu has a value
//  Requires a menu structured as follows:
//    	<SELECT name="foobar">
//			<OPTION selected="selected">Choose a state...</OPTION>
//			<OPTION value="val1">Value 1</OPTION>
//				...
//		</SELECT>
function isSelected(selectObj) {
	if (selectObj.selectedIndex != 0) {
		return true;
	} else {
		return false;
	}
}

// Determines if radio button is selected
function radioIsSelected(radioObj) {
	for (var i = 0; i < radioObj.length; i++) {
		if (radioObj[i].checked) {
			return true;
		}
	} 
	return false;
}

// Utility function to get the selected value within a select menu
function getSelectedValue(selectObj) {
	var idx = selectObj.selectedIndex;
	return selectObj.options[idx].value;
}


