/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Apache Jeff :: http://www.huntingground.freeserve.co.uk */

cookieName="usermemo"
expDays=365

maxLength=4000 // 4078 cookie max
document.onkeyup=chkLength
ns=document.getElementById&&!document.all
firstRun=1

//Memobox script
function initMemo(){
// enter the default text
document.getElementById("intro").innerHTML ="Copy & Paste the URL of any video on UzuVideo into this text area. After you finish, click the 'Save' button in the upper right hand corner. Now, you can access the link to all of your favorite videos whenever you click the 'Memo Pad' button! </div>"

getMemo()

displayLength=document.f1.the_pad.value.length

}

// ********** recieve (and reformat) the cookie data **********
function getMemo(){ // added function
inf=getCookie(cookieName) // get the saved memo
if(!inf){return}
document.f1.the_pad.value=inf // string from cookie
if(document.f1.the_pad.value.length>0){ // if memo exists
clrIntroLyr() // clear default text
}
chkLength()
}

function chkLength(e){
if(firstRun!=1){ // if there is info in memo-pad Moz shows incorrect count when first run due to Moz generated error message
keyChk=(!moz)?event.keyCode:e.which
firstRun=0 // firstRun prevents that error message
}
document.getElementById("remainder").style.color="#757575"
document.getElementById("remainder").style.fontWeight="normal"
document.getElementById("remainder").innerHTML=maxLength-escape(document.f1.the_pad.value).length

if(maxLength-escape(document.f1.the_pad.value).length<20){
document.getElementById("remainder").style.color="#DDDD00"

if(maxLength-escape(document.f1.the_pad.value).length<10){
document.getElementById("remainder").style.color="#DD0000"
document.getElementById("remainder").style.fontWeight="bold"
}

}

if(escape(document.f1.the_pad.value).length>maxLength){
alert("Your memo is over the "+maxLength+" characters allowed\nby the cookie command.\nThis may result in your memo not being saved.\n\nPlease reduce the size of your memo\n\nThank You")
}
}

function clrIntroLyr(){ // clear default text
document.getElementById("intro").style.left= -500
document.f1.the_pad.focus()
chkLength()
}

function deleteMemo(){
deleteCookie(cookieName)
document.f1.the_pad.value=""
document.getElementById("intro").style.left=0
document.getElementById("remainder").innerHTML="0000"
}

function emailMemo() {
input=document.f1.the_pad.value
var output = ""
for (var i = 0; i < input.length; i++) {
if ((input.charCodeAt(i) == 13) && (input.charCodeAt(i + 1) == 10)){
i++;
output += "%0D%0A"
} else {
output += input.charAt(i)
}
}
location="mailto:yourname@youraddress.com?Body="+output
}

function printMemo(){
padTxt=document["f1"]["the_pad"].value

output = ""
for (var i = 0; i < padTxt.length; i++) {

if(!moz){
if (padTxt.charCodeAt(i) == 13 && padTxt.charCodeAt(i + 1) == 10){output += "<BR>"}
else{output += padTxt.charAt(i)}
}

if(moz){
if (padTxt.charCodeAt(i) == 10){output += "<BR>"}
else{output += padTxt.charAt(i)}
}

}

printWin=window.open('','printwin','left=100.top=100,width=200,height=200')
printWin.document.write("<HTML>\n<HEAD>\n")
printWin.document.write("<TITLE>Print Page</TITLE>\n")
printWin.document.write("<script>\n")
printWin.document.write("<!--\n")
printWin.document.write("function chkstate(){\n")
printWin.document.write("if (document.readyState==\"complete\"){\n")
printWin.document.write("window.close()\n")
printWin.document.write("}\n")
printWin.document.write("else{\n")
printWin.document.write("setTimeout(\"chkstate()\",2000)\n")
printWin.document.write("}\n")
printWin.document.write("}\n")
printWin.document.write("function printWin(){\n")
printWin.document.write("window.print();\n")
printWin.document.write("chkstate();\n")
printWin.document.write("}\n")
printWin.document.write("//-->\n")
printWin.document.write("<\/script>\n")
printWin.document.write("</HEAD>\n")
printWin.document.write("<BODY onload=\"printWin()\">\n")
printWin.document.write(output)
printWin.document.write("</BODY>\n")
printWin.document.write("</HTML>\n")
printWin.document.close()
}

// ********** Pass the data to the cookie **********
function saveMemo(){ // added function
var expdate = new Date ();
expdate.setTime (expdate.getTime() + (expDays*24*60*60*1000)) // expiry date
path=null  // path="/" = top directory, path=null this documents root directory
data=document.f1.the_pad.value // string going to cookie
if(data.length==0){return} // if no data do not save
setCookie(cookieName,data,expdate,path) // this cookie is using dir root path
}

// Cookie functions
// An adaptation of Dorcht's function for getting a cookie.
function getCookie(name) {
var arg = name + "="
var alen = arg.length
var clen = document.cookie.length
var i = 0;
while (i < clen) {
var j = i + alen
if (document.cookie.substring(i, j) == arg){
return getCookieVal (j);
}
i = document.cookie.indexOf(" ", i) + 1
if (i == 0){
break
}
}
return null
}

function getCookieVal(num) {
var endstr = document.cookie.indexOf (";", num)
if (endstr == -1)
endstr = document.cookie.length
return unescape(document.cookie.substring(num, endstr))
}

// An adaptation of Dorcht's function for setting a cookie.
function setCookie(name, value, expires, path, domain, secure) {
if (!expires){expires = new Date()}
document.cookie = name + "=" + escape(value) +
((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
((secure == null) ? "" : "; secure")
}

// An adaptation of Dorcht's function for deleting a cookie.
function deleteCookie(name,path,domain) { // deletes cookie
document.cookie = name + "=" +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
"; expires=Thu, 01-Jan-00 00:00:01 GMT"
}

// hide or show CONFIRM div
function showHide(layerid){
if (document.getElementById(layerid).style.visibility != "hidden"){
document.getElementById(layerid).style.visibility = "hidden"
}else{
document.getElementById(layerid).style.visibility = "visible"
}
}

window.onload=initMemo;
window.onunload=saveMemo;
