// JavaScript Document

var lastUrl = '';
var param;
var status;
var g_url2;
var g_catname;
var g_cathref;
//var g_location="/index.htm";
//alert(param);

//request of customer
request();

//get location for back when login or logout
//getLocation();

function getParam(i_param, i_status){
	param=i_param;
	status=i_status;
	checkUrl();
//	alert(param);
}

function checkUrl(){
	//alert(param);
	url = window.location.href;	

	if (url !== '' && url !== lastUrl) {
		c_url = url.split('#');

		if(c_url.length>0){
			e_url= c_url[1];		
			if(e_url!=null){
				et=e_url.split('/');
				page=et[0];
				title=et[et.length-1];
			
				//create blank between word
				title=title.replace(/%20/g, " ");	
				title=title.replace(/%27/g, "'");				
				
				url=page+".php";	
				if(param[1]!="empty"){
					if(et.length-2>0) url+="?";				
					if(status) size=et.length;
					else size=et.length-1;
					
					for(i=1; i<size; i++){
						url+=param[i]+"="+et[i]+"&";
					}
					url=url.substr(0, url.length-1);	
				}
			
				//alert(url);		
	
				rq_data('GET', url, title);	
			}	
		}	
		lastUrl=url;
	}	
}

function startLoadPage(val){
    //return (true == val) ? setInterval('getParam(param, status)', 1555) : clearInterval(setInterval('getParam(param, status)', 1000));
	return (true == val) ? setInterval('getParam(param, status)', 1000) : clearInterval(setInterval('getParam(param, status)', 10000));	
}

function refreshMeta(val){
	return (true == val) ? setInterval("setMeta('abc', 'abc')", 10000) : clearInterval(setInterval("setMeta('abc', 'abc')", 10000));
}

//
function setMeta(i_meta_keywords, i_meta_desc){		
		meta_tag=document.getElementsByTagName('meta');
		
		for(i=0; i<meta_tag.length; i++){
			if(meta_tag[i].name=="keywords"){
				//alert(meta_tag[i].content);
				meta_tag[i].content=i_meta_keywords;				

			}
			else if(meta_tag[i].name=="description"){
				meta_tag[i].content=i_meta_desc;
			}
		}

		for(i=0; i<meta_tag.length; i++){
			//alert(meta_tag[i].content);			
		}

}




function checkUrlCat(param, status){
	//only change rq_data --> rq_cat with checkUrl
	//alert(param);
	url = window.location.href;	

	if (url !== '' && url !== lastUrl) {
		c_url = url.split('#');

		if(c_url.length>0){
			e_url= c_url[1];		
			if(e_url!=null){
				et=e_url.split('/');
				page=et[0];
				title=et[et.length-1];
			
				//create blank between word
				title=title.replace(/%20/g, " ");	
				title=title.replace(/%27/g, "'");				
				
				url=page+".php";	
				if(param[1]!="empty"){
					if(et.length-2>0) url+="?";				
					if(status) size=et.length;
					else size=et.length-1;
					
					for(i=1; i<size; i++){
						url+=param[i]+"="+et[i]+"&";
					}
					url=url.substr(0, url.length-1);	
				}
			
				//alert(url);		
				i_url=window.location.href.split('#');
				i_url=i_url[1];
				href="#"+i_url;
				rq_cat('GET', url, title, href);	
			}	
		}	
		lastUrl=url;
	}
}

function checkUrlCoupon(param, status){
	//only change rq_data --> rq_cat with checkUrl
	//alert(param);
	url = window.location.href;	

	if (url !== '' && url !== lastUrl) {
		c_url = url.split('#');

		if(c_url.length>0){
			e_url= c_url[1];		
			if(e_url!=null){
				et=e_url.split('/');
				page=et[0];
				title=et[et.length-1];
			
				//create blank between word
				title=title.replace(/%20/g, " ");	
				title=title.replace(/%27/g, "'");				
				
				url=page+".php";	
				if(param[1]!="empty"){
					if(et.length-2>0) url+="?";				
					if(status) size=et.length;
					else size=et.length-1;
					
					for(i=1; i<size; i++){
						url+=param[i]+"="+et[i]+"&";
					}
					url=url.substr(0, url.length-1);	
				}
			
				//alert(url);		
				i_url=window.location.href.split('#');
				i_url=i_url[1];
				s_url=i_url.split('/');
				i_subname=s_url[5];
				i_catid=s_url[6];
				i_catname=s_url[7];
				
				i_cathref="#coupon/"+i_catid+"/"+i_catname;
				
				url2="coupon.php?CategoryID="+i_catid;
				
				i_click="javascript:rq_cat('GET', '"+url2+"', '"+i_catname+"', '"+i_cathref+"')";
//				alert(i_catid);
				//create blank between words
				i_subname=i_subname.replace(/%20/g, " ");	
				i_catname=i_catname.replace(/%20/g, " ");	
				rq_sub('GET', url, i_subname, i_catname, i_cathref, i_click);
			}	
		}	
		lastUrl=url;
	}
}

function checkUrlReg(param, status, sDiv, key, i_choice){		
	url = window.location.href;	
	c_url = url.split('#');
	//alert(c_url[1]);
	window.location.hash=c_url[1];
	if(c_url.length>0){
		e_url= c_url[1];		
		if(e_url!=null){
			et=e_url.split('/');
			page=et[0];
			title=et[et.length-1];
			i_id=et[1];

			//create blank between word
			title=title.replace(/%20/g, " ");	
			title=title.replace(/%27/g, "'");
			
			url=page+".php";	
			if(param[1]!="empty"){
				if(et.length-2>0) url+="?";				
				if(status) size=et.length;
				else size=et.length-1;
				
				for(i=1; i<size; i++){
					url+=param[i]+"="+et[i]+"&";
				}
				url=url.substr(0, url.length-1);	
			}
		
//			alert(url);
			rq_register('GET', url, sDiv, title, key, i_id, i_choice);	
		}	
	}	
}


function getPage(){
	url = window.location.href;
	c_url = url.split('#');
	page="";
	if(c_url.length>0){
		e_url= c_url[1];
		if(e_url!=null){
			et=e_url.split('/');
			page=et[0];		
		}		
	}
	return page;
}


		
				//getTitle for right menu - search merchant
				function getTitle(){
					var drop=document.getElementById("mySelect");
					var id=drop.value;
					var url;
					for(var i=0; i<drop.length; i++){
						if(id==drop[i].value){
							url1='merchants.php?merchantid='+id;							
							title=drop[i].text;
							title1="View more coupons for this store '"+title+"'";
							document.location.href="#merchants/"+id+"/"+title1;
							
							title2="Information detail for this store '"+title+"'";
							url2='merchants_detail.php?id='+id;									
							rq_merchant('GET', url1, title1, "javascript:rq_data('GET', url2, title2)");
							//rq_data('GET', url, title);
						}
					}
				}		
				
				function getMerchant(id, text){
					url1='merchants.php?merchantid='+id;							
					title=text;
					title1="View more coupons for this store '"+title+"'";
					title2="Information detail for this store "+title+"";
					url2='merchants_detail.php?id='+id;										
					rq_merchant('GET', url1, title1, "#merchants_detail/"+id+"/"+title2, "javascript:rq_data('GET', '"+url2+"', '"+title2+"')");
				}
				
				function getMerchantDetail(id, text){
					title="Information detail for this store '"+text+"'";
					url='merchants_detail.php?id='+id;										
					rq_data('GET', url, title);
				}
				
				function getCoupon(){					
					window.location.href="#homepage";
					rq_data('GET', 'homepage.php', 'Home Page');					
				}
				
				//getSearchType display result about store, category
				function getSearchType(){					
					var keyword=document.getElementById('search_input').value;
					var type=document.getElementById('search_coupon').value;
					
					//alert(type);
					if(keyword!=""){
						if(type==1){
							display("search_display_type", true);
							var url="search_type_result.php?keyword="+keyword+"&type="+type;	
							rq_register('GET', url, "search_display_type", "Search Coupon", keyword, 0, type);	
						}				
	
					}
					else{
						display("search_display_type", false);
					}

					if(type==2){
						display("search_display_type", false);
					}
					
				}
				
							
				function getSearchClick(u_id, u_type, u_page, u_keyword){
					i_url="search_coupon_result.php?id="+u_id+"&type="+u_type+"&page="+u_page+"&keyword="+u_keyword;
					rq_register('GET', i_url, 'search_display_coupon', 'Search Coupon', u_keyword, u_id, u_type);
				}
				
				//getSearch Coupon
				function getSearchCoupon(){
					display("search_display_type", false);
					
					var id=document.getElementById('hidden_input').value;
					var type=document.getElementById('search_coupon').value;
					var keyword=document.getElementById('search_input').value;
					
					document.location.href="#search_coupon_result/"+id+"/"+type+"/1/"+keyword+"/Search Coupon";
					
					var url="search_coupon_result.php?id="+id+"&type="+type+"&page=1&keyword="+keyword;

					rq_register('GET', url, "search_display_coupon", 'Search Coupon', keyword, id, type);
				}		
				
				//getQuickSearchType 
				function getQuickSearchType(){						
				/*
					var	merchantid;
					
					var drop=document.getElementById("quick_search_type");
					var type=drop.value;
					
					//same use variable merchantid to take id from merchant and promotion
					if(type==1){
						//search follow merchant
						display("quick_search_merchant", true);	
						display("quick_search_promotion", false);	
						merchantid=document.getElementById("quick_search_merchant_input").value;
					}
					else if(type==2){
						//search follow promotion
						display("quick_search_merchant", false);	
						display("quick_search_promotion", true);	
						merchantid=document.getElementById("quick_search_promotion_input").value;
					}
					
					var url;
					for(var i=0; i<drop.length; i++){
						if(type==drop[i].value){
							document.getElementById("quick_search_type_input").value=type;							
							document.location.href="#quick_search/"+type+"/"+merchantid+"/1/Quick Search";																						
							url="quick_search.php?type="+type+"&merchantid="+merchantid+"&page=1";
							title="Quick Search";
							rq_data('GET', url, title);							
						}
					}
				*/
				}
				
				//getSearchType display result about store in left menu
				function getQuickSearchType(){							
					var keyword=document.getElementById('quick_search_input').value;
					var type=document.getElementById('quick_search_type').value;
					var nameDiv="quick_search_display_type";
					//alert(type);
					if(keyword!=""){
						if(type==1){
							display(nameDiv, true);
							var url="/quick_search_type_result.php?keyword="+keyword+"&type="+type;	
							rq_search('GET', url, nameDiv);	
						}				
	
					}
					else{
						display(nameDiv, false);
					}

					if(type==2){
						display(nameDiv, false);
					}
					
				}
				
				//set Text when chose from quicksearch input
				function setText_QuickSearch(text){
					//alert(id);
					document.getElementById("quick_search_input").value=text;					
					display("quick_search_display_type", false);
				}
				
				//getQuickSearchMerchant
				function getQuickSearchMerchant(){
					var	type=document.getElementById("quick_search_type_input").value;			
					var drop=document.getElementById("quick_search_merchant");
					var merchantid=drop.value;
					var url;			
					
					for(var i=0; i<drop.length; i++){
						if(merchantid==drop[i].value){
							document.getElementById("quick_search_merchant_input").value=merchantid;
							//alert(type);
							document.location.href="#quick_search/"+type+"/"+merchantid+"/1/Quick Search";																						
							url="quick_search.php?type="+type+"&merchantid="+merchantid+"&page=1";
							title="Quick Search";
							rq_data('GET', url, title);	
						}
					}
				}
				
				//getQuickSearchPromotion
				function getQuickSearchPromotion(){
					var	type=document.getElementById("quick_search_type_input").value;			
					var drop=document.getElementById("quick_search_promotion");
					var promotionid=drop.value;
					var url;			
					
					for(var i=0; i<drop.length; i++){
						if(promotionid==drop[i].value){
							document.getElementById("quick_search_promotion_input").value=promotionid;						
							document.location.href="#quick_search/"+type+"/"+promotionid+"/1/Quick Search";																						
							url="quick_search.php?type="+type+"&merchantid="+promotionid+"&page=1";
							title="Quick Search";
							rq_data('GET', url, title);	
						}
					}
				}
				
				//getQuickSearch
				function getQuickSearch(){					
					var	type=document.getElementById("quick_search_type").value;			
					var	keyword=document.getElementById("quick_search_input").value;					

					document.location.href="#quick_search/"+type+"/"+keyword+"/1/Quick Search";																						
					url="quick_search.php?type="+type+"&keyword="+keyword+"&page=1";
					title="Quick Search";
					rq_data('GET', url, title);					
				}	
							
				//getQuickSearchClick
				function getQuickSearchClick(u_type, u_keyword, u_page){
					url="quick_search.php?type="+u_type+"&keyword="+u_keyword+"&page="+u_page;
					rq_data('GET', url, 'Quick Search');
				}
				
				function out(){
					display("out", false);
				}
							
				function display(div, status){
					if(status){
						document.getElementById(div).style.display="block";
						document.getElementById(div).style.visibility="visible";
					}
					else{
						document.getElementById(div).style.display="none";
						document.getElementById(div).style.visibility="hidden";
					}
				}

				//set hidden_input
				function setHiddenInput(){
					document.getElementById("hidden_input").value=0;
				} 
				
				//set Text when chose from search input
				function setText(id, text){
					//alert(id);
					document.getElementById("hidden_input").value=id;
					document.getElementById("search_input").value=text;					
					display("search_display_type", false);
				}
								
				//registration.php
				function callRegisAva(){
					empty_ava(true);
					_username=document.getElementById('reg_username').value;
					
					rq_register_ava('GET', 'registration_avaiable.php?username='+_username, 'val_avaiable', 'Register Here');
				}
				
				//Vote for this deal.coupon
				function voteAssis(id){
					window.open("/vote_assis.php?id="+id, "voteCoupon", "channelmode=no, width=590px, height=340px, top=100px, left=200px, scrollbars=yes", true);
				}		
				
				function voteCoupon(id){
					sRandom=document.getElementById("vote_random_input").value;
					sInput=document.getElementById("vote_input").value;									
					if(sRandom!=sInput){
						display("vote_notice", true);						
						return false;
					}
					else{
						window.opener.location.reload();
						window.open("vote_result.php?id="+id, "voteCoupon", "channelmode=no, width=590px, height=300px, top=200px, left=200px, scrollbars=yes", true);	
						
					}
					
					/*
					sMethod='GET';
					
					sUrlA="forget_assis_result.php";
					sDiv="vote_notice";
					href=document.location.href;
					href=href.split("#");
					href=href[1];
					headerA="#"+href;
					
					sUrlB="vote_result.php";
					sUnder="Vote Result";					
					headerB="#vote_result";
					*/
//					rq_forget(sRandom, sInput, sMethod, sUrlA, sDiv, headerA, sUrlB, sUnder, headerB);
	//				window.open("vote_assis.php", "voteCoupon", "channelmode=no, width=590px, height=300px, top=200px, left=200px, scrollbars=yes", true);

				}
				
				//Vote for this article
				function voteAssisArticle(id){
					window.open("vote_assis_article.php?id="+id, "voteArticle", "channelmode=no, width=590px, height=420px, top=100px, left=200px, scrollbars=yes", true);
				}
				
				function voteArticle(id){					
					sRandom=document.getElementById("vote_random_input").value;
					sInput=document.getElementById("vote_input").value;									
					if(sRandom!=sInput){
						display("vote_notice", true);
						return false;						
					}
					else{
						window.open("vote_article_result.php?id="+id, "voteArticle", "channelmode=no, width=590px, height=300px, top=200px, left=200px, scrollbars=yes", true);	
					}
				
				}
				
				//Tell a Friend
				//t_link --> link of site, t_link_url --> link of site relative
				function tellFriend(t_link){		
					//alert(t_link);
					window.open("/tell_friend.php?t_link="+t_link, "tellFriend", "channelmode=no, width=590px, height=300px, top=200px, left=200px, scrollbars=yes", true);
				}
				
				//validate
				function validate_tell(type, value, div, ok_div, check_div){
					notice="";			
					error=false;
					switch(type){
						case 1:
						//fullname
						if(value.length<1){								
							notice="Min of 1 characters in length.";		
							error=true;
						}
						display(check_div, error);
						display(ok_div, !error);																		
						document.getElementById(div).innerHTML=notice;
						break;

						case 2:
							//email
							re=/^[a-zA-Z][\_\w\.]*@\w+(\.\w{2,3}){1,2}$/;
							if (value.length==0){
								notice="Didn't fill email.";
								error=true;
							}			
							else if(!re.test(value))
							{
								notice="Format: johnson@yahoo.com";
								error=true;
							}
							display(check_div, error);
							display(ok_div, !error);	
							document.getElementById(div).innerHTML=notice;
							break;
					}
					return notice;
				}
				
				//validate_tell_all
				function validate_tell_all(t_link){
					i_fullname="";
					i_email="";
					if(document.getElementById('tell_fullname')!=null){
						i_fullname=document.getElementById('tell_fullname').value;
						i_email=document.getElementById('tell_email').value;						
					}
					
					i_f_fullname=document.getElementById('tell_f_fullname').value;
					i_f_email=document.getElementById('tell_f_email').value;
										
					e_fullname=validate_tell(1, document.getElementById('tell_fullname').value, 'notice_name', 'ok_name', 'check_name');
					e_email=validate_tell(2, document.getElementById('tell_email').value, 'notice_email', 'ok_email', 'check_email');
					
					e_f_fullname=validate_tell(1, document.getElementById('tell_f_fullname').value, 'notice_f_name', 'ok_f_name', 'check_f_name');
					e_f_email=validate_tell(2, document.getElementById('tell_f_email').value, 'notice_f_email', 'ok_f_email', 'check_f_email');			
					
					notice="";
										
					if(e_fullname!=""){
						notice="- Your name: "+e_fullname+"\n";			
					}

					if(e_email!=""){
						notice+="- Your email: "+e_email+"\n";
					}			
					
					if(e_f_fullname!=""){
						notice+="- Your friend's name: "+e_f_fullname+"\n";
					}
					if(e_f_email!=""){
						notice+="- Your friend's email: "+e_f_email+"\n";
					}
					
					
					if(notice!=""){
						alert(notice);
					}
					else{
						document.location.href="#tell_friend_result/"+i_fullname+"/"+i_email+"/"+i_f_fullname+"/"+i_f_email+"/";
						url="tell_friend_result.php?fullname="+i_fullname+"&email="+i_email+"&f_fullname="+i_f_fullname+"&f_email="+i_f_email+"&t_link="+t_link;					
						
						window.open(url, "tellFriend", "channelmode=no, width=590px, height=300px, top=200px, left=200px, scrollbars=yes", true);
					}
					return notice;
				}
				
				
				
				//validate
				function validate(type, value){			
				notice="";			
				error=false;
				switch(type){
					case 1:
						//fullname
						if(value.length<1){								
							notice="Min of 1 characters in length.";		
							error=true;
						}
						display('check_name', error);
						display('ok_name', !error);																		
						document.getElementById("val_name").innerHTML=notice;
						break;		
							
					case 2:
						//username					
						if(value.length<6){										
							notice="Min of 6 characters in length.";		
							error=true;	
						}
						display('check_username', error);
						display('ok_username', !error);			
						document.getElementById("val_username").innerHTML=notice;
						break;
						
					case 3:
						//password
						if(value.length<6){			
							notice="Min of 6 characters in length.";
							error=true;
						}
						display('check_password', error);
						display('ok_password', !error);		
						document.getElementById("val_password").innerHTML=notice;				
						break;
						
					case 4:
						//confirm password
						pass=document.getElementById('reg_password').value;											

						if(value.length<6){			
							notice="Min of 6 characters in length.";		
							error=true;		
						}
						
						else if(value!=pass){
							notice="Not match with password.";		
							error=true;	
						}

						display('check_con_password', error);
						display('ok_con_password', !error);		
						document.getElementById("val_con_password").innerHTML=notice;				

						break;
						
					case 5:						
						//email
						re=/^[a-zA-Z][\_\w\.]*@\w+(\.\w{2,3}){1,2}$/;
						if (value.length==0){
							notice="Didn't fill email.";
							error=true;
						}			
						else if(!re.test(value))
						{
							notice="Format: johnson@yahoo.com";
							error=true;
						}
						display('check_email', error);
						display('ok_email', !error);	
						document.getElementById("val_email").innerHTML=notice;
						break;
						
					case 6:
						//old password
						if(value.length<6){			
							notice="Min of 6 characters in length.";
							error=true;
						}
						display('check_old_password', error);
						display('ok_old_password', !error);		
						document.getElementById("val_old_password").innerHTML=notice;				
						break;
					}	
					return notice;
				}

				//validate all for register 
				function validateAll(){	
					i_fullname=document.getElementById('reg_fullname').value;
					i_username=document.getElementById('reg_username').value;
					i_password=document.getElementById('reg_password').value;
					i_con_password=document.getElementById('reg_con_password').value;
					i_email=document.getElementById('reg_email').value;
					i_newsletter=document.getElementById('reg_news1').checked;
					i_newsletter=!i_newsletter;
										
					e_fullname=validate(1, document.getElementById('reg_fullname').value);
					e_password=validate(3, document.getElementById('reg_password').value);
					e_con_password=validate(4, document.getElementById('reg_con_password').value);					
					e_email=validate(5, document.getElementById('reg_email').value);
					e_username=validate(2, document.getElementById('reg_username').value);						
					
					
					notice="";
					
					no1=i_username+" is not avaiable";
					
										
					if(e_fullname!=""){
						notice+="- FullName: "+e_fullname+"\n";			
					}								
					
					if(e_username!=""){					
						notice+="- Username: "+e_username+"\n";
					}
					
					/*
					if(no2.match(no3)!=null&&e_username==""){
						notice+="- Username: "+no1+"\n";	
						display("ok_username", false);
					}
					else if(no2.match(no3)==null&&e_username==""){
						display("ok_username", false);	
					}*/
					
					checkUsername(e_username);
					no2=document.getElementById("exist_username").innerHTML;
					no3="is not avaiable";
					if(no2.match(no3)!=null){
						notice+="- Username: "+no2+".\n";	
					}
					
					
					if(e_password!=""){
						notice+="- Password: "+e_password+"\n";
					}
					if(e_con_password!=""){
						notice+="- Confirm Password: "+e_con_password+"\n";
					}
					if(e_email!=""){
						notice+="- Email: "+e_email+"\n";
					}
					
					if(notice!=""){
						alert(notice);
						return false;
					}
					/*
					else{
						i_password=MD5(i_password);
						document.location.href="#registration_result/"+i_fullname+"/"+i_username+"/"+i_password+"/"+i_email+"/Register Status";
						
						url="registration_result.php?fullname="+i_fullname+"&username="+i_username+"&password="+i_password+"&email="+i_email+"&newsletter="+i_newsletter;					
						rq_data('GET', url, 'Register Status');
					}*/		
					checkAllRegister();
				}				
				
				
				//check this username exist or no
				function checkUsername(i_username){						
					//test textbos username onclick
					//if not --> username (default) don't allow
					//if yes --> if username.length> 6 --> check is it existed?
					
					
					//if not --> username (default) don't allow
					i_reg_username_change=document.getElementById("reg_username_change").value;
					if((i_reg_username_change==0)&&(i_username.length==0)){
						i_notice="username is not avaiable.";

						//hide check, display ok
						display('check_username', true);
						display('ok_username', false);

						//input notice into div exist_username
						
						display('exist_username', true);						
						
						document.getElementById("exist_username").innerHTML=i_notice;							

					}

					//if yes --> if username.length> 6 --> check is it existed?					
					if(i_username.length>=6){		

						display('exist_username', true);
						rq_search('GET', 'registration_avaiable.php?username='+i_username, 'exist_username');								

						//undisplay check and ok when check length of username
						error=false;						
						display('check_username', error);
						display('ok_username', error);	
					}
					
					else if(i_reg_username_change==1&&i_username.length<6){						
						display('exist_username', false);
					}
					
				}
				
				function regUsernameChange(){
					document.getElementById("reg_username_change").value=1;
				}
				
				function regNewsChange(i_choice){
					if(i_choice == 1) 
					{
						document.getElementById("reg_news").style.display= "Block";
						
					}
					else 
					{
						document.getElementById("reg_news").style.display= "none";
						
					}
					document.getElementById("reg_news_hidden").value=i_choice;
					
				}
				
				
				//avaiable status in register			
				function empty_ava(status){
					display('val_avaiable', status);
					if(!status){
						document.getElementById('val_avaiable').innerHTML="";
					}
					
				}			
				
				function redirect(url){
					window.location.replace(url);	
				}
				
				function login(){								
					user=document.getElementById("log_username").value;
					pass=document.getElementById("log_password").value;
					url="/login.php?username="+user+"&password="+pass;
					redirect(url);
					//rq_login('GET', url, 'login');					
					//window.parent.location.reload();														
				
					//if logined + current page forgetpass--> change current page to homepage
					//checkPageCurrent("login", 1); --> must check --> it is in login.php
				}				
				
				function logout(){
					url="/logout.php";					
					redirect(url);
					//rq_login('GET', url, 'login');				
					//window.parent.location.reload();
					
					//if logouted + current page changepass--> change current page to homepage			
					//checkPageCurrent("logout", 1);
				}
				
				function checkPageCurrent(type, ok){					
					//type=="login", type=="logout"
					//if logined + current page forgetpass--> change current page to homepage
					
					var strmatch="";
					
					if(type=="login"){
						strmatch="forgetpass";	
					}
					else if(type=="logout"){
						strmatch="changepass";							
					}
					
					//ok==1 --> it real logined or logouted
					/*
					if(ok==1){
						i_href=""+window.parent.location.toString();				
						//alert(i_href);
						//alert("status: "+i_href.match(strmatch));
						if(i_href.match(strmatch)!=null){							
							redirectHome();	
						}		
						else{
							//alert('a');.
							window.history.back();	
						}
					}		
					else{
						window.history.back();	
					}*/
					
					//window.history.back();
					//window.location.reload();
					window.location.replace('/index.htm');


				}
				
				function getLocation(){					
					href=""+window.location;	
					if((href.match("login")==null)&&(href.match("logout")==null)){
						g_location=href;
//						alert("login or logout");	
						alert(g_location);
					}
				}
				
				
				function forgetpass(fUser){		
					sRandom=document.getElementById("forget_random_input").value;
					sInput=document.getElementById("forget_input").value;									
					if(sRandom!=sInput){
						document.getElementById("error").style.visibility="visible";
						return false;
					}
		/*			
					sMethod='GET';
					
					sUrlA="forget_assis_result.php";
					sDiv="forget_notice";
					href=document.location.href;
					href=href.split("#");
					href=href[1];
					headerA="#"+href;
					
					sUrlB="forget_result.php?username="+fUser;
					sUnder="Result";					
					headerB="#forget_result/"+fUser+"/Result";
					
					rq_forget(sRandom, sInput, sMethod, sUrlA, sDiv, headerA, sUrlB, sUnder, headerB);*/

				}
				
				//forgetpass.php
				function forgetpass_next(){
					document.location.href="#forget_assis/"+document.getElementById('f_username').value+"/Account Assistance";
					url='forget_assis.php?username='+document.getElementById('f_username').value;					
					rq_data('GET', url, 'Account Assistance');
				}
				
				function changepass(c_username){
					i_old_password=document.getElementById('reg_old_password').value;
					i_password=document.getElementById('reg_password').value;
					i_con_password=document.getElementById('reg_con_password').value;
										
					e_old_password=validate(6, document.getElementById('reg_old_password').value);
					e_password=validate(3, document.getElementById('reg_password').value);
					e_con_password=validate(4, document.getElementById('reg_con_password').value);								
					notice="";			
					
					if(e_password!=""){
						notice+="- Password: "+e_password+"\n";
					}
					if(e_con_password!=""){
						notice+="- Confirm Password: "+e_con_password+"\n";
					}
					
					if(notice!=""){
						alert(notice);						
						return false;
					}				
				
				}
				
				function reloadParent(c){	
					if(c<1){	
						alert("Incorrect password! Please try again!");
						window.history.back();
					}
					else{
						//come back homepage
						redirectHome();	
					}
				}
				

				
				//Other Page
				function general(url, text){
					rq_data('GET', url, text);
				}
				
				//newsletter
				function newsLetter(catid){				
					status=document.getElementById(catid).checked;
					catid=catid.substr(3);
					arr=document.getElementsByTagName("input");
					for(i=0; i<arr.length; i++){
						str=arr[i].id;						
						idx=str.indexOf("_");
						if(idx!=-1){						
							catid_sub=str.substr(0, idx);
							if(catid==catid_sub){
								check(str, status);								
							}
						}
					}

				}
				
				function newsLetterAll(status){
					arrA=document.getElementsByTagName("input");
					for(j=0; j<arrA.length; j++){
						strA=arrA[j].id;
						idxA=strA.indexOf("cat");
						if(idxA!=-1){
							check(strA, status);
							newsLetter(strA);
						}
						
					}
				}
				
				function checkAll(){				
					//validate
					//alert(document.getElementById("news_name"));
					//return false;
					
					//add list to array
					var arrChecked=new Array();
					var j=0;
					arrB=document.getElementsByTagName("input");	
					existName=false;
					for(i=0; i<arrB.length; i++){
						strB=arrB[i].id;
						type=arrB[i].type;						

						if(type=="checkbox"){
							status=arrB[i].checked;
							//alert(strB+": "+status);							
							if(status){								
								arrChecked[j++]=strB;								
							}
						}
						
					
						if(strB=="name"){
							existName=true;													
						}
																
					}
					
					var arrCat=new Array();
					var arrSub=new Array();
					c=0; s=0;				
					
					for(i=0; i<arrChecked.length; i++){
						strC=arrChecked[i];
						idx=strC.indexOf("cat");
					
						if(idx!=-1){
							//insert into arrCat
							
							catid=strC.substr(3);
							arrCat[c++]=catid;
						}
						else{
							//insert into arrSub if it didn't belong to cat which is in arrCat
							
							idxSub=strC.indexOf("sub");
							if(idxSub!=-1){
								catid_sub=strC.substr(0, idxSub-1);
								exist=false;
								for(k=0; k<arrCat.length; k++){
									if(catid_sub==arrCat[k]){
										exist=true;
									}										
								}
								if(!exist){
									subid=strC.substr(idxSub+4);
									arrSub[s++]=subid;
								}
							}
						}
						
						
					}
					
					//concat
					//name_news="empty";
					//email_news="empty";

					//if(existName){
						name_news=document.getElementById("news_name").value;
						email_news=document.getElementById("news_email").value;	
					//}
					
					
					url="newsletter_detail.php?name="+name_news+"&email="+email_news;	
					url+="&catid=";				
					
					for(i=0; i<arrCat.length; i++){
						url+=arrCat[i]+"/";
					}					
					if(arrCat.length>0) url=url.substr(0, url.length-1);
					else url+="0";
					
					url+="&subcatid=";
					for(i=0; i<arrSub.length; i++){
						url+=arrSub[i]+"/";
					}
					if(arrSub.length>0) url=url.substr(0, url.length-1);
					else url+="0";					
					
					//alert(url);
					window.location.replace(url);
					//rq_data('GET', url, 'Successful');
					
				}
				function check(input, status){
					document.getElementById(input).checked=status;
					document.getElementById(input).disabled=status;					
				}
				
				//newsletterRegister
				function newsLetterRegister(catid){				
					status=document.getElementById(catid).checked;
					catid=catid.substr(3);
					arr=document.getElementsByTagName("input");
					for(i=0; i<arr.length; i++){
						str=arr[i].id;						
						idx=str.indexOf("_");
						if(idx!=-1){						
							catid_sub=str.substr(0, idx);
							if(catid==catid_sub){
								check(str, status);								
							}
						}
					}
				}
				
				function newsLetterAllRegister(status){
					arrA=document.getElementsByTagName("input");
					for(j=0; j<arrA.length; j++){
						strA=arrA[j].id;
						idxA=strA.indexOf("cat");
						if(idxA!=-1){
							check(strA, status);
							newsLetterRegister(strA);
						}
						
					}
				}
				
				function checkAllRegister(){				
					//validate
					//alert(document.getElementById("news_name"));
					//return false;
					
					//add list to array
					var arrChecked=new Array();
					var j=0;
					arrB=document.getElementsByTagName("input");	
					existName=false;
					for(i=0; i<arrB.length; i++){
						strB=arrB[i].id;
						type=arrB[i].type;						

						if(type=="checkbox"){
							status=arrB[i].checked;
							//alert(strB+": "+status);							
							if(status){								
								arrChecked[j++]=strB;								
							}
						}
						
					
						if(strB=="name"){
							existName=true;													
						}
																
					}
					
					var arrCat=new Array();
					var arrSub=new Array();
					c=0; s=0;				
					
					for(i=0; i<arrChecked.length; i++){
						strC=arrChecked[i];
						idx=strC.indexOf("cat");
					
						if(idx!=-1){
							//insert into arrCat
							
							catid=strC.substr(3);
							arrCat[c++]=catid;
						}
						else{
							//insert into arrSub if it didn't belong to cat which is in arrCat
							
							idxSub=strC.indexOf("sub");
							if(idxSub!=-1){
								catid_sub=strC.substr(0, idxSub-1);
								exist=false;
								for(k=0; k<arrCat.length; k++){
									if(catid_sub==arrCat[k]){
										exist=true;
									}										
								}
								if(!exist){
									subid=strC.substr(idxSub+4);
									arrSub[s++]=subid;
								}
							}
						}
						
						
					}
					
					
					//url="registration_result.php?name="+name_news+"&email="+email_news;	
					//url="registration_result.php?catid=";				
					catid = "";
					for(i=0; i<arrCat.length; i++){
						catid+=arrCat[i]+"/";
					}					
					if(arrCat.length>0) catid=catid.substr(0, catid.length-1);
					else catid+="0";
						
					subcatid ="";
					for(i=0; i<arrSub.length; i++){
						subcatid+=arrSub[i]+"/";
					}
					if(arrSub.length>0) subcatid=subcatid.substr(0, subcatid.length-1);
					else subcatid+="0";					
					
				
					document.getElementById("subcatid").value = subcatid;
					document.getElementById("catid").value = catid
				
					
				}

				//contact us
				function contact_us(){					
					i_name=document.getElementById("c_name").value;
					i_company=document.getElementById("c_company").value;
					i_title=document.getElementById("c_title").value;					
					i_email=document.getElementById("c_email").value;
					i_question=document.getElementById("c_question").value;					
					
										
					e_name=validate_tell(1, document.getElementById('c_name').value, 'notice_name', 'ok_name', 'check_name');
					e_email=validate_tell(2, document.getElementById('c_email').value, 'notice_email', 'ok_email', 'check_email');
					e_question="";
					if(i_question==""){
						e_question="Question cannot empty.";						
					}
					
					notice="";
										
					if(e_name!=""){
						notice="- Your name: "+e_name+"\n";			
					}

					if(e_email!=""){
						notice+="- Your email: "+e_email+"\n";
					}			
									
					if(e_question!=""){
						notice+="- Your question: "+e_question+"\n";
					}
										

					if(notice!=""){
						alert(notice);
						return false;
					}
					/*
					else{
						
						document.location.href="#contact_us_result/"+i_name+"/"+i_company+"/"+i_title+"/"+i_email+"/"+i_question+"/Contact Us";
						url="contact_us_result.php?name="+i_name+"&company="+i_company+"&title="+i_title+"&email="+i_email+"&question="+i_question;					
						
						rq_data('GET', url, 'Contact Us');
					}*/
				}
				
				
				
				//validate
				function validate_contact(type, value, div, ok_div, check_div){
					notice="";			
					error=false;
					switch(type){
						case 1:
						//fullname
						if(value.length<1){								
							notice="Min of 1 characters in length.";		
							error=true;
						}
						display(check_div, error);
						display(ok_div, !error);																		
						document.getElementById(div).innerHTML=notice;
						break;

						case 2:
							//email
							re=/^[a-zA-Z][\_\w\.]*@\w+(\.\w{2,3}){1,2}$/;
							if (value.length==0){
								notice="Didn't fill email.";
								error=true;
							}			
							else if(!re.test(value))
							{
								notice="Format: johnson@yahoo.com";
								error=true;
							}
							display(check_div, error);
							display(ok_div, !error);	
							document.getElementById(div).innerHTML=notice;
							break;

						case 3:
							//phone		
							i_phone=new Number(value);
							//alert("fdsa"+i_phone);
							if(i_phone!="NaN"){
								//alert('fda');
								notice="Not number.";
								error=true;
							}
							display(check_div, error);
							display(ok_div, !error);	
							document.getElementById(div).innerHTML=notice;
							break;
					}
					return notice;
				}
				
				function getSubCategory(id, name, href, cat){
					rq_cat('GET', 'coupon.php?CategoryID='+id, name, href);
					g_url2='coupon.php?CategoryID='+id;
					g_catname=name;
					g_cathref=href;

				}
				
				function getCoupons(i_categoryID, i_type, i_page, i_title, i_a_href){					
					i_url="coupons.php?categoryID="+i_categoryID+"&type="+i_type+"&page="+i_page+"&title="+i_title;					
					rq_data('GET', i_url, i_title);
				}
				
				//change link: coupons?php?categoryID=1 --> coupons/1
				function separation(url){
						
				}
				
				
				//not full for any subcategory to display coupon --> only for subcategory in Coupon By Category
				function getCouponClick(i_subid, i_subname, i_catid, i_catname, i_page){

					//catid, subid
					url1="coupons_sub.php?categoryID="+i_subid+"&type=none&season=0&page="+i_page+"&title="+i_subname+"&super_categoryID="+i_catid+"&super_categoryName="+i_catname;
					//alert(url1);
					//alert(url1);
					url2="coupon.php?CategoryID="+i_catid;
					i_cathref="#coupon/"+i_catid+"/"+i_catname;
					i_click="javascript:rq_cat('GET', '"+url2+"', '"+i_catname+"', '"+i_cathref+"')";			
					g_url2=url2;
					g_catname=i_catname;
					g_cathref=i_cathref;

					rq_sub('GET', url1, i_subname, i_catname, i_cathref, i_click);
									
				}
				
				
				//full for any category or subcategory to display coupon
				
				function getCouponPageClick(i_subcatid, i_subname, i_catid, i_catname, i_type, i_season, i_page, i_title){	
					
					url1="coupons_sub.php?categoryID="+i_subcatid+"&type="+i_type+"&season="+i_season+"&page="+i_page+"&title="+i_title+"&super_categoryID="+i_catid+"&super_categoryName="+i_catname;					
		//			alert(url1);
					url2="coupon.php?CategoryID="+i_catid;
					i_cathref="#coupon/"+i_catid+"/"+i_catname;
					i_click="javascript:rq_cat('GET', '"+url2+"', '"+i_catname+"', '"+i_cathref+"')";		
					g_url2=url2;
					g_catname=i_catname;
					g_cathref=i_cathref;
					
					rq_sub('GET', url1, i_subname, i_catname, i_cathref, i_click);
				}
				
				function back_subcategory(){
					rq_cat('GET', g_url2, g_catname, g_cathref);
				}
				
				//full for any category or subcategory to display coupon
				
				function getMainCouponPageClick(i_catid, i_name, i_type, i_season, i_page, i_title){	
					i_url='coupons.php?categoryID='+i_catid+'&type='+i_type+'&season='+i_season+'&page='+i_page+'&title='+i_title;
					rq_data('GET', i_url, i_title);
				}
				
				//open page relative coupon
				function openPageRelative(i_link){					
					window.open(i_link);
				}
				
				function useful(i_type){					
					var i_url=window.location.href;
					var i_link;
					var g_title="Coupons House";
					i_title=document.getElementById("latestArticle");
					i_cat=document.getElementById("linkCat");
					i_sub=document.getElementById("linkSub");					
					
					//get generate title					
					if(i_title!=null){
						g_title+=" - "+i_title.innerHTML;
					}
					
					//get name of category
					if(i_cat!=null){
						g_title+=" - "+i_cat.innerHTML;
					}
					
					//get name of subcategory if have
					if(i_sub!=null){
						g_title+=" - "+i_sub.innerHTML;
					}
	
					switch(i_type){
						//Tag on del.icio.us
						case "delicious":							
							i_link="http://del.icio.us/post?url="+i_url+"&title="+g_title;
							break;
							
						//Add to StumbleUpon
						case "stumble":
							i_link="http://www.stumbleupon.com/submit?url="+i_url+"&title="+g_title;
							
							break;

						//Digg this	
						case "digg":							
							i_link="http://digg.com/submit?url="+i_url+"&title="+g_title;							
							break;
						
						//Add to Blinklist
						case "blink":							
							i_link="http://www.blinklist.com/index.php?Action=Blink/addblink.php&Description=&Url="+i_url+"&Title="+g_title;
							
							break;
					}
					openPageRelative(i_link);
					
				}
				
				function redirectHome(){
					t=setTimeout('redirectHome', 10000);
					window.location.replace("/index.htm");	
					clearTimeout(t);
				}
				
				function request(){
					var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
					document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));					
					try {
						var pageTracker = _gat._getTracker("UA-1703421-3");
						pageTracker._trackPageview();
					} 
					catch(err) {}
				}				

				//store detail page
				function Link_Popup(id){
					window.open("/store_details.php?id="+id, "voteCoupon", "channelmode=no, width=600px, height=350px, top=100px, left=200px, scrollbars=yes", true);
				}		


			
