  	var descID, tagID, divID, txtCommentsID, filesID, dateDivTagID, dateInputValID, errorLabelID, uploadFormID, uploadFormSubmitID;
	var uploadFileDivID, fileTitleID, uniqueID;
	var transaction, URIOfFile, URIOfContact, NameOfContact, currentFunc;
	var permaLINK, isRemote, refreshPage, isContactToBeCreated, isVersionOfFile;

    function init() {
      $('userId').focus();
	}

    function clearAll() {
      $('userId').value='';
      $('password').value='';
      $('confirm').value='';
	}
	
	function enableSubmit(checkbox) {
      		if (checkbox.checked) {
        		updateInnerHTML('infoTerms', '');
       			//$('signup').className = "disabled";
      		}
      		//else
			//	$('signup').className = "wizard";
    }
    
    function showAndHide(divId, toBeFocusedId) {
		var disp = document.getElementById(divId);
		if(disp != null) {
			if(disp.style.display == "block") {
				disp.style.display = "none";
			} else {
				disp.style.display = "block";
				if(toBeFocusedId != undefined) {
					document.getElementById(toBeFocusedId).focus();
				}
			}
		}
	}
    
    /**
     * Used for hiding a block.
     */
    function hide(divId) {
    	if(document.getElementById(divId) != null) {
	    	document.getElementById(divId).style.display = "none";
	    }
    }
    	
	function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

	/**
	 * Used to alternatively show and hide the block.
	 * Currently it is used for "Editing file description and tags",
	 * "uploading file".
	 */
	function showAndHide(divId, toBeFocusedId) {
		var disp = document.getElementById(divId);
		if(disp != null) {
			if(disp.style.display == "block") {
				disp.style.display = "none";
			} else {
				disp.style.display = "block";
				if(toBeFocusedId != undefined) {
					document.getElementById(toBeFocusedId).focus();
				}
			}
		}
	}
    
    /**
     * Used for hiding a block.
     */
    function hideElem(divId) {
    	if(document.getElementById(divId) != null) {
	    	document.getElementById(divId).style.display = "none";
	    }
    }
    
    /**
     * Updates inner html value of the given element.
     */
     function updateInnerHTML(elementID, newValue) {
     	var thisObj = document.getElementById(elementID);
     	if (thisObj != null) {
     		thisObj.innerHTML = newValue;
     	}
     }
        
    /**
     * On successful asynchronous ajax transaction, this block will be executed,
     * according to the current functionality.
     *
     * The values of currentFunc gloabal variable are
		 * editTags	- set when the current functionality is editing description 
		 *						and tags.
		 * save			- Set on posting comment.
		 * delete		- Set on deleting a comment.
		 * delFile	- Set on deleting a shared file.
     */
    var parseMessages = function(o) {
        if(currentFunc == "signin" || currentFunc == "createAccount") {
        	var resp = o.responseText;
			alert(resp);
	        
	        //document.account.submit();
        	
        	// SIGNIN_SUCCESS
        	if(parseInt(resp[0]) == 1) {
				//var frm = document.forms[0];
        		//frm.submit();
        		
                window.location.href = resp[1];	        
	        // ACCOUNT_NOT_FOUND
	        } 
        }
        else if(currentFunc == "cancelAccount") {
          window.location.href = "/settings/thanks";	        
        }
        else {
          document.location.reload(true);
        }
    }
        
    /**
     * If asynchronous ajax transaction fails, this block will be executed.
     */
    var handleFailure = function(o) {
    	if( o.conn && o.conn.status != undefined && o.conn.status != 0 && currentFunc != "openFile" ) {
	        alert("Connection Failed " + o);
		}
		
		if(currentFunc != "cancelSignin") {
    		alert("Connection Failed " + o);
    	}
    	
    	if(currentFunc == "signin") {
    	    hideElem('logging_in');
		} else if(currentFunc == "cancelSignin") {
        	window.location.href = 'about:blank';
        }
    };
    
	/**
	 * Tells what to be done after the ajax transaction.
	 */
    var callBack = {
        success: parseMessages,
        failure: handleFailure,
        argument: []
    };
        
    /**
     * 
     */
    function createAccount() {
    	currentFunc = 'createAccount';
        clearErrors();
		
        var email = trim(document.getElementById("userId").value.toLowerCase());
        var password = trim(document.getElementById("password").value);
        var siteName = trim(document.getElementById("siteName").value.toLowerCase());

		var confirm;
		if (document.getElementById("confirm") != null)
			confirm = trim(document.getElementById("confirm").value);
        
		var sawErrors = false;
        
        if(email == "") {
          updateInnerHTML('email_notice', '  Please specify your email address.');
          sawErrors = true;
        }
       
       	if (email != "" && !checkEmail(email)) {
       	  updateInnerHTML('email_notice', '  Please specify a valid email address.');
          sawErrors = true;
        }
       	
        if(password == "") {
          updateInnerHTML('password_notice', '  Please specify a password.');
          sawErrors = true;
        }
       
        if(siteName == "") {
			$('url_notice').show();
			updateInnerHTML('url_notice', '  Please specify a site name.');
            sawErrors = true;
        }

		if(siteName != "" && !checkURL(siteName)) {
          $('url_notice').show();
		  updateInnerHTML('url_notice', '  Please specify a valid site name.');
          sawErrors = true;
        }
        
        //if(confirm != null && (password != confirm)) {
        //  updateInnerHTML('password_notice', '  Passwords do not match.');
        //  sawErrors = true;
        //}
                
        if (sawErrors) {
		 return;
		}
    	
		document.getElementById("os").value = navigator.platform;
		document.getElementById("browser").value = navigator.userAgent;

		var referrer = readCookie("referrer");
		if (referrer != null)
		  document.getElementById("referrer").value = referrer;

		var entrypage = readCookie("entrypage");
		if (entrypage != null)
		  document.getElementById("entrypage").value = entrypage;

		KM.identify(email);
		createCookie('userId', email, 1); 
		createCookie('password', password, 1); 
		
    	showAndHide('creating_account');
    	
		document.create_account.submit();		
    }

    /**
     * 
     */
    function linkFBAccount() {
    	currentFunc = 'linkFBAccount';
				
        var userId = trim(document.getElementById("userId").value.toLowerCase());
        var fbId = trim(document.getElementById("fbId").value);
        var accessToken = trim(document.getElementById("accessToken").value.toLowerCase());
        
		var sawErrors = false;
        
        if(userId == "") {
          sawErrors = true;
        }

        if(fbId == "") {
          sawErrors = true;
        }

        if(accessToken == "") {
          sawErrors = true;
        }

        if (sawErrors) {
		 return;
		}
    			
    	//document.getElementById('message').innerHTML = "Linking your Facebook account...";
    	
		document.link_fb_account.submit();		
    }

    function createFBAccount() {
    	currentFunc = 'createFBAccount';
        //clearErrors();
				
        var email = trim(document.getElementById("fb_userId").value);
        var password = 'abc123';
        var siteName = trim(document.getElementById("fb_siteName").value);

		var sawErrors = false;
        
        if(email == "") {
          updateInnerHTML('fb_email_notice', '  Please specify your email address.');
          sawErrors = true;
        }
       
       	if (email != "" && !checkEmail(email)) {
       	  updateInnerHTML('fb_email_notice', '  Please specify a valid email address.');
          sawErrors = true;
        }
       	
        if(siteName == "") {
			$('url_notice').show();
			updateInnerHTML('fb_url_notice', '  Please specify a site name.');
            sawErrors = true;
        }

		if(siteName != "" && !checkURL(siteName)) {
          $('url_notice').show();
		  updateInnerHTML('fb_url_notice', '  Please specify a valid site name.');
          sawErrors = true;
        }
                
        if (sawErrors) {
			alert('sawErrors');
		 return;
		}

		document.getElementById("os").value = navigator.platform;
		document.getElementById("browser").value = navigator.userAgent;

		KM.identify(email);
		createCookie('userId', email, 1); 
		createCookie('password', password, 1); 
		
    	showAndHide('fb_creating_account');
    	
		document.create_fb_account.submit();		
    }

	function clearErrors() {
	      updateInnerHTML('email_notice', '');
	      updateInnerHTML('password_notice', '');
	      updateInnerHTML('url_notice', '');
	      updateInnerHTML('infoTerms', '');
	}

    /**
     * 
     */
    function upgradeAccount() {
    	currentFunc = 'upgradeAccount';
        
        var cardType = "";
        if (document.getElementById("visaid").checked)
        	cardType = "VISA";
        else if (document.getElementById("masterid").checked)
        	cardType = "MASTER";
        if (document.getElementById("amexid").checked)
        	cardType = "AMEX";
         
        var cardName = trim(document.getElementById("card_name").value);
        var cardNumber = trim(document.getElementById("card_number").value);
        var expMonth = trim(document.getElementById("exp_month").value);
        var expYear = trim(document.getElementById("exp_year").value);
        var street1 = trim(document.getElementById("street1").value);
        var street2 = trim(document.getElementById("street2").value);
        var city = trim(document.getElementById("city").value);
        var state = trim(document.getElementById("state").value);
        var zip = trim(document.getElementById("zip").value);
        var country = trim(document.getElementById("country").value);
        var coupon = trim(document.getElementById("coupon").value);
                
    	var url = "/api/account/upgrade";
    	var postBody = cardType + "\n" + cardName + "\n" + cardNumber + "\n" + expMonth + "\n"
    	+ expYear + "\n" + street1 + "\n" + street2 + "\n"
    	+ city + "\n" + state + "\n" + zip + "\n"
    	+ country + "\n" + coupon + "\n";
    	transaction = YAHOO.util.Connect.asyncRequest('POST', url, callBack, postBody);    	
    }

    /**
     * 
     */
    function cancelAccount() {
    	currentFunc = 'cancelAccount';
        
    	var url = "/api/account/cancel";
    	var postBody = "\n";
    	transaction = YAHOO.util.Connect.asyncRequest('POST', url, callBack, postBody);
    }
	
	function signin() {
		currentFunc = 'signin';
		var username = trim(document.getElementById('userid').value);
		var password = trim(document.getElementById('idPassword').value);
		updateInnerHTML('infoUsername', '');
		updateInnerHTML('infoPassword', '');
		if(username == "") {
			updateInnerHTML('infoUsername', '  Please specify username');
			return;
		} 
		
		if(username != "") {
			if(password == "") {
				updateInnerHTML('infoPassword', '  Please specify password');
				return;
			}
		}
		
		document.getElementById('login').value = username;
		document.getElementById('pass').value = password;

    	showAndHide('logging_in');

		var url = "/dnsws/verifySignin";
		var postBody = "0\n" + username + "\n" + password + "\n" + "true";
		transaction = YAHOO.util.Connect.asyncRequest('POST', url, callBack, postBody);		
	}

	function showLogin() {
	  showAndHide('sign_in');
	  hideElem('create_account');
	}
		
	function cancel() {
		currentFunc = 'cancelSignin';
		var url = "/dnsws/verifySignin";
		transaction = YAHOO.util.Connect.asyncRequest('POST', url, callBack, "-1");
	}
	
	function trim(str) {
		str = this != window? this : str;
		return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
	}
	
	function getParameterValue(url, paramName) {
		var splittedURL = url.split('?');
		if(splittedURL.length > 1) {
			var startIndex = splittedURL[1].indexOf(paramName);
			if(startIndex != -1) {
				var endIndex = splittedURL[1].indexOf('&', startIndex+1);
				if(endIndex == -1) {
					endIndex = splittedURL[1].length;
				}
				return splittedURL[1].substring(startIndex, endIndex).split('=')[1];
			}
		}
		return "";
	}
	
	function checkEmail(value) {
	    var testresults=false;
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if (filter.test(value))
			testresults=true
		else{
			testresults=false
		}
		return (testresults)
	}
	
	function checkURL(value) {
		var testresults=false;
		var filter=/^(?:[A-Za-z0-9-_]+)+$/i
		if (filter.test(value))
			testresults=true
		else{
			testresults=false
		}
		return (testresults)
	}

	function removeParamFromURL(paramName, URL) {
		var currentURL = document.location.href;
		if(URL) {
			currentURL = URL;
		}
		var newURL = currentURL;
		var paramIndex = currentURL.indexOf('?' + paramName + '=');
		if(paramIndex == -1) {
			paramIndex = currentURL.indexOf('&' + paramName + '=');
		}
		if(paramIndex != -1) {
			paramIndex += 1;
			var currentURLLength = currentURL.length;
			var i = 0;
			for(i=paramIndex+paramName.length+1; i<currentURLLength; i++) {
				if( currentURL.charAt(i) == '&' ) {
					break;
				}
			}
			var firstPart = currentURL.substring(0, paramIndex);
			var lastPart = currentURL.substring(i, currentURLLength);
			if( lastPart.charAt(0)== '&' ) {
				lastPart = lastPart.substring(1, lastPart.length);
			}
			newURL = firstPart + lastPart; 
			if( newURL.charAt(newURL.length-1) == '&' || newURL.charAt(newURL.length-1) == '?' ) {
				newURL = newURL.substring(0, newURL.length-1); 
			} 
		}
		return newURL;
	}
	
	function getPageForFilter(filterName, filterVal, toGetNewURL) {
		var newURL;
		if( filterName == "search" || filterName == "tag" || filterName == "fileType" ) {
			newURL = removeParamFromURL("search");
			newURL = removeParamFromURL("tag", newURL);
			if(filterName != "tag") {
				newURL = removeParamFromURL("fileType", newURL);
			}
		} else {
			newURL = removeParamFromURL(filterName);
		}
		if(newURL.indexOf('?') != -1) {
			newURL += '&';
		} else {
			newURL += '?';
		}
		newURL += filterName + '=' + filterVal;
		if(toGetNewURL == undefined) {
			window.location.href = newURL;
		} else {
			return newURL;
		}
    }
	
	
	if (readCookie("referrer") == null) {
	  var referrer = ((document.referrer != "") ? document.referrer : "direct");
	  createCookie("referrer", referrer, 90);
	}
	
	if (readCookie("entrypage") == null)
	  createCookie("entrypage", window.location.href, 90);

