//Copyright by Avadh Patel
//Created for SoftUse

var base64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split("");

function base64_encode (s)
 {
   var r = ""; var p = ""; var c = s.length % 3;
 
   if (c > 0) { for (; c < 3; c++) { p += '='; s += "\0"; } }
 
   for (c = 0; c < s.length; c += 3) {
 
     if (c > 0 && (c / 3 * 4) % 76 == 0) { r += "\r\n"; }
 
     var n = (s.charCodeAt(c) << 16) + (s.charCodeAt(c+1) << 8) + s.charCodeAt(c+2);
 
      n = [(n >>> 18) & 63, (n >>> 12) & 63, (n >>> 6) & 63, n & 63];
 
      r += base64chars[n[0]] + base64chars[n[1]] + base64chars[n[2]] + base64chars[n[3]];
   }
 
   return r.substring(0, r.length - p.length) + p;
 }


function getCookie(name) {
	//var name = "icon";
	if(document.cookie.length > 0) {
		index = document.cookie.indexOf(name + "=");
		if(index != -1) {
			index = index + name.length + 1;
			end = document.cookie.indexOf(";",index);
			if(end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(index, end));
		}
	}
	return "";
}

function setIconCookie(icon_url) {
	var name = "icon";
	var exdate = new Date();
	//set expiary date one day later
	exdate.setDate(exdate.getDate()+1);
	document.cookie = name + "=" + escape(icon_url) + ";expires=" + exdate.toGMTString();
}

function setURLCookie(web_url) {
	var name = "weburl";
	var exdate = new Date();
	exdate.setDate(exdate.getDate()+1);
	document.cookie = name + "=" + escape(web_url) + ";expires=" + exdate.toGMTString();
}

function setCookieFrmUrl() {
	var url = String(window.location);
	var index = url.indexOf("?");
	var iconUrl = "";
	var webUrl = "";
	if(index == -1) {
		return;
	}
	var data = url.substr(index+1);
	var reloadPage = false;
	var splitted = data.split("=");
	if(splitted[0] == "icon") {
		iconUrl = splitted[1];
		setIconCookie(iconUrl);
		reloadPage = true;
	}
	else if(splitted[0] == "weburl") {
		webUrl = splitted[1];
		setURLCookie(webUrl);
		reloadPage = true;
	}
	if(reloadPage == true){
		document.location = url.substring(0,index);
	}
}

function checkIconCookie() {
	var icon_url = getIconCookie();
	if(icon_url == "")
		return null;
	else 
		return icon_url;
}
 
var ip_is_set = false;

function setIPFields() {
	if(ip_is_set == true)
		return;
	var icon_ip = document.getElementById("icon_ip");
	var icon_url = getCookie("icon");
	if(icon_url == "") {
		icon_ip.setAttribute("onFocus","zap(this)");
		icon_ip.setAttribute("onTap","zap(this)");
		icon_url = "Enter Icon URL Here";
	}
	icon_ip.setAttribute("value",icon_url);
	
	var weburl_ip = document.getElementById("weburl_ip");
	var web_url = getCookie("weburl");
	if(web_url == "") {
		weburl_ip.setAttribute("onFocus","zap(this)");
		weburl_ip.setAttribute("onTap","zap(this)");
		web_url = "Enter Web URL Here";
	}
	weburl_ip.setAttribute("value",web_url);
	ip_is_set = true;
}

function zap(field) {
    if (! field.zapped ) {
        field.zapped = true;
        field.value  = "";
    }
    if (field.value) {
    	field.value = "";
    }
}

var output = null;

function clear_output() {
  if(output) {
    while(output.firstChild) {
      output.removeChild(output.firstChild);
    }
  }
}

function generate() {
	var weburl = document.getElementById("weburl_ip").value;
	//var weburl = weburl_ip.getAttribute("value");
	if(weburl == null || weburl == "Enter Web URL Here" || weburl == "") {
		alert("Please enter the valid web URL");
		return;
	}
	var iconurl = document.getElementById("icon_ip").value;
	//var iconurl = icon_ip.getAttribute("value");
	if(iconurl == null || iconurl == "Enter Icon URL Here" || iconurl == "") {
		alert("Please 	enter the valid icon URL");
		return;
	}
	//delete all the cookies
	//set the redirection
	document.location.href = "http://soft-use.com/webclipicon/redir.php?icon="+ iconurl + "&web=" + weburl ;
	//document.location.reload();
	return;
}

function loaded() {
	window.scrollTo(0,1);
}

window.addEventListener("load", function() {setTimeout(loaded, 100) }, false);