var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}



//-------------------------------------------------------------------------------
//AJAX FUNCTIONS START HERE
var divid;
var actionstr;

var showobj;
		var pageURL;
		function makeRequest(url,arg,obj) {
			//alert(url);
			pageURL=url;	
			arg=getargs(arg);
			showobj=obj;
			var http_request = false;
			if (window.XMLHttpRequest) { // Mozilla, Safari, ...
				http_request = new XMLHttpRequest();
				if (http_request.overrideMimeType) {
					http_request.overrideMimeType('text/xml');
				}
			} else if (window.ActiveXObject) { // IE
				try {
					http_request = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try {
						http_request = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e) {}
				}
			}
			if (!http_request) {
				alert('Giving up :( Cannot create an XMLHTTP instance');
				return false;
			}
		
			http_request.onreadystatechange = function() { handleRespon(http_request); };
			http_request.open('POST', url, true);
			http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			http_request.setRequestHeader("Content-Length", arg.length);
			http_request.send(arg);
		}
			
		
		function handleRespon(http_request) {
			if (http_request.readyState == 4) {
				//alert(http_request.responseText);
				if (http_request.status == 200) {
					//alert(showobj);
					//alert(http_request.responseText);
					document.getElementById(showobj).innerHTML =  http_request.responseText;
					
					var checkvalue = http_request.responseText;	
					
					//alert(checkvalue);
					
					//document.getElementById('pb1').style.display='none';
					
				} else {
					alert('Your request can\'t process.\nThere is some problem with the request.');
				}
			}
		}
		
		function getargs(arg){
			var val;
			var timestamp = Date.parse(new Date()); //document.lastModified;
			if (typeof arg != 'undefined'){	val = "sess="+timestamp+"&"+arg;	}
			else{	val = "sess="+timestamp; }
			return val;
		}


//AJAX FUNCTION ENDS HERE
//-------------------------------------------------------------------------------




//-------------------------------------------------------------------------------
//JQuery functions

// prepare the form when the DOM is ready
function signupfrmsubmission()
{
	$('#add_dealform').unbind('submit');
		var options = {
		target: '#output', // target element(s) to be updated with server response
		beforeSubmit: showRequest, // pre-submit callback
		success: showResponse, // post-submit callback
		url: 'request_process.php?calling=1' //override for form's 'action' attribute //type: type // 'get' or 'post', override for form's 'method' attribute
	};

	// bind to the form's submit event
	$('#add_dealform').submit(function() {
	$(this).ajaxSubmit(options);
	
	return false;
	});
}



// pre-submit callback
function showRequest(formData, jqForm, options) {
var queryString = $.param(formData);
return true;
}


function showResponse(responseText, statusText) {

$("#output").show();

}

//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//show city w.r.t country starts here

function showcity(countryid,holder)
	{
		//alert(countryid+"------"+holder);
		var FileName 	=	 "ajax.php";
		var arg 		=	 "myaccount=contactinfo&countryid="+countryid;
		makeRequest(FileName,arg,holder);
	}

//show city w.r.t country ends here
//-------------------------------------------------------------------------------

function evssetprofstatusmy(curprstatus)
{
	$.ajax({
		   type: "POST",
		   url: 'ajax.php',
		   data: 'myprofcurstatus='+curprstatus,
		   //beforeSend: function(){$("#"+resultDiv).html("Loading...");},
		   success: function(msg){
			   	document.getElementById('setpublicprofilediv').innerHTML = msg;
			   	//if(msg == 'public')
				//{
					//document.getElementById('setpublicprofile2').style.display = '';
					//document.getElementById('setpublicprofile').style.display = 'none';
					//$("#setpublicprofilediv").html(msg); 
				//}
				//if(msg == 'private')
				//{
					//document.getElementById('setpublicprofile').style.display = '';
					//document.getElementById('setpublicprofile2').style.display = 'none';
					//$("#setpublicprofilediv").html(msg);
				//}
			},
			error: function(){ //so, if data is retrieved, store it in html 
				//alert('error')
			//$("#"+resultDiv).html('Error Loading Script'); //show the html inside .content div 
		 }
	});
}

//----------- INVITE FRIENDs REQUEST -----------------------------//
//Invite friend request code starts here
function echeck(str)
{

		var emailerr=0;
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   emailerr = 1;
		   
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   emailerr = 1;
		   
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    emailerr = 1;
		   
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   emailerr = 1;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    emailerr = 1;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    emailerr = 1;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    emailerr = 1;
		 }
		return emailerr;
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function filterbymanuf(dd1)
{
	var idx = dd1.selectedIndex;
    // var them_name = dd1[idx].text;
	var mapid = dd1[idx].value;
	
	document.getElementById('leveltwo').innerHTML =''; 
	document.getElementById('levelthree').innerHTML ='';
	// var val = dd1[idx].value;
	
	$.ajax({
	   type: "POST",
	   url: serverpath+'request_process.php?calling=1&action=level1',
	   data: 'mapid='+mapid,
	   
	   beforeSend: function(){
			jQuery("#manufacload_div").html('<img src="'+serverpath+'images/loaders/ajax-loaderpubprof.gif" alt="Processing..." />');
				//window.scrollTo(0,0);
	   },
	   
	   success: function(msg){
		   	jQuery("#levelone").html(msg);
			jQuery("#manufacload_div").html('');
		},
		error: function(){ //so, if data is retrieved, store it in html 
			//alert('error')
			//$("#"+resultDiv).html('Error Loading Script'); //show the html inside .content div 
		}
	 });
}

//------------------------------------------------//
function ajaxproductfilters(dd1)
{
	var idx = dd1.selectedIndex;
    // var them_name = dd1[idx].text;
	var mapid = dd1[idx].value;
	
	document.getElementById('leveltwo').innerHTML =''; 
	document.getElementById('levelthree').innerHTML ='';
	// var val = dd1[idx].value;
	
	$.ajax({
	   type: "POST",
	   url: serverpath+'request_process.php?calling=3&action=level1',
	   data: 'mapid='+mapid,
	   
	   beforeSend: function(){
			jQuery("#manufacload_div").html('<img src="'+serverpath+'images/loaders/ajax-loaderpubprof.gif" alt="Processing..." />');
				//window.scrollTo(0,0);
	   },
	   
	   success: function(msg){
		   	jQuery("#levelone").html(msg);
			jQuery("#manufacload_div").html('');
		},
		error: function(){ //so, if data is retrieved, store it in html 
			//alert('error')
			//$("#"+resultDiv).html('Error Loading Script'); //show the html inside .content div 
		}
	 });
}

function filterbytype()
{
	var ptype;
    var manuid;
    ptype = document.getElementById('prodtype').value;
	manuid = document.getElementById('manuid').value;
	
	// var val = dd1[idx].value;
	
	$.ajax({
	   type: "POST",
	   url: serverpath+'request_process.php?calling=3&action=level2',
	   data: 'ptype='+ptype+'&manuid='+manuid,
	   
	   beforeSend: function(){
			jQuery("#manufacload_div").html('<img src="'+serverpath+'images/loaders/ajax-loaderpubprof.gif" alt="Processing..." />');
				//window.scrollTo(0,0);
	   },
	   
	   success: function(msg){
		   	jQuery("#leveltwo").html(msg);
			jQuery("#manufacload_div").html('');
		},
		error: function(){ //so, if data is retrieved, store it in html 
			//alert('error')
			//$("#"+resultDiv).html('Error Loading Script'); //show the html inside .content div 
		}
	 });
}

function filterbycategory()
{
	var ptype;
    var manuid;
	var catid;
	
    //ptype = document.getElementById('prodtype').value;
	manuid = document.getElementById('manuid').value;
	catid = document.getElementById('maincatid').value;

	// var val = dd1[idx].value;
	
	$.ajax({
	   type: "POST",
	   url: serverpath+'request_process.php?calling=3&action=level3',
	   data: 'ptype='+ptype+'&manuid='+manuid+'&catid='+catid,
	   
	   beforeSend: function(){
			jQuery("#manufacload_div").html('<img src="'+serverpath+'images/loaders/ajax-loaderpubprof.gif" alt="Processing..." />');
				//window.scrollTo(0,0);
	   },
	   
	   success: function(msg){
		   	jQuery("#levelthree").html(msg);
			jQuery("#manufacload_div").html('');
		},
		error: function(){ //so, if data is retrieved, store it in html 
			//alert('error')
			//$("#"+resultDiv).html('Error Loading Script'); //show the html inside .content div 
		}
	 });
}

//-----------------------------------------------------//

function level2(dd1)
{
	var idx = dd1.selectedIndex;
    // var them_name = dd1[idx].text;
    var mapid = dd1[idx].value;
    document.getElementById('levelthree').innerHTML ='';

	// var val = dd1[idx].value;
  	//alert(mapid);
	
	$.ajax({
	   type: "POST",
	   url: serverpath+'request_process.php?calling=1&action=level2',
	   data: 'mapid='+mapid,
	   
	   beforeSend: function(){
			jQuery("#manufacload_div").html('<img src="'+serverpath+'images/loaders/ajax-loaderpubprof.gif" alt="Processing..." />');
				//window.scrollTo(0,0);
	   },
	   
	   success: function(msg){
		   	jQuery("#leveltwo").html(msg);
			jQuery("#manufacload_div").html('');
		},
		error: function(){ //so, if data is retrieved, store it in html 
			//alert('error')
			//$("#"+resultDiv).html('Error Loading Script'); //show the html inside .content div 
		}
	 });
}

function level3(dd1)
{
	alert(dd1);
	 var idx = dd1.selectedIndex;
	 alert(idx);
 	// var them_name = dd1[idx].text;
  	var mapid = dd1[idx].value;
 	document.getElementById('levelthree').innerHTML ='';
 
 	// var val = dd1[idx].value;
    alert(mapid);
	
	$.ajax({
	   type: "POST",
	   url: serverpath+'request_process.php?calling=1&action=level3',
	   data: 'mapid='+mapid,
	   
	   beforeSend: function(){
			jQuery("#manufacload_div").html('<img src="'+serverpath+'images/loaders/ajax-loaderpubprof.gif" alt="Processing..." />');
				//window.scrollTo(0,0);
	   },
	   
	   success: function(msg){
		   	jQuery("#levelthree").html(msg);
			jQuery("#manufacload_div").html('');
		},
		error: function(){ //so, if data is retrieved, store it in html 
			//alert('error')
			//$("#"+resultDiv).html('Error Loading Script'); //show the html inside .content div 
		}
	 });
}

//-----------------------------------------------------//

function latestproducts()
{
	$.ajax({
	   type: "POST",
	   url: serverpath+'request_process.php?calling=4',
	   data: '',
	   
	   beforeSend: function(){
			//jQuery("#slider").html('<img src="'+serverpath+'images/loaders/loader_bar.gif" alt="Processing..." />');
			//window.scrollTo(0,0);
	   },
	   
	   success: function(msg){
		   	jQuery("#slider").html(msg);
			
			$("#slider").easySlider({
			controlsBefore:	'<p id="controls">',
			controlsAfter:	'</p>',
			prevId: 'prevBtn',
			nextId: 'nextBtn'
			});
			
		},
		error: function(){ //so, if data is retrieved, store it in html 
			//alert('error')
			//$("#"+resultDiv).html('Error Loading Script'); //show the html inside .content div 
		}
	 });
}

function featprods()
{
	$.ajax({
	   type: "POST",
	   url: serverpath+'request_process.php?calling=5',
	   data: '',
	   
	   beforeSend: function(){
			//jQuery("#slider").html('<img src="'+serverpath+'images/loaders/loader_bar.gif" alt="Processing..." />');
			//window.scrollTo(0,0);
	   },
	   
	   success: function(msg){
		   	jQuery("#slider2").html(msg);
			
			$("#slider2").easySlider({
			controlsBefore:	'<p id="controls2">',
			controlsAfter:	'</p>',
			prevId: 'prevBtn2',
			nextId: 'nextBtn2',
			auto:		false,
			continuous:	false
			});
			
		},
		error: function(){ //so, if data is retrieved, store it in html 
			//alert('error')
			//$("#"+resultDiv).html('Error Loading Script'); //show the html inside .content div 
		}
	 });
}

//------------------------------------------------------------------------------------------------//
// prepare the form when the DOM is ready
function submitlandscapform()
{
	$('#landformregister').unbind('submit');
		var options = {
		target: '#output', // target element(s) to be updated with server response
		beforeSubmit: showlandscapRequest, // pre-submit callback
		success: showlandscapResponse, // post-submit callback
		url: 'request_process.php?calling=8' //override for form's 'action' attribute //type: type // 'get' or 'post', override for form's 'method' attribute
	};

	// bind to the form's submit event
	$('#landformregister').submit(function() {
	$(this).ajaxSubmit(options);
	
	return false;
	});
}

// pre-submit callback
function showlandscapRequest(formData, jqForm, options) {
	var queryString = $.param(formData);
	return true;
}

function showlandscapResponse(responseText, statusText) {
	$("#output").show();
}
//------------------------------------------------------------------------------------------------//

function selectmanufacturer(id)
{
	$.ajax({
	   type: "POST",
	   url: serverpath+'request_process.php?calling=10',
	   data: 'cid='+id,
	   
	   beforeSend: function(){
			//jQuery("#slider").html('<img src="'+serverpath+'images/loaders/loader_bar.gif" alt="Processing..." />');
			//window.scrollTo(0,0);
	   },
	   
	   success: function(msg){
		   	jQuery("#levelone").html(msg);
		},
		error: function(){ //so, if data is retrieved, store it in html 
			////alert('error')
			//$("#"+resultDiv).html('Error Loading Script'); //show the html inside .content div 
		}

	 });
}


function verify(form) 
  {
        var passed = false;
        var blnRetval, intAtSign, intDot, intComma, intSpace, intLastDot, intDomain, intStrLen;
        if (form.Email){
                       intAtSign=form.Email.value.indexOf("@");
                        intDot=form.Email.value.indexOf(".",intAtSign);
                        intComma=form.Email.value.indexOf(",");
                        intSpace=form.Email.value.indexOf(" ");
                        intLastDot=form.Email.value.lastIndexOf(".");
                        intDomain=intDot-intAtSign;
                        intStrLen=form.Email.value.length;
                if (form.Email.value == "" ) 
                {
                alert("Please enter your email.");
                form.Email.focus();
                passed = false;
                }
                else if (intAtSign == -1)
                {
	                alert("Invalid email");
                        form.Email.focus();
    	            passed = false;
                }
                else if (intComma != -1)
                {
                alert("Votre Courriel ne doit pas contenir une virgule ou apostrophe.");
                form.Email.focus();
                passed = false;
                }
                else if (intSpace != -1)
                {
                alert("Votre courriel est trop long.");
                form.Email.focus();
                passed = false;
                }
                else if ((intDot <= 2) || (intDomain <= 1)  || (intStrLen-(intLastDot+1) < 2)) 
                {
                alert("Invalid email.");                                             
                form.Email.focus();                                                     
                passed = false;
                }  else {
                 passed = true;  
                }
        	} else    {
              passed = true;
        }  return passed;
  }
