dojo.require("dojo.widget.Tooltip");
dojo.require("dojo.widget.Dialog");

function GetXmlHttpObject(){
	var xmlHttp=null;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function slide(div){
	var slided=document.getElementById(div);
	if(slided.style.display=="none"){
		dojo.lfx.wipeIn(slided,250).play();		
	}
	else{
		dojo.lfx.wipeOut(slided,250).play();
	}
}

function valSearch(){
	if(document.getElementById('key').value==""){
		alert('Please insert your med');
		return;
	}
	document.getElementById('searchform').submit();
}

function hideDialog(dialog){
	var endiag = dojo.widget.byId(dialog);				
	endiag.hide();
}

function addToolTips(iddom,msg,tag){
	var domtree= document.getElementById(iddom).getElementsByTagName(tag);
	
	for (var i=0;i<domtree.length;i++){ 
  		if(domtree[i].id==""){
			domtree[i].id="link"+i;	
		}
 		var newtool="<span dojoType='tooltip' id='tool"+i+"' connectId='"+domtree[i].id+"' toggle='explode' style='width:160px;background: #FFF5D7 url(images/logoback.gif) no-repeat right bottom; border: 1px solid #F59A6D; padding:10px 32px 10px 10px; font-weight: bold;'>";
		if(domtree[i].title!=""){
			newtool=newtool+domtree[i].title;
		}
		else{
			newtool=newtool+msg;
		}
		
		newtool=newtool+"</span>";
		//document.getElementById('tooltips').innerHTML=document.getElementById('tooltips').innerHTML+newtool;		
  	}

	for (var k=0;k<i;k++){
		var nodeRef=document.getElementById('tool'+k);
		//dojo.widget.createWidget(nodeRef);
	}
}

function addToFavorites() {
	title = "Online-Foreign-Pharmacy.com - The Best Online Pharmacy Directory !!"; 
	url = "http://www.online-foreign-pharmacy.com/";
	try{		
		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		}
	}
	catch(e){
		try{
			if( window.external ) { // IE Favorite
				window.external.AddFavorite( url, title); 
			}
		}
		catch(e){
			try{
				if(window.opera && window.print) { // Opera Hotlist
					return true; 
				}
			}
			catch(e){
				alert('your browser is not supported');
			}
		}
	}
} 

function contactUs(){
	var dialogsec=document.getElementById("dialogs");
	if(document.getElementById("contactd")==null){
		var content='<div class="ofpdialog"><fieldset style="width:380px;" id="divdialog"><legend>Contact Us</legend><p>Name:<br /><input type="text" name="name" id="name" /><br />Email Address:<br /><input type="text" name="email" id="email" /><br />Usercode if you have:<br /><input type="text" name="usercode" id="usercode" /><br />Subject:<br /><select name="subject" id="subject"><option value="General questions">General questions</option><option value="FAQ">FAQ</option><option value="Problems with Order">Problems with Order</option><option value="Affiliate Question">Affiliate Question</option></select><br />Comments/Quetions:<br /><textarea name="comments" id="comments" cols="30" rows="7"></textarea></p><br /><p><input type="image" src="images/sendb.jpg" style="border:none; cursor: pointer;" onclick="sendMail();" alt="send your message!!" /> &nbsp;&nbsp; <input type="image" id="cancel" src="images/cancelb.jpg" style="border:none; cursor: pointer;" alt="cancel" /></p><p>You can email us any time to <a href="mailto:customer@online-foreign-pharmacy.com">customer@online-foreign-pharmacy.com</a><br />Phone number: 00+1-786-206-1773<br /></p></fieldset><div id="divmsg" style="display:none; text-align:center; background: url(images/loading.gif) no-repeat top left;"><p class="remark">Thank you for your message.<br /> We will respond to your inquiry within 24 hours</p></div></div>';
		dialogsec.innerHTML=dialogsec.innerHTML+'<div dojoType="dialog" id="contactd" bgColor="#DBECF5" bgOpacity="0.7" toggle="fade" toggleDuration="600">'+content+'</div>';		
	}
	else{
		document.getElementById("divdialog").style.display="block";
		document.getElementById("divmsg").style.display="none";
	}
	var nodeRef = document.getElementById("contactd");
	dojo.widget.createWidget(nodeRef);
		
	dlg = dojo.widget.byId("contactd");
	btn = document.getElementById("cancel");
	dlg.setCloseControl(btn);
	dlg.show();
}

function sendMail(){
	
	if(document.getElementById('name').value==""){
		alert('Please insert your name');
		document.getElementById('name').focus();
		return;
	}
	
	if(document.getElementById('email').value==""){
		alert('Please insert your email');
		document.getElementById('email').focus();
		return;
	}
	
	if(document.getElementById('comments').value==""){
		alert('Please insert your message');
		document.getElementById('comments').focus();
		return;
	}
	
	var url="name="+document.getElementById('name').value+"&email="+document.getElementById('email').value+"&subject="+document.getElementById('subject').value+"&commentsquetions="+document.getElementById('comments').value+"&usercode="+document.getElementById('usercode').value;
	
	
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
	    alert ("Your browser does not support AJAX!");
	    return;
	}
	xmlHttp.onreadystatechange=function (){
		if(xmlHttp.readyState==4){
			if(xmlHttp.responseText==0){
            	var fadeform=dojo.lfx.wipeOut(document.getElementById("divdialog"),800);
				var fademsg=dojo.lfx.wipeIn(document.getElementById("divmsg"),800);
				var compdiag = dojo.lfx.chain(fadeform,fademsg);
				document.getElementById('name').value="";
				document.getElementById('email').value="";
				document.getElementById('usercode').value="";
				document.getElementById('comments').value="";
				compdiag.play(200,true);
				setTimeout("hideDialog('contactd')",8000);
			}
			else{
				alert(xmlHttp.responseText);
			}
       	}	            
	};
	xmlHttp.open("POST","services/mail_services.php",false);
	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttp.send(url);
}

function login(){
	var dialogsec=document.getElementById("dialogs");
	if(document.getElementById("logind")==null){
		var content='<div class="ofpdialog"><fieldset style="width:280px;" id="divlog"><legend>Login</legend><p id="loginres"></p><p>Username:<br /><input type="text" name="username" id="username" /><br />Password:<br /><input type="password" name="passwd" id="passwd" /><p>&nbsp;</p><p><input type="image" src="images/loginb.jpg" style="border:none; cursor: pointer;" onclick="loginProcess();" alt="Log into our members area!!" /> &nbsp;&nbsp; <input type="image" id="logcancel" src="images/cancelb.jpg" style="border:none; cursor: pointer;" alt="cancel" /></p></fieldset><div id="divlogmsg" style="display:none; text-align:center; background: url(images/loading.gif) no-repeat top left;"><p class="remark">Welcome to our members area.<br /> you are being redirected, please wait</p></div></div>';
		dialogsec.innerHTML=dialogsec.innerHTML+'<div dojoType="dialog" id="logind" bgColor="#DBECF5" bgOpacity="0.7" toggle="fade" toggleDuration="600">'+content+'</div>';		
	}
	else{
		document.getElementById("divlog").style.display="block";
		document.getElementById("divlogmsg").style.display="none";
	}
	var nodeRef = document.getElementById("logind");
	dojo.widget.createWidget(nodeRef);
		
	dlg = dojo.widget.byId("logind");
	btn = document.getElementById("logcancel");
	dlg.setCloseControl(btn);
	dlg.show();
}

function loginProcess(){
	
	if(document.getElementById('username').value==""){
		alert('Please insert your username');
		document.getElementById('username').focus();
		return;
	}
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
	    alert ("Your browser does not support AJAX!");
	    return;
	}
	xmlHttp.onreadystatechange=function (){
		if(xmlHttp.readyState==4){
			if(xmlHttp.responseText=='0' || xmlHttp.responseText=='1'){
				if(xmlHttp.responseText=='1'){
            		/*var fadeform=dojo.lfx.wipeOut(document.getElementById("divlog"),800);
					var fademsg=dojo.lfx.wipeIn(document.getElementById("divlogmsg"),800);
					var compdiag = dojo.lfx.chain(fadeform,fademsg);
					compdiag.play(200,true);
					setTimeout("endLogin()",8000);
					*/
					endLogin();
				}
				else{
					document.getElementById('loginres').innerHTML='<span class="remark">Wrong username and password combination, please try again</span>' ;
					document.getElementById('passwd').value="";
				}				
			}
			else{
				alert(xmlHttp.responseText);
			}
       	}	            
	};
	xmlHttp.open("POST","services/users_services.php",false);
	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttp.send('username='+document.getElementById('username').value+'&passwd='+document.getElementById('passwd').value);
}

function endLogin(){
	//hideDialog('logind');
	window.location.href='members.php';
}