var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
		return this;
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

var browser = BrowserDetect.init();

function JSConfirm( question, redirectUrl )
{
	if( confirm( question ) )
	{
		window.location = redirectUrl;
	}
}

function changeToForgotPass() {
	jQuery("#loginfields").slideUp(200);
	jQuery("#forgotfields").slideDown(200);
	jQuery("#logforg").html('Forgot Password');
	return;
}

function showForgotPass() {
	jQuery("#loginfields").hide();
	jQuery("#forgotfields").show();
	jQuery("#logforg").html('Forgot Password');
	return;
}

function changeToLogin() {
	jQuery("#forgotfields").slideUp(200);
	jQuery("#loginfields").slideDown(200);
	jQuery("#logforg").html('Login');

	return;
}


function showHide(val) {
	if(val==1 || val==2) {		
		jQuery("#additionalfld4").fadeOut("slow");
		jQuery("#contsend").fadeIn("slow");
		jQuery("#additionalfld").fadeIn("slow");
	}else if(val==4) {		
		jQuery("#additionalfld").fadeOut("slow");
		jQuery("#additionalfld4").fadeIn("slow");		
		jQuery("#contsend").fadeIn("slow");		
	}else{
		jQuery("#additionalfld4").fadeOut("slow");
		jQuery("#additionalfld").fadeOut("slow");
		jQuery("#contsend").fadeIn("slow");
	}
	return;
}

function bookmarkProduct(url, productCode, bookmark)
{
	action_url=url+"productCode/"+productCode+"/bookmark/"+bookmark+"/";

	jQuery.ajax({
		   type: "POST",
		   url: action_url,
		   dataType: 'xml',
		   beforeSend: function(){
  		},
	   success: function(xml){

	   var old_product_class=jQuery(xml).find('old_product_class').text();
	   var new_product_class=jQuery(xml).find('new_product_class').text();
	   var my_products_html=jQuery(xml).find('my_products_html').text();

	   jQuery("#productDiv"+productCode).removeClass(old_product_class).addClass(new_product_class);
	   jQuery("#myProductDiv"+productCode).removeClass(old_product_class).addClass(new_product_class);

	   jQuery("#product_listing").html(my_products_html);

	   }

	 });
}

function addProductToOrder(url, productCode, quantity, cartons)
{
	action_url=url+"productCode/"+productCode+"/quantity/"+quantity+"/cartons/"+cartons+"/";

	jQuery.ajax({
		   type: "POST",
		   url: action_url,
		   dataType: 'xml',
		   beforeSend: function(){
		   	jQuery("#totalSpan").html(' Refreshing ');
  		},
	   success: function(xml){

	   var cartons=jQuery(xml).find('cartons').text();
	   var splits=jQuery(xml).find('splits').text();
	   var products=jQuery(xml).find('products').text();
	   var total=jQuery(xml).find('total').text();
	   var quantity=jQuery(xml).find('quantity').text();
	   var split_quantity=jQuery(xml).find('split_quantity').text();

	   var all_products_count=jQuery(xml).find('all_products_count').text();
	   var all_products_total=jQuery(xml).find('all_products_total').text();

	   var old_product_class=jQuery(xml).find('old_product_class').text();
	   var new_product_class=jQuery(xml).find('new_product_class').text();

	   //var my_products_html=jQuery(xml).find('my_products_html').text();
	   var oneproduct_html=jQuery(xml).find('oneproduct_html').text();
	   var remove_from_myproducts=jQuery(xml).find('remove_from_myproducts').text();

	   var current_order_html=jQuery(xml).find('current_order_html').text();


	   //if ((jQuery("#quantity"+productCode).val() == quantity || jQuery("#quantitym"+productCode).val() == quantity) && (jQuery("#split_quantity"+productCode).val()	==	split_quantity || jQuery("#split_quantitym"+productCode).val()	==	split_quantity ) )
	   //{
	   try {

	   		if(!(jQuery("#quantity"+productCode).val() == quantity || jQuery("#quantitym"+productCode).val() == quantity)) {
	   			throw "Err1";
	   		}


	   		if(!(jQuery("#split_quantity"+productCode).val()	==	split_quantity || jQuery("#split_quantitym"+productCode).val()	==	split_quantity )&&((jQuery("#split_quantity"+productCode).val()!=null)||(jQuery("#split_quantitym"+productCode).val()!=null))) {
	   			throw "Err1";
	   		}

			 jQuery("#cartonsSpan").html(cartons);
		   jQuery("#splitsSpan").html(splits);
		   jQuery("#productsSpan").html(products);
		   jQuery("#totalSpan").html(total);

		   jQuery("#quantitym"+productCode).attr("value",quantity);
		   jQuery("#split_quantitym"+productCode).attr("value",split_quantity);
		   jQuery("#quantity"+productCode).attr("value",quantity);
		   jQuery("#split_quantity"+productCode).attr("value",split_quantity);

		   jQuery("#all_products_count").html(all_products_count);
		   jQuery("#all_products_total").html(all_products_total);



		   //jQuery("#product_listing").html(my_products_html);
		   jQuery("#listallitems").html(current_order_html);

		   if(remove_from_myproducts) {
		   	 jQuery("#myProductDiv"+remove_from_myproducts).remove();

		   }else{
		   		if(jQuery("#myProductDiv"+productCode).val()==null) {
		   			jQuery(oneproduct_html).appendTo("#listofmyproducts");
		   		}
		   }

		   jQuery("#productDiv"+productCode).removeClass().addClass('product_box ' + new_product_class);
		   jQuery("#myProductDiv"+productCode).removeClass().addClass('product myproduct ' + new_product_class);


		  }
			catch(er)
			  {


				}

	   //}

	   }

	 });

	//jQuery("#last_searched_results").html('');
}


function currentOrderManage(url, productCode) {



	var quantity = jQuery("#quantitym"+ productCode).attr("value");
	var cartons = 1;
	//if(jQuery("#c2"+ productCode).attr("checked")) {
	if(jQuery("#cartons"+ productCode).attr("value")=="") {
		cartons = 2;
	}

	var action_url = url+"item_number/"+productCode+"/quantity/"+quantity+"/cartons/"+cartons+"/";


	jQuery.ajax({
		 type: "POST",
		 dataType: 'xml',
		 url: action_url,
		 beforeSend: function(){
		   	jQuery("#totalSpan").html(' Refreshing ');
  	},
	   success: function(xml) {

	   	var quantity=jQuery(xml).find('quantity').text();
	   	var current_order_html=jQuery(xml).find('current_order_html').text();
	    var total=jQuery(xml).find('total').text();

	   	 if(jQuery("#quantitym"+productCode).val() == quantity){
	   		jQuery("#listallitems").html(current_order_html);
	   		jQuery("#totalSpan").html(total);
	   	 }
	   }
	  });

	//jQuery("#last_searched_results").html('');
}


function quickSearch(url) {
	var search_text = jQuery("#quick_search_text").attr("value");

	var action_url = url+"search_text/"+search_text+"/";

	jQuery.ajax({
		 type: "POST",
		 dataType: 'xml',
		 url: action_url,
	   success: function(xml){
	   	var quick_search_result=jQuery(xml).find('quick_search_result').text();
	    var last_searched_results=jQuery(xml).find('last_searched_results').text();

	   	jQuery("#quick_search_result").html(quick_search_result);
	   	jQuery("#last_searched_results").html(last_searched_results);
	   	jQuery("#quick_search_text").val('');
	   }
	 });
}



	function setNextCall(customer_number, url) {

		var nextcall = jQuery("#calend"+customer_number).attr("value");

		var action_url = url + 'agent/setnext/customer_number/' + customer_number + '/nextcall/'+ nextcall + '/';

		jQuery.ajax({
			 type: "POST",
			 url: action_url,
		   success: function(){
		   	var linkurl = url + 'agent/index/';
		   	document.location.href=linkurl;
		   }
		 });
	}


	function relatedProducts(action_url) {
		if(jQuery("#relatedprod")) {

		jQuery.ajax({
			 type: "POST",
			 url: action_url,
		   success: function(html){
		   	jQuery("#relatedprod").html(html);
		   	//jQuery("#relatedtitle").html('Related products in which you might be interested');
		   }
		 });

		}
		return;
	}




	function clearNextCall(customer_number, url) {

		jQuery("#dialog").html('Please confirm date clearing!');
		jQuery("#dialog").dialog('open');
		  	jQuery("#dialog").dialog({

					resizable: false,
					width: 340,
					height:180,
					title: 'Confirmation',
					modal: true,
					overlay: {
						backgroundColor: '#000',
						opacity: 0.5
					},
					buttons: {
						'Close': function() {
							jQuery(this).dialog('close');
						},
						'Clear Date' : function() {
							var action_url = url + 'agent/clearnext/customer_number/' + customer_number;
							jQuery.ajax({
								 type: "POST",
								 url: action_url,
							   success: function(){
							   	jQuery("#calend"+customer_number).attr("value", "");
							   	jQuery("#dialog").dialog('close');
							   }
							 });
						}
					}
				});

	}



  function setBPSP(url) {



		var action_url = url;

		jQuery.ajax({
			 type: "POST",
			 url: action_url,
		   success: function(){

		   	document.location.href=window.location;
		   }
		 });
	}


	function currOrderProdDetails(url) {
		var action_url = url;

		jQuery.ajax({
			 type: "POST",
			 url: action_url,
		   success: function(html){
		   	jQuery("#dialog").html(html);
			  jQuery("#dialog").dialog('open');
		  	jQuery("#dialog").dialog({

					resizable: false,
					width: 800,
					height:500,
					title: 'Product Details',
					modal: true,
					overlay: {
						backgroundColor: '#000',
						opacity: 0.5
					},
					buttons: {
						'Close': function() {
							jQuery(this).dialog('close');
						}
					}
				});

		   }
		 });
	}



function checkEnter(e){
	var characterCode;

	if(e && e.which){
		e = e;
		characterCode = e.which;
	}else{
		e = event;
		characterCode = e.keyCode;
	}

	if(characterCode == 13){
		return false;
	}else{
		return true;
	}
}


function requestAccount(url) {

	var action_url = url + 'index/requestaccount/';

		jQuery.ajax({
			 type: "POST",
			 url: action_url,

		   success: function(html){

				jQuery("#dialog").html(html);
				//jQuery("#dialog").attr("title", "Register for website (Apollo Housewares are a business to business supplier and do not supply the public)");
				jQuery("#dialog").dialog('open');
		  	jQuery("#dialog").dialog({

					resizable: false,
					width: 1000,
					height:720,
					title: 'Register for website (Apollo Housewares are a business to business supplier and do not supply the public)',
					modal: true,
					overlay: {
						backgroundColor: '#000',
						opacity: 0.5
					},
					buttons: {
						Close: function() {
							jQuery(this).dialog('close');
						},
						Send: function() {
							jQuery.ajax({
								 url: action_url,
								  beforeSend: function(){
								  	jQuery('#docbody').height(window.innerHeight);
										jQuery('#docbody').showLoading();
									},
								 type: "POST",
								 data:jQuery("#request_form").serialize(),
								 dataType:html,
							   success: function(html){
										jQuery("#dialog").html(html);
										jQuery('#docbody').hideLoading();
							   }
							});
						}
					}
				});

		 	 }
		});
}



function changePassword(url) {
		var action_url = url;

		jQuery.ajax({
			 type: "POST",
			 url: action_url,

		   success: function(html){

				jQuery("#dialog").html(html);
			  jQuery("#dialog").dialog('open');
		  	jQuery("#dialog").dialog({

					resizable: false,
					width: 600,
					height:360,
					title: 'Change Password',
					modal: true,
					overlay: {
						backgroundColor: '#000',
						opacity: 0.5
					},
					buttons: {
						'Close': function() {
							jQuery(this).dialog('close');
						},
						'Change Password': function() {
								jQuery.ajax({
								 type: "POST",
								 data:jQuery("#changepassform").serialize(),
								 dataType:html,
								 url: action_url,
							   success: function(html){
									 jQuery("#dialog").html(html);

								 }
								});
						}
					}
				});

			 }
		});
}


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

