
function signin(action){
 
 	var user_param={};
 	if(action == "login"){
 		enc = encrypt($("#password").val());
 		$.extend(user_param, {
 		"action":action,
 		"username":$("#userName").val(),
 		"password":enc
 		});
 		$.get("users/login_s.php", user_param, function(rs){eval(rs)});
 	}else if(action == "logout"){
		$.extend(user_param, {"action":action});
		$.get("users/login_s.php", user_param, function(rs){
		eval(rs);});
	}else if(action =="register"){
		/*
	 	"firstname":$("#firstName").val(),
	 	"lastname":$("#lastName").val(),
	 	*/
	 	enc = encrypt($("#password").val());
	 	var user_param={};
	 	$.extend(user_param, {
	 	"action":action,
	 	"username":$("#userName").val(),
	 	"email_address":$("#eMail").val(),
	 	"password":enc,
	 	"currency":$("#currency").val(),
	 	"country":$("#country").val(),
	 	"lt":marker_points.lat(),
	 	"ln":marker_points.lng(),
	 	"z":map.getZoom()
	 	});
 		$.get("users/login_s.php", user_param, function(rs){eval(rs);});
	}else if(action =="pass_reminder"){
		var user_param={};
		$.extend(user_param, {
		"action":action,
		"username":$("#userName").val(),
	 	"email_address":$("#eMail").val()});
	 	$.get("users/login_s.php", user_param, function(rs){eval(rs);});
	}

}
var marker;
function initialize(lat,lng,z) {
      
        if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(lat,lng),z);
        geocoder = new GClientGeocoder();
	map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        GEvent.addListener(map, "click",mapClick);
        }
}
function initialize2maps(lat,lng,zoom){

	var m1 = new GMap2(document.getElementById("minimap1"));
	m1.setMapType(G_HYBRID_MAP);
	var p = new GLatLng(lat,lng); 
	m1.setCenter(p,zoom);
	m1.addControl(new GSmallMapControl());
	m1.addControl(new GMapTypeControl());
	mk1 = new GMarker(p,{clickable:false});
	m1.addOverlay(mk1);	
	var toto = new GSize(150,150);
	m1.panBy(toto);

	var m2 = new GMap2(document.getElementById("minimap2"));
	m2.setCenter(p, 1);
	m2.addControl(new GSmallZoomControl());
	mk2 = new GMarker(p,{clickable:false});
	m2.addOverlay(mk2);
	var toto = new GSize(150,50);
	m2.panBy(toto);				
 }
function mapClick(overlay,point) {
	if (point) {
		marker.setLatLng(point);
		marker_points = marker.getLatLng();
	}
} 
function showMarker(latlng){
     	map.addOverlay(createMarker(latlng));
}
 
function createMarker(point) {
	// Set up our GMarkerOptions object
	markerOptions = { draggable: true };
	marker = new GMarker(point, markerOptions);

	GEvent.addListener(marker, "click", function() {
	       marker.openInfoWindowHtml("Location <b>" + marker.getLatLng() + "</b>");});
	GEvent.addListener(marker, "dragend", function() {
	marker_points = marker.getLatLng();
	marker.openInfoWindowHtml("Located at <b>" + marker.getLatLng() +"</b>");}); 
        return marker;
}
function addLocationToMap(response) {
     
      map.clearOverlays();
      if (!response || response.Status.code != 200) {
      
        alert("Sorry, we were unable to geocode that address.Place the marker by dragging to the required position,");
      	
      	place=$("#country").val();point= new GLatLng(0,0);
      	markerOptions = { title: place,draggable: true };
      	marker = new GMarker(point,markerOptions);
        marker_points = point;
        GEvent.addListener(marker, "click", function() {
		        marker.openInfoWindowHtml("Location <b>" + place + "</b>");
	            });
	GEvent.addListener(marker, "dragend", function() {
			 marker_points = marker.getLatLng();
			 marker.openInfoWindowHtml("Located at <b>" + marker.getLatLng() +"</b>");
	            });
        map.addOverlay(marker);
      	map.setCenter(point, 2);
      	
      } else {
        

       	place = response.Placemark[0];
	
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
	markerOptions = { title: place.address,draggable: true };
	marker = new GMarker(point,markerOptions);
        marker_points = point;
        GEvent.addListener(marker, "click", function() {
	        marker.openInfoWindowHtml("Location <b>" + place.address + "</b>");
            });
         GEvent.addListener(marker, "dragend", function() {
		 marker_points = marker.getLatLng();
		 marker.openInfoWindowHtml("Located at <b>" + marker.getLatLng() +"</b>");
            });
        map.addOverlay(marker);
        marker.openInfoWindowHtml(place.address + '<br>' +
          '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode + '<br> <b>Coordinates</b>: ' + point);
	map.setCenter(point, 5);
	
      }
      //}
}


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

function ValidateSignUp(){

if (trim(window.document.getElementById('userName').value) == '' ){alert('Requested User Name is required. Please enter it.');window.document.getElementById('userName').focus();return false;}

if(document.getElementById('userName').value.length < 3){
	alert ('User Name must be greater than 3 characters.');
	document.getElementById('userName').focus ();
	return false;
}
if(isNaN(document.getElementById('userName').value)){
	var iChars = "-\\*.|,\":<>[]{}`\';()@&$#%";
	var aChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	for (var i = 0; i < document.getElementById('userName').value.length; i++) {
		if (iChars.indexOf(document.getElementById('userName').value.charAt(i)) != -1 || aChars.indexOf(document.getElementById('userName').value.charAt(i)) == -1 ){
			alert ('User Name should only contain ENGLISH characters ,please remove any special or other langauge codes.');
			document.getElementById('userName').focus ();
			return false;
		}
	}
}else{
	alert ('User Name must be a string 0r alphanumeric.');
	document.getElementById('userName').focus ();
	return false;
}

if (trim(window.document.getElementById('password').value) == '' ){alert('Password is required. Please enter it.');window.document.getElementById('password').focus();return false;}

if (trim(window.document.getElementById('ConfirmPassword').value) == '' ){alert('Password Confirmation is required. Please enter it.');window.document.getElementById('ConfirmPassword').focus();return false;}

/*
if (trim(window.document.getElementById('firstName').value) == '' ){alert('First Name is required. Please enter it.');window.document.getElementById('firstName').focus();return false;}
if (trim(window.document.getElementById('lastName').value) == '' ){alert('Last Name is required. Please enter it.');window.document.getElementById('lastName').focus();return false;}
*/

if (trim(window.document.getElementById('eMail').value) == '' ){alert('Email is required. Please enter it.');window.document.getElementById('eMail').focus();return false;}

if (trim(window.document.getElementById('password').value) != trim(window.document.getElementById('ConfirmPassword').value) ){alert('Password and Password Confirmation should be the same. Please correct it and try again.');window.document.getElementById('password').focus();return false;}
/*
if (trim(window.document.getElementById('confirmEMail').value) == '' ){alert('Email Confirmation is required. Please enter it.');window.document.getElementById('confirmEMail').focus();return false;}

if (trim(window.document.getElementById('eMail').value) != trim(window.document.getElementById('confirmEMail').value) ){alert('Email and Email Confirmation should be the same. Please correct it and try again.');window.document.getElementById('eMail').focus();return false;}
*/
var pat1,pat2,pat3,pat4;

pat1=/^[a-z0-9]*$/i;
if (pat1.test(trim(window.document.getElementById('userName').value)) == false ){alert('User name should only contain alphabets (a-z) and numbers (0-9). Please enter valid User Name.');window.document.getElementById('userName').focus();return false;}

pat4=/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/i;if (pat4.test(trim(window.document.getElementById('eMail').value)) == false ){alert('Email address is invalid. Please enter valid Email address.');window.document.getElementById('eMail').focus();return false;}

if (trim(window.document.getElementById('currency').value) == '' ){alert('Currency is required. Please enter it.');window.document.getElementById('currency').focus();return false;}

if (trim(window.document.getElementById('country').value) == '' ){alert('Country is required. Please enter it.');window.document.getElementById('country').focus();return false;}

if (!window.document.getElementById('acceptTermsCheckBox').checked){alert('Please read and accept the Registration Agreement Checkbox.');window.document.getElementById('acceptTermsCheckBox').focus();return false;}



return true;

}

function showCity(address){
	geocoder.getLocations(address, addLocationToMap);
}

function ord( string ) {
    return string.charCodeAt(0);
}
function base64_encode( data ) {
   
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = ac = 0, enc="", tmp_arr = [];
    
    do { // pack three octets into four hexets
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);

        bits = o1<<16 | o2<<8 | o3;

        h1 = bits>>18 & 0x3f;
        h2 = bits>>12 & 0x3f;
        h3 = bits>>6 & 0x3f;
        h4 = bits & 0x3f;

        // use hexets to index into b64, and append result to encoded string
        tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);
    
    enc = tmp_arr.join('');
    
    switch( data.length % 3 ){
        case 1:
            enc = enc.slice(0, -2) + '==';
        break;
        case 2:
            enc = enc.slice(0, -1) + '=';
        break;
    }

    return enc;
}

function base64_decode( data ) {
    
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = ac = 0, dec = "", tmp_arr = [];

    do {  // unpack four hexets into three octets using index points in b64
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));

        bits = h1<<18 | h2<<12 | h3<<6 | h4;

        o1 = bits>>16 & 0xff;
        o2 = bits>>8 & 0xff;
        o3 = bits & 0xff;

        if (h3 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1);
        } else if (h4 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1, o2);
        } else {
            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
        }
    } while (i < data.length);
    
    dec = tmp_arr.join('');
    
    return dec;
}

function chr( ascii ) {
   return String.fromCharCode(ascii);
}