// Common Routines

//get cookie value
function getCookie(Name){ 
	//construct RE to search for target name/value pair
	var re = new RegExp( Name + "=[^;]+", "i" ); 
	//if cookie found return its value
	if( document.cookie.match(re) ) 
		return document.cookie.match(re)[0].split("=")[1];
	return "";
}

//set cookie value
function setCookie(name, value, days){ 
	var expireDate = new Date();
	//set "expstring" to either future or past date, to set or delete cookie, respectively
	var expstring = expireDate.setDate( expireDate.getDate() + parseInt(days) );
	document.cookie = name + "=" + value + "; expires=" + expireDate.toGMTString() + "; path=/";
}

function isFile(str){
    var O= AJ();
    if(!O) return false;
    try
    {
        O.open("HEAD", str, false);
        O.send(null);
        return (O.status==200) ? true : false;
    }
    catch(er)
    {
        return false;
    }
}

function AJ()
{
    var obj;
    if (window.XMLHttpRequest)
    {
        obj= new XMLHttpRequest();
    }
    else if (window.ActiveXObject) 
    {
        try
        {
            obj= new ActiveXObject('MSXML2.XMLHTTP.3.0');
        }
        catch(er)
        {
            obj=false;
        }
    }
    return obj;
}

////////////////////////////////////
//Encryption Algorythm ////////////
////////////////////////////////////

var char_set = '$%^NOZ1&PQR(./~`"CDEFG!@STUVWZghij}:<pHB*#8uvwx>?[]\',ef34590qrklmnoIJ)_+{st67 abcdAyzKLM2Y';

function doSecure(key, password) {
//function encrypt_data() {
	var input = password;
	var output = "";
	var char_code;

	//set algorithm index (1-9)
	var algorithm = 9;
	algorithm++;

	var alpha_length = char_set.length - algorithm;
	var space;

	//begin for loop to cycle through input
	for (loop=0; loop<input.length; loop++) {
	
		//if conditional detects unknown characters
		if (char_set.indexOf(input.charAt(loop)) == -1) {
			//alert("Program Error: Unknown Character!");
		}
	
		//search char_set string for character and set char_code variable...
		char_code = char_set.indexOf(input.charAt(loop));
	
		//actual text encoding algorithm goes here
		if (char_code + algorithm > char_set.length)
		{
			space = char_set.length - char_code;
			char_code = algorithm - space;
		}
		else
		{
			char_code += algorithm;
		}
	
		//set output variable in accordance to char_set
		output += char_set.charAt(char_code);
	}
	//Return Encrypted Texzt
//	window.input.input_field.value = output;
	return output;
}

function doUnsecure(key, password) {
//	function decrypt_data() {
	var input = password;
	var output = "";
	var char_code;

	//set algorithm index (1-9)
	var algorithm = 9;
	algorithm++;

	var alpha_length = char_set.length - algorithm;
	var space;

	//begin for loop to cycle through input
	for (loop=0; loop<input.length; loop++) {
	
		//if conditional detects unknown characters
		if (char_set.indexOf(input.charAt(loop)) == -1) {
			//alert("Program Error: Unknown Character!");
		}
	
		//search char_set string for character and set char_code variable...
		char_code = char_set.indexOf(input.charAt(loop));
	
		//oppisite of encrypt algorithm goes here
		if (char_code - algorithm < 0)
		{
			space = algorithm - char_code;
			char_code = char_set.length - space;
		}
		else
		{
			char_code -= algorithm;
		}
	
		//set output variable in accordance to char_set
		output += char_set.charAt(char_code);
	}
	//dump contents of output variable into textarea
//	window.input.input_field.value = output;
	return output;
}

