var obj_to_populate,obj_to_from,obj_to_subject,obj_to_msg,base_e_dir;

function leftTrim(sString){	
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);	
		}
	return sString;
	}
function rightTrim(sString){
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
		}
	return sString;
	}
function trimAll(sString)
{
	while (sString.substring(0,1) == ' ')
	{
	sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
	sString = sString.substring(0,sString.length-1);
	}
return sString;
}
/////////// prototype ajax functions 
function reportError(request)
{
	alert('error report'+ request.responseText);
}
function reportException(request)
{
	alert('error exception'+ request.responseText);
}
function getHeight() {
	var document_body = Screen.getBody();
	var innerHeight =(defined(self.innerHeight)&&!isNaN(self.innerHeight))?self.innerHeight:0;
	if (!document.compatMode || document.compatMode=="CSS1Compat") {
		var topMargin = parseInt(CSS.get(document_body,'marginTop'),10) || 0;
		var bottomMargin = parseInt(CSS.get(document_body,'marginBottom'), 10) || 0;
		return Math.max(document_body.offsetHeight + topMargin + bottomMargin,document.documentElement.clientHeight,document.documentElement.scrollHeight, Screen.zero(self.innerHeight));
		}
	return Math.max(document_body.scrollHeight, document_body.clientHeight,Screen.zero(self.innerHeight));
	}
function showLoader(request){
	//var layer = document.getElementById('preload_bg');
    //layer.style.height = getHeight();
	//layer.style.display = 'block';
    
	// show IFRAME
	//alert(layer.style.width);
	//alert(layer.style.height);
    var iframe = $('iframe_bg');
    iframe.style.display = 'block';
	iframe.style.height = getHeight()+'px';
	
    
	}
function destory_loader() {
    // hide IFRAME

    var iframe = $('iframe_bg');
    iframe.style.display = 'none';
    // hide layer

    /*var layer = document.getElementById('preload_bg');
    layer.style.display = 'none';*/
}
function send_ajax_request(url,params,success_method,error_method)
{
	var ajax = new Ajax.Request(url,
										{
											method: 'post',
											parameters: params,
											onSuccess: success_method,
											onFailure: error_method,
											onException: reportException,
											onLoading: showLoader
											/*onLoaded: showLoader,
											onInteractive: showLoader*/
											
										}
									)
}

function showdeadcenterdiv() {
// First, determine how much the visitor has scrolled

var scrolledX, scrolledY;
if( self.pageYoffset ) {
scrolledX = self.pageXoffset;
scrolledY = self.pageYoffset;
} else if( document.documentElement && document.documentElement.scrollTop ) {
scrolledX = document.documentElement.scrollLeft;
scrolledY = document.documentElement.scrollTop;
} else if( document.body ) {
scrolledX = document.body.scrollLeft;
scrolledY = document.body.scrollTop;
}
//alert("Srolled Y: "+scrolledY);
// Next, determine the coordinates of the center of browser's window

var centerX, centerY;
if( self.innerHeight ) {
centerX = self.innerWidth;
centerY = self.innerHeight;
} else if( document.documentElement && document.documentElement.clientHeight ) {
centerX = document.documentElement.clientWidth;
centerY = document.documentElement.clientHeight;
} else if( document.body ) {
centerX = document.body.clientWidth;
centerY = document.body.clientHeight;
}
//alert("Center Y: "+centerY);
// Xwidth is the width of the div, Yheight is the height of the
// div passed as arguments to the function:
var Xwidth=$('preloading').offsetWidth;
var Yheight=$('preloading').offsetHeight;
//alert(Xwidth);
//alert(Yheight);
//Xwidth=800;
//Xeight=800;
//alert("DIV HEIGHT: "+Yheight);
var leftoffset = scrolledX + (centerX - Xwidth) / 2;
var topoffset = scrolledY + (centerY - Yheight) / 2;
// The initial width and height of the div can be set in the
// style sheet with display:none; divid is passed as an argument to // the function
var o=document.getElementById('preloading');
var r=o.style;
r.position='absolute';
//alert(topoffset);
r.top = topoffset + 'px';
//r.left = leftoffset + 'px';
/*r.display = "block";*/
$('preloading').style.visibility=	"visible";
Effect.Appear('preloading');
} 

/////////// end /////////// prototype ajax functions 



function populate_message(obj_id,obj_id_to_populate){

	get_object(obj_id_to_populate).value=get_objectvalue(obj_id);

	}

function populate_template_messages(obj_id,obj_id_to_populate,base_dir,server_file){

	/*if(document.forms[0].radio[0].checked == true)

	obj_value=0;

	else if(document.forms[0].radio[1].checked == true)

	obj_value=1;

	else if(document.forms[1].radio[2].checked == true)

	obj_value=2;

	*/

	for(i=0; i<3; i++){

	if(document.frm_shopping_cart.message_tone[i].checked == true)

		obj_value=i;

	}

	obj_to_populate=obj_id_to_populate;

	select_obj=get_object(obj_to_populate);

	clear_option(obj_to_populate);

	xmlHttp = createAjaxObj(stateChanged_populate_template_messages);

	xmlHttp.open("POST",server_file,true);

	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	xmlHttp.send("message_tone="+obj_value+"&BASE_DIR="+base_dir);

	}

function stateChanged_populate_template_messages(){

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")

	{ 

		str=xmlHttp.responseText;

		arr_str_opt=str.split(":");

		i=0;

		while(arr_str_opt[i])

		{

			arr_opt_atr=arr_str_opt[i].split(";");

			new_value=arr_opt_atr[0];

			new_text=arr_opt_atr[1];

			add_new_option(obj_to_populate,new_text,new_value);

			i++;

		}

	populate_message('message_list','sms_message');

	}

	

	}	

function show_order_details(base_url,base_dir,server_file,order_number,obj_id){

	obj_to_populate=obj_id;
	xmlHttp = createAjaxObj(stateChanged_show_order_details);

	xmlHttp.open("POST",server_file,true);

	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	xmlHttp.send("obj_id="+obj_id+"&order_number="+order_number+"&BASE_DIR="+base_dir+"&BASE_URL="+base_url);



	}

function stateChanged_show_order_details(){

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){

		var str=xmlHttp.responseText;
		//alert(str);
		arr_str_opt=str.split("~");

		//obj_to_populate=arr_str_opt[0];

		data=arr_str_opt[1];
		
		
		
		$(obj_to_populate).innerHTML=data;
		//$(obj_to_populate).style.display='block';
		
		Effect.SlideDown(obj_to_populate);
		}

	

	}



function show_order_pins(base_url,base_dir,server_file,obj_id,order_number,product_id){

	
	obj_to_populate=obj_id;
	xmlHttp = createAjaxObj(stateChanged_show_order_pins);

	xmlHttp.open("POST",server_file,true);

	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	xmlHttp.send("obj_id="+obj_id+"&product_id="+product_id+"&order_number="+order_number+"&BASE_DIR="+base_dir+"&BASE_URL="+base_url);



	}

function stateChanged_show_order_pins(){

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){

		var str=xmlHttp.responseText;

		arr_str_opt=str.split("~");

		//obj_to_populate=arr_str_opt[0];

		data=arr_str_opt[1];

		get_object(obj_to_populate).innerHTML=data;

		Effect.SlideDown(obj_to_populate);
		}

	

	}



function del_item_shopping_cart(base_url,base_dir,server_file,shopping_cart_item_id){


	get_object('div_items_on_shopping_cart').style.display='none';
	xmlHttp = createAjaxObj(stateChanged_del_item_shopping_cart);

	xmlHttp.open("POST",server_file,true);

	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	xmlHttp.send("shopping_cart_id="+shopping_cart_item_id+"&BASE_DIR="+base_dir+"&BASE_URL="+base_url);



	

	

	}

function stateChanged_del_item_shopping_cart(){

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){

		var str=xmlHttp.responseText;

			arr_str_opt=str.split("~");

			err=arr_str_opt[0];

			msg=arr_str_opt[1];

			data=arr_str_opt[2];

			if(err!="none"){

			get_object('error_msg').innerHTML=err;

			get_object('ok_msg').innerHTML="";

			}

			if(msg!="none"){

			get_object('error_msg').innerHTML="";

			get_object('ok_msg').innerHTML=msg;

			}

		get_object('div_items_on_shopping_cart').innerHTML=data;
		Effect.BlindDown('div_items_on_shopping_cart');
		}

	

	}



function populate_products(obj_id,obj_id_to_populate,base_dir,server_file){

	 	obj_value=get_objectvalue(obj_id);

	 	obj_to_populate=obj_id_to_populate;

		

	select_obj=get_object(obj_to_populate);

	

	clear_option(obj_to_populate);

		

	

	xmlHttp = createAjaxObj(stateChanged_populate_products);

	xmlHttp.open("POST",server_file,true);

	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttp.send("category_id="+obj_value+"&BASE_DIR="+base_dir);

	

	}

function stateChanged_populate_products(){

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")

	{ 

		str=xmlHttp.responseText;
		arr_str_opt=str.split(":");

		i=0;

		while(arr_str_opt[i])

		{

			arr_opt_atr=arr_str_opt[i].split(";");

			new_value=arr_opt_atr[0];

			new_text=arr_opt_atr[1];

			add_new_option(obj_to_populate,new_text,new_value);

			i++;

		}

	}

	

	}

function populate_products_siteadmin(obj_id,obj_id_to_populate,base_dir,base_url){

	obj_value=$(obj_id).value;
	obj_to_populate=obj_id_to_populate;
	select_obj=get_object(obj_to_populate);
	clear_option(obj_to_populate);

	var url				=	base_url+"/include/ajax/php/populate_products_siteadmin.php";
	var params			=	"category_id="+obj_value+"&BASE_DIR="+base_dir;
		
	send_ajax_request(url,params,stateChanged_populate_products_siteadmin,reportError);
	

	

	}

function stateChanged_populate_products_siteadmin(request){

	
		//destory_loader();
		str=request.responseText;
		alert(str);
		arr_str_opt=str.split(":");

		i=0;

		while(arr_str_opt[i])

		{

			arr_opt_atr=arr_str_opt[i].split(";");

			new_value=arr_opt_atr[0];

			new_text=arr_opt_atr[1];

			add_new_option(obj_to_populate,new_text,new_value);

			i++;

		}

	

	

	}

function populate_states(obj_id,obj_id_to_populate,base_dir,server_file){

	 	obj_value=get_objectvalue(obj_id);

	 	obj_to_populate=obj_id_to_populate;

		

	select_obj=get_object(obj_to_populate);

	

	clear_option(obj_to_populate);

		

	

	xmlHttp = createAjaxObj(stateChanged_populate_states);

	xmlHttp.open("POST",server_file,true);

	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	xmlHttp.send("country_id="+obj_value+"&BASE_DIR="+base_dir);

	

	}

function stateChanged_populate_states(){

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")

	{ 

		str=xmlHttp.responseText;

		arr_response=str.split("#");

		arr_str_opt=arr_response[0].split(":");

		document.getElementById("dialing_code").value=arr_response[1];

		i=0;

		while(arr_str_opt[i])

		{

			arr_opt_atr=arr_str_opt[i].split(";");

			new_value=arr_opt_atr[0];

			new_text=arr_opt_atr[1];

			add_new_option(obj_to_populate,new_text,new_value);

			i++;

		}

	}

	

	}

function populate_face_values_country(obj_id,obj_id_to_populate,obj_id_to_populate2,base_dir,server_file){

	 obj_value=get_objectvalue(obj_id);

	 obj_to_populate=obj_id_to_populate;

	 obj_to_populate2=obj_id_to_populate2;

	clear_option(obj_to_populate);

		clear_option(obj_to_populate2);

	

	xmlHttp = createAjaxObj(stateChanged_populate_face_values_country);

	xmlHttp.open("POST",server_file,true);

	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	xmlHttp.send("product_id="+obj_value+"&BASE_DIR="+base_dir);

	

	}

function populate_face_values(obj_id,obj_id_to_populate,base_dir,server_file){

	 obj_value=get_objectvalue(obj_id);

	 obj_to_populate=obj_id_to_populate;

	 

	clear_option(obj_to_populate);

	

	

	xmlHttp = createAjaxObj(stateChanged_populate_face_values);

	xmlHttp.open("POST",server_file,true);

	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	xmlHttp.send("product_id="+obj_value+"&BASE_DIR="+base_dir);

	

	}



function stateChanged_populate_face_values(){

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")

	{ 

		str=xmlHttp.responseText;

		arr_str_opt=str.split(":");

		i=0;

		while(arr_str_opt[i])

		{

			arr_opt_atr=arr_str_opt[i].split(";");

			new_value=arr_opt_atr[0];

			new_text=arr_opt_atr[1];

			add_new_option(obj_to_populate,new_text,new_value);

			i++;

		}

	}

	

	}



function populate_face_values_siteadmin(obj_id,obj_id_to_populate,base_dir,server_file){

	 obj_value=get_objectvalue(obj_id);

	 obj_to_populate=obj_id_to_populate;

	 

	clear_option(obj_to_populate);

	

	

	xmlHttp = createAjaxObj(stateChanged_populate_face_values_siteadmin);

	xmlHttp.open("POST",server_file,true);

	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	xmlHttp.send("product_id="+obj_value+"&BASE_DIR="+base_dir);

	

	}



function stateChanged_populate_face_values_siteadmin(){

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")

	{ 

		str=xmlHttp.responseText;

		arr_str_opt=str.split(":");

		i=0;

		while(arr_str_opt[i])

		{

			arr_opt_atr=arr_str_opt[i].split(";");

			new_value=arr_opt_atr[0];

			new_text=arr_opt_atr[1];

			add_new_option(obj_to_populate,new_text,new_value);

			i++;

		}

	}

	

	}



function stateChanged_populate_face_values_country(){

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")

	{ 

		str=xmlHttp.responseText;

		arr_str=str.split("^");

		arr_str_opt=arr_str[0].split(":");

		arr_str_opt2=arr_str[1].split(":");

		i=0;

		

		while(arr_str_opt[i])

		{

			arr_opt_atr=arr_str_opt[i].split(";");

			new_value=arr_opt_atr[0];

			new_text=arr_opt_atr[1];

			add_new_option(obj_to_populate,new_text,new_value);

			i++;

		}

		i=0;

		while(arr_str_opt2[i])

		{

			arr_opt_atr=arr_str_opt2[i].split(";");

			new_value=arr_opt_atr[0];

			new_text=arr_opt_atr[1];

			add_new_option(obj_to_populate2,new_text,new_value);

			i++;

		}

	}

	

	}



function add_to_cart(base_url,base_dir,server_file){
	//get_object('div_items_on_shopping_cart').style.display='none';
	var product_id		=	$('product_list').value;
	var number_of_units	=	$('number_of_units').value;
	var send_sms		=	"No";
	var url				=	base_url+"/include/ajax/php/add_to_cart.php";
	var params			=	"";
	if($('deliver_sms').checked==true){
			send_sms	=	'Yes'
		if($('phone_number').value==''){
			alert("Please provide Recipient's GSM number");
			return false;
			}
		else{	
		var sender_name		=	$('sender_name').value;
		var sms_message		=	$('sms_message').value;
		var phone_number	=	$('phone_number').value;
		if(phone_number.charAt(0)=='0')
			phone_number=phone_number.substr(1);
		var	receiver_gsm	=	$('country_list').value+phone_number;	
		params				=	"product_id="+product_id+"&receiver_gsm="+receiver_gsm+"&sms_message="+sms_message+"&sender_name="+sender_name+"&send_sms="+send_sms+"&BASE_DIR="+base_dir+"&BASE_URL="+base_url+"&number_of_units="+number_of_units;	
		}
	}
	else{
		 params			=	"product_id="+product_id+"&send_sms="+send_sms+"&BASE_DIR="+base_dir+"&BASE_URL="+base_url+"&number_of_units="+number_of_units;	
	}
	
	
	send_ajax_request(url,params,stateChanged_add_to_cart,reportError);
	}


function stateChanged_add_to_cart(request){

	destory_loader(request);
		
		var str=request.responseText;
			//alert(str);
			arr_str_opt=str.split("~");

			err=arr_str_opt[0];

			msg=arr_str_opt[1];

			data=arr_str_opt[2];

			if(err!="none"){

			get_object('error_msg').innerHTML=err;

			get_object('ok_msg').innerHTML="";

			}

			if(msg!="none"){

			get_object('error_msg').innerHTML="";

			get_object('ok_msg').innerHTML=msg;

			}

		get_object('div_items_on_shopping_cart').innerHTML=data;
		Effect.BlindDown('div_items_on_shopping_cart');


	



}

function selected_value(obj_id1,obj_id2)

{

  	get_object(obj_id2).innerHTML=loopSelected(obj_id1,"need_text");

	hdn_obj=obj_id2+"_obj_id";

	get_object(hdn_obj).value=loopSelected(obj_id1,"need_value");

	hdn_obj=obj_id2+"_id";

	if(get_object(hdn_obj).value=="")

	{

		get_object(hdn_obj).value=loopSelected(obj_id1,"need_value");

	}

}

function clear_value(obj)

{

	get_object(obj).value="";

}

function loopSelected(obj_id,need)

{

  var selectedArray = new Array();

  var selObj = document.getElementById(obj_id);

  var i;

  var count = 0;

  

  for (i=0; i<selObj.options.length; i++) {

    if (selObj.options[i].selected) {

	   if(need=="need_text")

	   {

	   		selectedArray[count] = selObj.options[i].text;

	   }

	   else if(need=="need_value")

	   {

		  selectedArray[count] = selObj.options[i].value; 

	   }

      count++;

    }

	

  }

  return selectedArray;

}

function add_new_value(obj_to_from,obj_to_subject,obj_to_msg,from_text,subject_text,message_text)

{

	//select_obj=get_object(obj_id);

	//alert(get_object(obj_to_from));

	get_object(obj_to_from).value=from_text;

	get_object(obj_to_subject).value=subject_text;

	

	

	 	

	get_object(obj_to_msg).value=message_text;

	tinyMCE.updateContent(obj_to_msg);

	//alert(get_object(obj_to_msg).src);

	

}

function replace_email_text(string_text,find_str,replace_str)

{

				var str=string_text;

	 			var intIndexOfMatch = str.indexOf( find_str );

		 	// Loop over the string value replacing out each matching

		 	// substring.

		 		while (intIndexOfMatch != -1){

			 // Relace out the current instance.

				str = str.replace( find_str, replace_str );

			  

			// Get the index of any next matching substring.

				intIndexOfMatch = str.indexOf( find_str );

				}

				return str;

}



function add_new_option(obj_id,new_text,new_value)

{

		select_obj=get_object(obj_id);

		select_obj.options[select_obj.options.length]= new Option(new_text,new_value);

	

}

function clear_option(obj_id)

{

	

	

	select_obj=get_object(obj_id);

	

	for(i=0;i<select_obj.options.length;i++)

	{

		select_obj.options[i]=null;

	    

	}

select_obj.options.length=0;	



}
///////////////////////////// Shopping Cart /////////////////////////
var page_option;
var obj_base_url;
function add_product_to_cart(base_url,base_dir,product,quantity,option){
	
	 obj_base_url=base_url;
	if(option=='show'){
		var product_id		=	product;
		var number_of_units	=	$(quantity).value;
		page_option			=	option;
		var url				=	base_url+"/include/ajax/php/add_product_to_cart.php";
		var params			=	"product_id="+product_id+"&BASE_DIR="+base_dir+"&BASE_URL="+base_url+"&number_of_units="+number_of_units+'&option='+page_option;
		send_ajax_request(url,params,stateChanged_add_product_to_cart,reportError);
		}
	else{
		var product_id		=	$('product').value;
		var number_of_units	=	$('num_of_units').value;
		page_option			=	option;
		var send_sms		=	"No";
		var url				=	base_url+"/include/ajax/php/add_product_to_cart.php";
		var params			=	"";
		if($('deliver_sms').checked==true){
				send_sms	=	'Yes'
			if($('phone_number').value==''){
				alert("Please provide Recipient's GSM number");
				return false;
				}
			else{	
			var sender_name		=	$('sender_name').value;
			var sms_message		=	$('sms_message').value;
			var phone_number	=	$('phone_number').value;
			if(phone_number.charAt(0)=='0')
				phone_number=phone_number.substr(1);
			var	receiver_gsm	=	$('country_list').value+phone_number;	
			params				=	"product_id="+product_id+"&receiver_gsm="+receiver_gsm+"&sms_message="+sms_message+"&sender_name="+sender_name+"&send_sms="+send_sms+"&BASE_DIR="+base_dir+"&BASE_URL="+base_url+"&number_of_units="+number_of_units+'&option='+page_option;	
			}
		}
		else{
			 params			=	"product_id="+product_id+"&send_sms="+send_sms+"&BASE_DIR="+base_dir+"&BASE_URL="+base_url+"&number_of_units="+number_of_units+'&option='+page_option;	
		}
	
	
		send_ajax_request(url,params,stateChanged_add_product_to_cart,reportError);

		}	
	
	
	}
function stateChanged_add_product_to_cart(request){
	var response	=	request.responseText;
	if(page_option=='show'){
		$('preloading').innerHTML=response;
		$('preloading').style.display	=	"block";
		$('preloading').style.visibility=	"hidden";
		showdeadcenterdiv();
	}
	else if(page_option=='save'){
		$('preloading').innerHTML='';
		$('preloading').style.display='none';
		destory_loader(request);
		window.location=obj_base_url+'/index.php?m=shopping_cart_paypal&p=shopping_cart_paypal&msg='+response;
	}
	}
function close_pop_up(){
	Effect.Fade('preloading');
	destory_loader();
	}	
