// JavaScript Document
function $(id){
	return document.getElementById(id);
}
function show_login(){
	//error = 1;
	$('error_text').style.visibility = 'hidden';
	$('error_text').style.display = 'none';
	$('login_text').style.visibility = 'visible';
	$('login_text').style.display = '';
	$('error').style.left = 250 + 'px';
	$('error').style.top = 60 + 'px';
	$('error').style.width = '490px';
	$('error').style.visibility = 'visible';
	$('reg_username').focus();
}
function hide_error2(){
		$('error_text').style.visibility='hidden';
		$('login_text').style.visibility='hidden';
		$('error').style.visibility='hidden';
}
function get_nImage(i,arg_field,arg_folder){
	/*var oXmlHttp = zXmlHttp.createRequest();
	var url = "/button/next_image.php?field="+arg_field+"&folder="+arg_folder+"&limit="+i;
	//alert(url);
	oXmlHttp.open("GET", url, true);
	
	oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.status == 200) {
				if(oXmlHttp.responseText!='no'){
					var text = oXmlHttp.responseText.split('<>');
					for(var j=0; j<text.length; j++){
						var data = text[j].split('|');
						ss_img_arr[i] = new Image();
						if((data[0].length)>0)
						{
						ss_img_arr[i].src = data[0];
						ss_link_arr[i] = data[1];
						ss_txt_arr[i] = data[2];
						}
						else
						{
							continue;
						}
						i++;
					}
					get_nImage(i,arg_field,arg_folder);
				}
			} else {
				throw new Error('An error occured');
			}
		}	
	};
	oXmlHttp.send(null);*/
}	
function AddToDailyImage(arg_folder){
	if(!($('getdailyimage').value.indexOf(".") > 2) || !($('getdailyimage').value.indexOf("@") > 0)){
		alert('Enter a valid email address');
		$('getdailyimage').focus();
		return false;
	}else{
		$('getdailyimage').style.display = 'none';
		$('dailyimage').innerHTML = 'Please wait ......';
		$('dailyimage').style.display = '';
		var oXmlHttp = zXmlHttp.createRequest();
		var url = "/button/daily_image.php?email=" + $('getdailyimage').value +"&folder="+arg_folder;
		oXmlHttp.open("GET", url, true);
		
		oXmlHttp.onreadystatechange = function () {
			if (oXmlHttp.readyState == 4) {
				if (oXmlHttp.status == 200) {
					$('dailyimage').innerHTML = oXmlHttp.responseText;
					window.setTimeout('reset_daily_image()', 2000);
				} else {
					throw new Error('An error occured');
				}
			}	
		};
		oXmlHttp.send(null);
	}
	return true;
}
function AddToDailyImage_new(arg_folder){
	if(!($('getdailyimage').value.indexOf(".") > 2) || !($('getdailyimage').value.indexOf("@") > 0)){
		alert('Enter a valid email address');
		$('getdailyimage').focus();
		return false;
	}else{
		$('getdailyimage').style.display = 'none';
		$('dailyimage').innerHTML = 'Please wait ......';
		$('dailyimage').style.display = '';
		var oXmlHttp = zXmlHttp.createRequest();
		var url = "/button/daily_image_new.php?email=" + $('getdailyimage').value +"&folder="+arg_folder;
		oXmlHttp.open("GET", url, true);
		
		oXmlHttp.onreadystatechange = function () {
			if (oXmlHttp.readyState == 4) {
				if (oXmlHttp.status == 200) {
					$('dailyimage').innerHTML = oXmlHttp.responseText;
					window.setTimeout('reset_daily_image()', 2000);
				} else {
					throw new Error('An error occured');
				}
			}	
		};
		oXmlHttp.send(null);
	}
	return true;
}
function reset_daily_image(){
	$('dailyimage').style.display = 'none';
	$('getdailyimage').value = '';
	$('getdailyimage').style.display = '';
}
function init_slideshow(){
	setOpacity($('ssi'), 100);
	fadeOut('ssi',100);
}
function initImage() {
	imageId = 'ssi';
	image = $(imageId);
	setOpacity(image, 0);
	//image.style.visibility = 'visible';
	fadeIn(imageId,0);
}
function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}
function fadeIn(objId,opacity) {
	if ($) {
		obj = $(objId);
		if (opacity <= 100) {
			setOpacity(obj, opacity);
			opacity += 5;
			window.setTimeout("fadeIn('"+objId+"',"+opacity+")", fadeInTime);
		}else{
			window.setTimeout("init_slideshow()", displayTime);
		}
	}
}
function fadeOut(objId,opacity) {
	if ($) {
		obj = $(objId);
		if (opacity >= 0) {
			setOpacity(obj, opacity);
			opacity -= 5;
			window.setTimeout("fadeOut('"+objId+"',"+opacity+")", fadeOutTime);
		}else{
			//obj.style.visibility = 'hidden';
			if(ss_img_arr.length==curr_img){
				curr_img = 0;
			}	
			obj.src = ss_img_arr[curr_img].src;
			initImage();
			$('ccd').innerHTML = "<h2>"+ss_txt_arr[curr_img]+"</h2>";
			$('ssl').href = ss_link_arr[curr_img];
			if(typeof sIFR == "function"){
				sIFR.replaceElement("h2", "/template/fontname.swf", "#CACACA", null, null, null, 0, 0, 0, 0);
			};
			curr_img++;
			
		}
	}
}
function submit_login1(){
	if($('reg_username').value==''){
		alert('Please enter your username');
		$('reg_username').focus();
		return false;
	}
	if($('reg_pass').value==''){
		alert('Please enter your password');
		$('reg_pass').focus();
		return false;
	}
	var oXmlHttp = zXmlHttp.createRequest();
	var login_content = "reg_username=" + escape($('reg_username').value) + "&reg_pass=" + escape($('reg_pass').value);
	var url = "/button/submit_login.php";
	oXmlHttp.open("POST", url);
	oXmlHttp.onreadystatechange = function(){
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.status == 200) {
				if(oXmlHttp.responseText=='fail'){
					get_error_text('Invalid login.');
					}
				else{
					if($('tr1'))
					$('tr1').style.display='';
					if($('tr2'))
					$('tr2').style.display='none';
					show_error1('Login Successfull. You can continue with your session.&nbsp;&nbsp;&nbsp;<a onclick="hide_error2();" style="cursor:pointer;" title="Close">X</a>');
				}
			} else {
				throw new Error('Cannot login.');
			}
		}	
	};
	
	oXmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
	oXmlHttp.send(login_content);
}
function show_error1(err_txt){
	//alert('hi');
	//var position1  = findPos($("make_image"));
	$('login_text').style.visibility = 'hidden';
	//alert('hi1');	
	$('login_text').style.display = 'none';
	//alert('hi2');	
	$('error_text').style.visibility = 'visible';
	$('error_text').style.display = '';
	$('error_text').innerHTML = err_txt;
	$('error').style.left = 250 + 'px';
	$('error').style.top = 60 + 'px';
	$('error').style.width = '470px';
	$('error').style.visibility = 'visible';
	//error = 0;
	window.setTimeout("hide_error2();", 5000);
	
}
function get_error_text(text){
	show_error1(text+' Click <a onclick="show_login();" style="cursor:pointer;" title="Login">here</a> to login.&nbsp;&nbsp;Or&nbsp;&nbsp<a href="https://www.imagesofasia.com/login/forget_password.php" style="color:#4b4b4b; text-decoration:underline;" title="Forget Password">Forget Password?</a>&nbsp;&nbsp;&nbsp;<a onclick="error=0;hide_error();" style="cursor:pointer;" title="Close">X</a>');
}
function readCookie(name) {
	var nameEQ = name + "=";
	//alert(document.cookie);
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		//alert(c);
		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 findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
function set_login()
{
	link_pos  = findPos($('img_short'));
	dlink1=document.getElementById('main_login');
	dlink1.style.left = ((link_pos[0])-60 )+ 'px';
	dlink1.style.top = (link_pos[1])+parseInt(1)+ 'px';
	//alert('gg');
	dlink2=document.getElementById('sub_login');
	dlink2.style.left = ((link_pos[0])-60)+ 'px';
	dlink2.style.top = (link_pos[1])+parseInt(1)+ 'px';
}
function set_login1()
{
	link_pos  = findPos($('img_short'));
	//alert('gg');
	dlink2=document.getElementById('sub_login');
	dlink2.style.left = ((link_pos[0])-60 )+ 'px';
	dlink2.style.top = (link_pos[1])+parseInt(1)+ 'px';
	//dlink2.style.z-index='10';	
	
	dlink1=document.getElementById('main_login');
	dlink1.style.left = ((link_pos[0])-60 )+ 'px';
	dlink1.style.top = (link_pos[1])+parseInt(1)+ 'px';	
	//dlink1.style.z-index=100;
}
function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
	var oXmlHttp = zXmlHttp.createRequest();
	var url = "/button/cart_clear.php";
	oXmlHttp.open("GET", url, true);
	
	oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.status == 200) {
				  window.location='/index.php';
			} else {
				alert("An error occurred: " + oXmlHttp.statusText); //statusText is not always accurate
			}
		}	
	};
	oXmlHttp.send(null);  
}
