// remote scripting library
// (c) copyright 2005 modernmethod, inc
var sajax_debug_mode = false;
var sajax_request_type = "GET";
var fsms_ajax_response = '1';
var __SITE_URL__  = 'http://localhost/myworks/greenhost/';
/**
* if sajax_debug_mode is true, this function outputs given the message into 
* the element with id = sajax_debug; if no such element exists in the document, 
* it is injected.
*/
 function os_getElementPosition(elemID){
			var offsetTrail = document.getElementById(elemID);
			var offsetLeft = 0;
			var offsetTop = 0;
			while (offsetTrail){
				offsetLeft += offsetTrail.offsetLeft;
				offsetTop += offsetTrail.offsetTop;
				offsetTrail = offsetTrail.offsetParent;
			}
			if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined'){
				offsetLeft += document.body.leftMargin;
				offsetTop += document.body.topMargin;
			}
			return {left:offsetLeft,top:offsetTop};
	}

function create_div(div_id) {
	var c = document.createElement("div");
	//var s = document.getElementById(r);
	//var pos = os_getElementPosition(r);
	//alert(window.screen.width);	
	
	var left = window.screen.width/3;
	var top = window.screen.height/5;
	c.style.border = "0px solid #999999";
	c.style.background = "#660066";
	c.style.position = 'absolute';
	//c.style.height = '200px'
	c.style.padding = '20px'
	c.style.width = '400px'
	c.style.top = top+'px';
	c.style.left = left+'px';
	c.style.display = 'none';
	//c.setAttribute("id", );	
	document.body.appendChild(c);
	
	c.innerHTML = '<div id="'+div_id+'" width="100%" height="100%" style="position:relative; padding:10px; background:#ffffff;"></div>'; 
	 Effect.Appear(c);
	 //Effect.Grow(c);
	 return c;
}

function set_content(container, thecontent) {
	document.getElementById(container).innerHTML = thecontent;
}

function parseXML (xml) {
			var resp = new Array;
			msg = xml.getElementsByTagName('msg').item(0);
			//error=xml.getElementsByTagName('error').item(0);
			resp['msg']=msg.childNodes.item(0).data;
			//resp['error']=error.childNodes.item(0).data;
			return resp;
}
function print_r(x, max_, sep, l) {   
   
     l = l || 0;   
     max_ = max_ || 10;   
     sep = sep || ' ';   
   
     if (l > max_) {   
         return "[WARNING: Too much recursion]\n";   
     }   
   
     var  
         i,   
         r = '',   
         t = typeof x,   
         tab = '';   
   
     if (x === null) {   
         r += "(null)\n";   
     } else if (t == 'object') {   
   
         l++;   
   
         for (i = 0; i < l; i++) {   
             tab += sep;   
         }   
   
         if (x && x.length) {   
             t = 'array';   
         }   
   
         r += '(' + t + ") :\n";   
   
         for (i in x) {   
             try {   
                 r += tab + '[' + i + '] : ' + print_r(x[i], max_, sep, (l + 1));   
             } catch(e) {   
                 return "[ERROR: " + e + "]\n";   
             }   
         }   
   
     } else {   
   
         if (t == 'string') {   
             if (x == '') {   
                 x = '(empty)';   
             }   
         }   
   
         r += '(' + t + ') ' + x + "\n";   
   
     }   
   
     return r;   
   
 }

select_all=false;
  function select_all_a() {
  	ii=1;
	if(select_all == 'undefined' || select_all == false) {
		while(true) {
			if(document.getElementById('sel_'+ii) != null) {
				
					document.getElementById('sel_'+ii).checked=true;
					//document.getElementById('selll').innerHTML = 'Deselect All';
					
			}else break;			
			
			ii++;
		}
		ii=1;
		select_all = true;
		$('do_select_all').checked=true;
	}else{
		while(true) {
			if(document.getElementById('sel_'+ii) != null) {
					document.getElementById('sel_'+ii).checked=false;
					//document.getElementById('selll').innerHTML = 'Select All';
					//select_all = false;
				
			}else break;
			ii++;
		}
		ii=1;
		select_all = false;
		$('do_select_all').checked=false;
	  }
  }
function mydo_sms_count(ids, textfield) {
		var MAX_LEN = 5000;
			var len =  parseInt(ids.length);
			//alert(len);
			$('sms_text_count').innerHTML = MAX_LEN-len;
			if(parseInt($('sms_text_count').innerHTML) > 0)  {
				//$('sms_text_count').innerHTML = parseInt($('sms_text_count').innerHTML)-len;
			}else{
				$('sms_text_count').innerHTML=0;
				//alert(textfield.value.substring(0, MAX_LEN));
				textfield.value=textfield.value.substring(0, MAX_LEN);
			}
}
function ajaxRequest(fx, param, success_fx_to_call, failure_fx_to_call, loader) {
	 //if(isNaN('a')) alert ("femi");
	 	
 whn=new Date();
 url='ajax.php?fx='+fx
 pars=param+'&tim='+whn;
 //$(loader).style.display='';
 var appr=new Ajax.Request(url, {method:'post', parameters:pars, onLoading:function() { showloading(loader); }, onFailure:failure_fx_to_call, onSuccess:success_fx_to_call
																									});
	//$(loader).style.display='none';
 }
 function showloading (loader) {
	 $(loader).style.display='';
 }
 function hideloading (loader) {
	 $(loader).style.display='none';
 }
 function json_decode (json) {
	 try{
		 return eval("femi="+json);
	 }catch(e) {
		throw e; 
	 }
	 
 }
 function change_selected_plan(v) {
	 ajaxRequest('change_selected_plan', 'plan='+v, function(txt) {
															// alert(txt.responseText);
															 hideloading ('indicator3');													  
															try{
																resp=json_decode(txt.responseText);
																//alert(txt.responseText);
																$('plan_price').innerHTML=parseFloat(resp.Price_per_Year).toFixed(2);
																//update_plan_table(resp);	
																//alert(resp);
															}catch(e) {
																$('plan_price').innerHTML=parseFloat(0.00).toFixed(2);
																//alert('Unknown error occured');
															}
																	 },
																	 function ()  {
																		 hideloading ('indicator');
																		 alert('Could not connect to server. Please check your internet connection');
																	 }, 'indicator3');
 }
 function do_login(email, pword) {
	if(email.length==0) {
		alert('You must supply your email address');
		return;
	}
	if(pword.length==0) {
		alert('You must supply your password');
		return;
	}
	if($('login_but') != null) $('login_but').disabled=true;
	ajaxRequest('do_login', 'email='+email+'&password='+pword, function(txt) {
																		//alert(txt.responseText);
																		hideloading ('indicator');
																  try{
																resp=json_decode(txt.responseText);
																window.open((resp.redirect.length==0)?'cp.php':resp.redirect, '_self');
															}catch(e) {
																if(txt.responseText=='<i>') alert('Invalid email address or password!');
																else{
																	alert('Internal error. Please check your internet connection');
																	
																}	
															}
																  },
																  function () {
																	  hideloading ('indicator');
																	 // alert(txt.responseText);
																	 alert('Could not connect to server. Please check your internet connection');
																  }, 'indicator');
	if($('login_but') != null) $('login_but').disabled=false;
 }
 function do_register() {
	 email = trim($('email').value);
	 pword =  trim($('password').value);
	 pword2 = trim($('password2').value);
	fname = trim($('firstname').value);
	 lname = trim($('lastname').value);
	phone = trim($('phonenumber').value);
	if(email.length==0) {
		alert('You must supply your email address');
		return;
	}
	if(pword.length==0) {
		alert('You must supply your password');
		return;
	}
	if(pword.length < 6) {
		alert('Password must be at least 6 characters');
		return;
	}
	if(pword != pword2) {
		alert('Password do not match'); return;	
	}
	if(fname.length==0) {
		alert('You must supply your first name');
		return;
	}
	if(lname.length==0) {
		alert('You must supply your last name (surname)');
		return;
	}
	if(phone.length==0) {
		alert('You must supply your phone number');
		return;
	}
	//if($('register_but') != null) $('register_but').disabled=true;
	ajaxRequest('do_register', 'email='+email+'&password='+pword+'&password2='+pword2+'&fname='+fname+'&lname='+lname+'&phone='+phone, function(txt) {
																		//alert(txt.responseText);
																		hideloading ('indicator');
																  try{
																resp=json_decode(txt.responseText);
																window.open((resp.redirect.length==0)?'cp.php':resp.redirect, '_self');
															}catch(e) {
																if(txt.responseText=='<0>') alert('One or more fields are empty!');
																else if(txt.responseText=='<1>') alert('Email already exists in our database. Please use another!');
																else if(txt.responseText=='<2>') alert('An error occured. Please try again!');
																else if(txt.responseText=='<4>') alert('Password must be at least 6 characters long');
																else if(txt.responseText=='<3>') alert('Phone number is invalid!');
																
																else{
																	alert('Internal error. Please check your internet connection');
																	
																}	
															}
																  },
																  function () {
																	  hideloading ('indicator');
																	 // alert(txt.responseText);
																	 alert('Could not connect to server. Please check your internet connection');
																  }, 'indicator');
	$('register_but').disabled=false;
 }
 function do_update() {
	 email = trim($('email').value);
	 cp = trim($('current_password').value);
	 pword =  trim($('password').value);
	 pword2 = trim($('password2').value);
	fname = trim($('firstname').value);
	 lname = trim($('lastname').value);
	phone = trim($('phonenumber').value);
	if(email.length==0) {
			alert('You must supply your email address');
			return;
		}
		
	
	if(fname.length==0) {
		alert('You must supply your first name');
		return;
	}
	if(lname.length==0) {
		alert('You must supply your last name (surname)');
		return;
	}
	if(phone.length==0) {
		alert('You must supply your phone number');
		return;
	}
	if(cp.length == 0) {
		alert('You must supply your current password to save changes');
		return;
	}
	if(pword.length > 0) {
		if(cp.length == 0) {
			alert('You must supply your current password to change it');
			return;
		}
		if(pword.length < 6) {
			alert('Password must be at least 6 characters');
			return;
		}
		if(pword != pword2) {
			alert('Password do not match'); return;	
		}
	}
	//if($('register_but') != null) $('register_but').disabled=true;
	ajaxRequest('do_update', 'email='+email+'&cp='+cp+'&password='+pword+'&password2='+pword2+'&fname='+fname+'&lname='+lname+'&phone='+phone, function(txt) {
																		//alert(txt.responseText);
																		hideloading ('indicator');
																  try{
																resp=json_decode(txt.responseText);
																//window.open((resp.redirect.length==0)?'cp.php':resp.redirect, '_self');
																window.open(window.location.href, '_self');
																alert('Changes saved');
															}catch(e) {
																if(txt.responseText=='<0>') alert('One or more fields are empty!');
																else if(txt.responseText=='<1>') alert('Email already exists in our database. Please use another!');
																else if(txt.responseText=='<2>') alert('An error occured. Please try again!');
																else if(txt.responseText=='<4>') alert('Password must be at least 6 characters long');
																else if(txt.responseText=='<3>') alert('Phone number is invalid!');
																else if(txt.responseText=='<5>') alert('Current password supplied is incorrect');
																else{
																	alert('Internal error. Please check your internet connection');
																	
																}	
															}
																  },
																  function () {
																	  hideloading ('indicator');
																	 // alert(txt.responseText);
																	 alert('Could not connect to server. Please check your internet connection');
																  }, 'indicator');
	$('register_but').disabled=false;
 }
 function domain_price(domain, duration, hosting, domain_for) {
	  $('place_order').disabled=true;
	ajaxRequest('domain_price', 'domain='+domain+'&duration='+duration+'&hosting='+hosting+'&domain_for='+domain_for, function (txt) {
																									//  alert(txt.responseText);
																									hideloading ('indicator');
																									   try{
																	resp=json_decode(txt.responseText);
																	$('domain_price').innerHTML=resp.price;
																	$('rawDomainPrice').value=resp.rawPrice;
																	order_total();
																									   }catch(e) {
																if(txt.responseText=='<i>') alert('Invalid domain name specified!');
																else{
																	alert('Internal error. Please check your internet connection');
																	
																}	
															}
																									  }, 
																									  function () {
																										   // alert(txt.responseText);
																	 alert('Could not connect to server. Please check your internet connection');
																										  hideloading ('indicator');
																									  }, 'indicator');
	 $('place_order').disabled=false;
 }
 function order_total() {
	 dom_price=0;
	 host_price=0;
	 if(!isNaN(parseFloat($('rawHostingPrice').value))) host_price = parseFloat($('rawHostingPrice').value)
	 if(!isNaN(parseFloat($('rawDomainPrice').value))) dom_price = parseFloat($('rawDomainPrice').value)
	 
	totalHost = dom_price+host_price;
	//alert(parseFloat(totalHost).toFixed(2));
	$('totalPrice').innerHTML=parseFloat(totalHost).toFixed(2);
 }
 function hosting_price(duration) {
	 $('place_order').disabled=true;
	ajaxRequest('hosting_price', 'duration='+duration, function (txt) {
													try{
													//	alert(txt.responseText);
													
													 hideloading ('indicator');
																	resp=json_decode(txt.responseText);
																	$('hosting_price').innerHTML=resp.price;
																	$('rawHostingPrice').value=resp.rawPrice;
																	order_total();
																									   }catch(e) {
																if(txt.responseText=='<i>') alert('Invalid hosting specified!');
																else{
																	alert('Internal error. Please check your internet connection');
																	
																}	
															}
																									  },
																									  
																									  ajaxFailure, 'indicator');
	 $('place_order').disabled=false;
 }
 function delete_domain(oid) {
	if(confirm('Are you sure you want to delete this domain from your record?')) {
		if(delete_product(oid)==true) $('row'+oid).style.display='none';
	}
	
 }
 function delete_hosting(oid) {
	if(confirm(' All sites within this plan will be deleted after 2 weeks. Are you sure you want to terminate this hosting account?')) {
		if(delete_product(oid)==true) $('row'+oid).style.display='none';
	}
	
 }
 function delete_product(order_id) {
	 //$('orderBut').disabled=true;
	// $('orderloader').style.display='';
	 ajaxRequest('delete_product', 'oid='+order_id, function (txt) {
												try{
														//alert(txt.responseText.length);
													//alert(txt.responseText);
																	resp=json_decode(txt.responseText);
																
												}catch(e) {
													alert('Internal error. Please check your internet connection');
													return false;
												}
												//alert('row'+oid);
												alert('Product deleted successfully');
												if($('row'+order_id) != null) {
													$('row'+order_id).style.display='none';
													return true;
												}else{
												
																	//$('htmlDiv').style.display='none';
																	window.open(window.location.href, '_self');
																	return true;
												}
																	//order_total();
																	// hideloading ('orderloader');
																									 //  }catch(e) {
																										  // alert(e.message);
																if(txt.responseText.length==0) alert('Could not connect to server. Please try again');
																else if(txt.responseText=='<i>' || txt.responseText=='<j>') alert('Your session has expired. Please log in!');
																else if	(txt.responseText=='<k>') alert('Technical error. Please try again');
																else if	(txt.responseText=='<l>') alert('Either your session has expired or you do not have access to this order. Please try again!');
																else{
																	//alert('Internal error. Please check your internet connection');
																	alert(txt.responseText);
																	//return;
																}	
																hide_cover();
															
																									  }, 
																									  ajaxFailure, 'orderloader');
	// $('orderBut').disabled=false;
 }
 function placeOrder(domain, dom_duration, host_plan, host_duration, domain_for, type) {
	 $('place_order').disabled=true;
	 show_cover();
	// alert(type);
	// alert(domain_for);
	ajaxRequest('place_order', 'domain='+domain+'&dom_duration='+dom_duration+'&host_plan='+host_plan+'&host_duration='+host_duration+'&domain_for='+domain_for+'&type='+type, function (txt) {
															 hideloading ('indicator');																	 
													try{
														//alert(txt.responseText);
																	//resp=json_decode(txt.responseText);
																	show_html(txt.responseText);
																	//order_total();
																									   }catch(e) {
																										   //alert(e.message);
																if(txt.responseText=='<i>') alert('Your session has expired. Please log in!');
																if(txt.responseText=='<k>') { 
																alert('Transaction is invalid'); window.open('cp.php', '_self');
																}
																else{
																	alert('Internal error. Please check your internet connection');
																	
																}	
															}
																									  }, 
																									  ajaxFailure, 'indicator');
	 $('place_order').disabled=false;
 }
 function process_order(order_id) {
	 $('orderBut').disabled=true;
	 $('orderloader').style.display='';
	 $('orderloader').style.margin='10px';
	// alert(order_id);
	 ajaxRequest('process_order', 'oid='+order_id, function (txt) {
												try{
														//alert(txt.responseText.length);
														//alert("Resp="+txt.responseText);
																	resp=json_decode(txt.responseText);
																	alert('Order processed successfully');
																	try{
																		$('htmlDiv').style.display='none';
																		}catch(e) {
																		}
																	window.open('cp.php?sp=invoice', '_self');
																	return;
												}catch(e) {
													alert('Internal error. Please check your internet connection');
													 $('orderBut').disabled=false;
													return;
												}
																	//order_total();
																	 $('orderBut').disabled=false;
																	 hideloading ('orderloader');
																									 //  }catch(e) {
																										  // alert(e.message);
																if(txt.responseText.length==0) alert('Could not connect to server. Please try again');
																else if(txt.responseText=='<i>' || txt.responseText=='<j>') alert('Your session has expired. Please log in!');
																else if	(txt.responseText=='<k>') alert('Technical error. Please try again');
																else if	(txt.responseText=='<l>') alert('Either your session has expired or you do not have access to this order. Please try again!');
																else{
																	//alert('Internal error. Please check your internet connection');
																	alert(txt.responseText);
																	//return;
																}	
																hide_cover();
																
															
																									  }, 
																									  ajaxFailure, 'orderloader');
	// $('orderBut').disabled=false;
 }
 function renew(order_id, duration) {
	 $('place_order').disabled=true;
	 show_cover();
	ajaxRequest('renew', 'duration='+duration+'&oid='+order_id, function (txt) {
													//try{
														//alert(txt.responseText.length);
													//	alert(txt.responseText);
																	//resp=json_decode(txt.responseText);
																	
																	//order_total();
																	 hideloading ('indicator');
																									 //  }catch(e) {
																										  // alert(e.message);
																if(txt.responseText.length==0) alert('Could not connect to server. Please try again');
																else if(txt.responseText=='<i>' || txt.responseText=='<j>') alert('Your session has expired. Please log in!');
																else if	(txt.responseText=='<k>') alert('Technical error. Please try again');
																else if	(txt.responseText=='<l>') alert('Either your session has expired or you do not have access to this order. Please try again!');
																else{
																	//alert('Internal error. Please check your internet connection');
																	show_html(txt.responseText);
																	return;
																}	
																hide_cover();
															
																									  }, 
																									  ajaxFailure, 'indicator');
	 $('place_order').disabled=false;
 }
 var ajaxFailure = function () {
	  hideloading ('indicator');
	 alert('Could not connect to server. Please check your internet connection');
 }
function check_domain_availabilty(sv) {
	if(sv.length <= 4 || sv=='example.com') {
		//alert(sv);
		alert('Enter a valid domain name e.g. mydomain.com');
		if($('domain').value != null) $('domain').value='';
		return;
	}
	//alert(sv);
	//$('indicator2').style.display='';
	ajaxRequest('check_domain_availabilty', 'domain='+sv, function(txt) {
																    hideloading ('indicator2');
																	//alert(txt.responseText);
																  try{
																$('check_domain_form').innerHTML=txt.responseText;
															}catch(e) {
																if(txt.responseText=='<i>') alert('Domain name supplied is invalid');
																
															}
															
																  },
																  function () {
																	 // alert(txt.responseText);
																	  hideloading ('indicator2');
																	 alert('Could not connect to server. Please check your internet connection');
																  }, 'indicator2');
	//$('login_but').disabled=false;
}
function show_cover() {
	//alert('sdf');
	var objBody = document.getElementsByTagName("body").item(0);
	//alert(objBody.id);
		var objOverlay = document.createElement("div"); objOverlay.setAttribute('id','overlay'); 
		var objInnerlay = document.createElement("div"); objOverlay.setAttribute('id','innerlay'); 
		
		objInnerlay.style.backgroundColor='#000000';
		objInnerlay.style.height='100%';
		objInnerlay.style.width='100%';
		
		objInnerlay.style.filter = 'Alpha(Opacity=30, FinishOpacity=70)';
		objInnerlay.style.mozOpacity='0.3';
		objInnerlay.style.opacity='0.3';
		
		objOverlay.style.position='fixed';
		objOverlay.style.width='100%';
		objOverlay.style.height='100%';
		//objOverlay.style.backgroundColor='#fff';
		objOverlay.style.left='0px';
		objOverlay.style.top='0px';
		objOverlay.style.display='none';
		objOverlay.appendChild(objInnerlay);
		objBody.appendChild(objOverlay);	
		
		//objInnerlay.style.display='';
		Effect.Appear('innerlay',{duration:1});
		//$('overlay').style.zIndex=10000;
		//$('myaboki_page_cover').style.left='0px';
		//do_rem=setInterval("hide_notification()", 20000);
	}
	function show_html(html) {
		var objBody = document.getElementsByTagName("body").item(0);
	//alert(objBody.id);
	if($('htmlDiv') == null) {
		var htmlDiv = document.createElement("div"); htmlDiv.setAttribute('id','htmlDiv'); 
		var left = window.screen.width/4;
	var top = window.screen.height/20;
	
	htmlDiv.style.position='absolute';
	htmlDiv.style.top=top+'px';
	htmlDiv.style.left=left+'px';
	htmlDiv.style.zIndex='101';
	htmlDiv.style.width='50%';
	htmlDiv.style.background='#ffffff';
	htmlDiv.style.border='5px solid #666';
	htmlDiv.style.padding='8px';
	htmlDiv.style.minHeight='150px';
	}else{
		htmlDiv = $('htmlDiv');
	}
		htmlDiv.innerHTML=html;
		objBody.appendChild(htmlDiv);
	}
	function hide_cover() {
		//document.getElementById('notification_span').innerHTML = msg;
		if($('htmlDiv')!=null) {
			
			Effect.Fade('htmlDiv',{duration:1});
			var objBody = document.getElementsByTagName("body").item(0);
			objBody.removeChild($('htmlDiv'));
		}
		if($('innerlay') != null) Effect.Fade('innerlay',{duration:1});
		
		//do_rem=setInterval("hide_notification()", 20000);
	}
	function renew_update_price(dur) {
		total = parseFloat(dur)*parseFloat($('renew_price').value);
		$('plan_price').innerHTML = parseFloat(total).toFixed(2);
		//alert(total.toFixed(2));
	}
	function trim(val) {
		if(val.substr(0, 1) == ' ')  {
		vallen= val.length;
			val = val.substr(1, vallen);
			//val = trim(val);
		}
		vallen= val.length;
		//alert(val.substr(vallen-1, 1));
		if(val.substr(vallen-1, 1) == ' ')  {
			val = val.substr(0, vallen-1);
			//return trim(val);
		}
		if(val.substr(0, 1) == ' ') {
			//alert('yes');
			return trim(val);
		}
		vallen= val.length;
		if(val.substr(vallen-1, 1) == ' ') {
			return trim(val);
			}
			return val;	
	}
