function randomString(argLength)
	{
    pool = new String("abcdefghijklmnopqrstuvwxyz1234567890");
    var i = 0;
    var passwd = " ";

    while(i <= argLength)
		{
        rand = parseFloat(Math.random()) * parseInt(pool.length);
        passwd += pool.charAt(rand);
        i++;
    	}
			
    passwd = passwd.substring(1, 8);
	return passwd;
	}

function setRandomPass(form, field)
	{
	var newpass = randomString(8);
	eval("document.forms."+form+"."+field+".value = \""+newpass+"\";");
	}	
/* added by huy tong to show pay rate information for writers */
function showPayRate() {
	var sel = document.getElementById("user_type");
	var type_id = sel.options[sel.selectedIndex].value;
	if (type_id == '6') {
		document.getElementById("user_pay_rate").style.visibility = 'visible';
		//document.getElementById("user_comment").style.visibility = 'visible';
	} else {
		document.getElementById("user_pay_rate").style.visibility = 'hidden';
		//document.getElementById("user_comment").style.visibility = 'hidden';
	}
}
/* added by huy tong to check if the writer agree to Disney's terms */
function assignmentCheckTerms()
{
	if (document.getElementById("termsagree").checked == true) {
		return true;
	} else {
		alert('You must check the box that says you agree to master legal agreement terms.')
		return false;
	}
}

function changeColor(field,form)
	{
	window.open("color_chooser.php?field="+field+"&form="+form, "mywin", "width=370,height=300");
	}
	
function helpBox(marea, sarea)
	{
	window.open("help.php?help_marea="+marea+"&help_sarea="+sarea, "mywin", "width=470,height=400");
	}

function commentsBox(userid)
	{
	window.open("a-comments-popup.php?f_userid="+userid, "mywin", "width=850,height=500,scrollbars=yes");
	}

function ratingsBox(userid, userarea)
	{
	window.open("a-ratings-popup.php?f_userid="+userid+"&f_userarea="+userarea, "mywin", "width=850,height=500,scrollbars=yes");
	}
	
function sortForm(formname, fieldname, value)
	{
	var cmd = "document.forms."+formname+"."+fieldname+".value = \""+value+"\";";
	eval(cmd);
	}
	
function sortUserRatings(formname, sbfield, sbvalue)
	{
	sortForm(formname, sbfield, sbvalue);
	var cmd = "document.forms."+formname+".submit();";
	eval(cmd);
	}	
	
function photoconPreview(cid)
	{
	window.open("photocon_preview.php?cid="+cid, "mywin", "width=700,height=600,scrollbars=yes");
	}

function photoconEditor(photofile)
	{
	window.open("photoconeditor.php?photofile="+photofile, "mywin", "width=900,height=700,scrollbars=yes,resizable=yes");
	}
	
function blockPreview(blockid) 
	{
	window.open("previews.php?s=blocks&blockid="+blockid, "mywin", "width=500,height=500,scrollbars=yes,resizable=yes");
	}
	
function assignmentsSourceRemove(sourceid) 
{
	var a = confirm("Are you sure you want to delete this source?");
	if (a) {
		window.location = "wrequests.php?action=removesource&sourceid="+sourceid;
	} else{
		
	}
}
function assignmentsImageRemove(imageid) 
{
	var a = confirm("Are you sure you want to delete this image?");
	if (a) {
		window.location = "wrequests.php?action=removeimage&imageid="+imageid;
	} else{
		
	}
}
function bookclubInterviewPreview(id) 
	{
	window.open("previews.php?s=bookclubinterview&interviewid="+id, "mywin", "width=500,height=500,scrollbars=yes,resizable=yes");
	}
function bookclubBookPreview(id) 
	{
	window.open("previews.php?s=bookclubbook&bookid="+id, "mywin", "width=500,height=500,scrollbars=yes,resizable=yes");
	}
function deleteBlock(blockid)
	{
		var a = confirm("Are you sure you want to delete this block?");
		if (a) {
			window.location = "blocks.php?action=deleteblock&blockid="+blockid;
		} else{
			
		}
	}
function deleteBook(book_id)
	{
		var a = confirm("Are you sure you want to delete this book?");
		if (a) {
			window.location = "bookclub.php?action=deletebook&book_id="+book_id;
		} else{

		}
	}
function deleteBookType(type_id)
	{
		var a = confirm("Are you sure you want to delete this book type?");
		if (a) {
			window.location = "bookclub.php?action=deletebooktype&type_id="+type_id;
		} else{

		}
	}
function deleteBookInterview(interview_id)
	{
		var a = confirm("Are you sure you want to delete this interview?");
		if (a) {
			window.location = "bookclub.php?action=deleteinterview&interview_id="+interview_id;
		} else{

		}
	}
function sitenewsPreview(newsDate,newssite,singlepreview)
	{
	window.open("sitenews_preview.php?news_date="+newsDate+"&newssite="+newssite+"&singlepreview="+singlepreview, "mywin", "width=800,height=600,scrollbars=yes,resizable=yes");
	}

function sitenewsDatePreview(newsDate)
	{
	window.open("sitenews_date_preview.php?news_date="+newsDate, "mywin", "width=800,height=600,scrollbars=yes,resizable=yes");
	}
		
function recallsPreview(recallcatid,publishDate,recallsiteid,singlepreview)
	{
	window.open("recalls_preview.php?recallcatid="+recallcatid+"&publish_date="+publishDate+"&recallsiteid="+recallsiteid+"&singlepreview="+singlepreview, "mywin", "width=800,height=600,scrollbars=yes,resizable=yes");
	}	

function recallsDatePreview(publishDate,recallsiteid)
	{
	window.open("recalls_date_preview.php?publish_date="+publishDate+"&recallsiteid="+recallsiteid, "mywin", "width=800,height=600,scrollbars=yes,resizable=yes");
	}	
	
function articlePreview(action,article_id,subrev_id)
	{
	window.open("articles_popup.php?action="+action+"&article_id="+article_id+"&subrev_id="+subrev_id, "mywin", "width=850,height=500,scrollbars=yes");
	}

function storyPreview(action,story_id,subrev_id)
	{
	window.open("stories_popup.php?action="+action+"&story_id="+story_id+"&subrev_id="+subrev_id, "mywin", "width=850,height=500,scrollbars=yes");
	}
	
function openSearchWTSM()
	{
	window.open("articles.php?action=articlessearch&skipheadfoot=1&wtsm=1", "mywin", "width=850,height=500,scrollbars=yes");		
	}	
	
function openMultipleSearchWTSM(fieldnum)
	{
	window.open("articles.php?action=articlessearch&skipheadfoot=1&wtsm=1&fieldnum="+fieldnum, "mywin", "width=850,height=500,scrollbars=yes");		
	}	
	
function insertCallout(textarea)
	{
	window.open("articles.php?action=add_callout&skipheadfoot=1&insert=1", "mywin", "width=850,height=500,scrollbars=yes");	
	}

function viewSource(source_id)
	{
	window.open("articles_popup.php?action=source_view&source_id="+source_id, "mywin", "width=450,height=300,scrollbars=yes");
	}	
	
function recipePreview(action,recipe_id)
	{
	window.open("recipes_popup.php?action="+action+"&recipe_id="+recipe_id, "mywin", "width=850,height=500,scrollbars=yes");
	}		

function mainPagePreview(build_id,site_id)
	{
	window.open("mainpages.php?skipheadfoot=1&action=preview&build_id="+build_id+"&site_id="+site_id, "mywin", "width=785,height=940,scrollbars=yes");
	}
	
		
function insertAtCursor(myField, myValue)
	{
	//IE support
	if(document.selection)
		{
		myField.focus();
		sel = opener.document.selection.createRange();
		sel.text = myValue;
		}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0')
		{
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
		+ myValue
		+ myField.value.substring(endPos, myField.value.length);
		}
	else
		{
		myField.value += myValue;
		}
	}

	
function insertCalloutInto(callout_id)
	{
	var header = document.forms.addcallout.callout_header.value;
	var footer = document.forms.addcallout.callout_footer.value;	
	var callout_text = document.forms.addcallout.callout_text.value;
	insertAtCursor(opener.document.forms.writearticle.article_text, '\n\n'+header+'\n'+callout_text+'\n'+footer+'\n\n');
	window.close();
	}
	
function inspect(elm)
	{
	var str = "";
	var acnt;
	for (var i in elm)
		{
		var x = elm.getAttribute(i);
		acnt++;
	    alert(i + ": " + x + "\n")
		}
	}

	
//For Spell Check
var currentSpellText = null;
function openSpellCheck(textarea,userid)
	{
	currentSpellText = textarea;
	var spellcheck = window.open("spell_popup.php?checktext="+textarea, 'editorWindow', 'width=720, height=450');
	spellcheck.focus();
	}

//For WYSIWYG
var currentTextArea = null
function openEditor(textarea,userid)
	{
	// location of edit.php file:
	var editFile = 'edithtml.php?userid='+userid;
	
	currentTextArea = textarea;

	var edit = window.open(editFile, 'editorWindow', 'width=720, height=450, status=yes');
	edit.focus();
	}

function openEditorWNoImage(textarea,userid)
	{
	// location of edit.php file:
	var editFile = 'edithtml.php?remove_image_button=1userid='+userid;
	
	currentTextArea = textarea;

	var edit = window.open(editFile, 'editorWindow', 'width=720, height=450');
	edit.focus();
	}
		
//For Mulitple Category Edit
function putcatin(form, ifield)
	{
	var val = form.options[form.selectedIndex].text;
	val = trim(val);
	var s = /\xA0/g;
	var d = /^\x2D(.*)$/;
	val = val.replace(s, "");
	if(val.match(d))
		{
		val = val.substr(1, val.length - 1);
		}
	ifield.value=val;
	}
	
//Trim Function
function trim(s) 
	{
	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
		{
		s = s.substring(1,s.length);
		}
	while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
		{
		s = s.substring(0,s.length-1);
		}
	return s;
	}
	
	
//display - show - hide - flip visibility
function show(id) 
{
document.getElementById(id).style.display = "block";
}

function hide(id) 
{
document.getElementById(id).style.display = "none";
}

function flipVisibility(id)
{
var current = "";
current = document.getElementById(id).style.display;
if(current == 'none') { document.getElementById(id).style.display = "block"; }
else {document.getElementById(id).style.display = "none";}	
}


//playlist list reorder functions
function outputList(ar, name, size) {
 var strIDs = "<SELECT SIZE=\"" + size + "\" NAME=\"ro_lst" + name + "\">"
 var sel = " SELECTED"
 for (var i=0;i<ar.length;i++) {
  strIDs += "<OPTION " + sel + " VALUE=\"" + ar[i][0] + "\">" + ar[i][1]
  sel = ""
 }
 strIDs+="</SELECT>"
 strIDs+="<INPUT NAME=\"" + name + "\" TYPE=hidden>"
 return strIDs
}

function outputButton(bDir,name,val) {
 return "<INPUT TYPE=button VALUE=\"" + val + "\" ONCLICK=\"move(this.form," + bDir + ",'" + name + "')\">"
}

function move(f,bDir,sName) {
 var el = f.elements["ro_lst" + sName]
 var idx = el.selectedIndex
 if (idx==-1) 
  alert("You must first select the item to reorder.")
 else {
  var nxidx = idx+( bDir? -1 : 1)
  if (nxidx<0) nxidx=el.length-1
  if (nxidx>=el.length) nxidx=0
  var oldVal = el[idx].value
  var oldText = el[idx].text
  el[idx].value = el[nxidx].value
  el[idx].text = el[nxidx].text
  el[nxidx].value = oldVal
  el[nxidx].text = oldText
  el.selectedIndex = nxidx
 }
}

function processForm(f) {
 for (var i=0;i<f.length;i++) {	
  var el = f[i]
  if (el.name.substring(0,6)=="ro_lst") {
   var strIDs = ""
   for (var j=0;j<f[i].options.length;j++)
     strIDs += f[i].options[j].value + ", "
   f.elements[f.elements[i].name.substring(6)].value = strIDs.substring(0,strIDs.length-2)
  }
 }
}

function generateSlug(original_field_id, slug_field_id)
{

document.getElementById(slug_field_id).value = document.getElementById(original_field_id).value.split(/[^a-zA-Z0-9-]+/).join("-").toLowerCase();

}

//////