var xmlHttp_obj=null;

function GetxmlHttp_object(){
	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 sendRequest(page,method,formname,divId){
	xmlHttp_obj=GetxmlHttp_object()
	if (xmlHttp_obj==null){
	  alert ("Your browser does not support AJAX!");
	  return false;
	} 
	
	var url=page;
	xmlHttp_obj.onreadystatechange=function(){stateChanged_fnc(divId)};
	//xmlHttp_obj.open("GET",url,true);
	//xmlHttp_obj.send(null);
	
	if(method == "POST"){
		xmlHttp_obj.open('POST',page,true);
		// set form http header
		xmlHttp_obj.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
		// get form values and send http request
		xmlHttp_obj.send(getFormValues(formname));
	
	
	}else if(method == "GET"){
	
		xmlHttp_obj.open('GET',page,true);
		
		xmlHttp_obj.send(null);						
	}
 
}

function stateChanged_fnc(divId){ 
	if (xmlHttp_obj.readyState==4){ 
	 	document.getElementById(divId).innerHTML=xmlHttp_obj.responseText;	
		var sPath = window.location.pathname;
		//var sPage = sPath.substring(sPath.lastIndexOf('\\') + 1);
		var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
		//alert(sPage);
		
		/*if(sPage=="register.php"){
			window.location="index.php";
		}else 	if(divId=="loginDiv"){
			//window.location.reload();	
			if(sPage=="checkout.php"){
				//alert(sPage);
				window.location.reload();
			}
		}*/
	}
}
function getFormValues(fobjname){
	var fobj=document.getElementById(fobjname);
    var str='';
    for(var i=0;i< fobj.elements.length;i++){
       if(fobj.elements[i].type=="checkbox" ){
			if(fobj.elements[i].checked==true){
				str+=fobj.elements[i].name+'='+escape(fobj.elements[i].value)+'&';//escape		
			}
		}else{
        	str+=fobj.elements[i].name+'='+escape(fobj.elements[i].value)+'&';//escape
		}
    }
    str=str.substr(0,(str.length-1));
	//alert(str); 
    return str;
}

function displayServerResponse(){
	var output = xmlHttp_obj.responseText;	
	document.location.href = output;	
}


var timerID,timerID2;
var cont=0;	 
function show2(id){	
	clearTimeout(timerID);
	timerID=0;
	clearTimeout(timerID2);
	timerID2=0;
	document.getElementById(id).innerHTML="";
	document.getElementById(id).style.display='none';
}
function loadTimer(id){     
setInterval("show2('"+id+"')",5000);
}
function showmsg(id){
	cont=cont+1;
	//alert(cont+""+id);
	document.getElementById(id).style.display='';
	document.getElementById(id).innerHTML='<b>Added to Cart!</b>';
	if(document.getElementById(id)!=null && document.getElementById(id).className=="gray-text"){
		document.getElementById(id).className="yellow-text";
	}else if(document.getElementById(id)!=null && document.getElementById(id).className=="yellow-text"){
		document.getElementById(id).className="gray-text";
	}
	if(cont==7){
	 show2(id);
	 cont=0;	 
	}
}


function subscribe_enter(e){ 
	 if(window.event) {
	  keynum = event.keyCode;
	 }else if(e.which) {
	  keynum = e.which;
	 }
	 if(keynum==13){
		 submSubscription();
	   return false;
	 } 
}
function blankbox(t){
	if(t.value=="Name" || t.value=="Email"){
		 t.value="";
	}
}
function defaultbox(t,val){
	if(t.value==""){
		t.value=val;
	}
}
function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}	
function setCookie(c_name,value,expiredays){
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}
function DeleteCookie( name, path, domain ) {
	//alert(document.cookie);
 if ( getCookie( name ) ) {
	// alert("ok");
	var cookie_date = new Date ( );  // current date & time
	cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = name + "=" +
    ( ( path ) ? "; path=" + path : "" ) +
    ( ( domain ) ? "; domain=" + domain : "" ) +
    "; expires="+ cookie_date.toGMTString();//Thu, 01-Jan-70 00:00:01 GMT
 }
 //alert(document.cookie);
}

function checkCookie(name){
	disclaimer=getCookie(name)
	if (disclaimer !=null && disclaimer !=""){
		return true;
	}else {
		return false;
	}
} 
	
function page_transfer(name,t,url){
	//document.cookie=name+"="+t;
	if(name=='subcat_id' || name=='cat_id'){
		if(checkCookie("subcat_id")==true){
			DeleteCookie("subcat_id");
		}
		if(checkCookie("prod_id")==true){
			DeleteCookie("prod_id");
		}
		if(checkCookie("cat_id")==true){
			DeleteCookie("cat_id");
		}
		if(checkCookie("pageno")==true){
			DeleteCookie("pageno");
		}
	}
	if(checkCookie(name)==true){
		DeleteCookie(name);
	}
	if(name!=""){
		setCookie(name,t,100);
		if(checkCookie(name)==true){
			window.location=url;
		}else{
			msg = "Please enable Cookies to view this site properly";
			alert(msg);	
		}
	}else{
		window.location=url;	
	}
}

function page_move(url){
		if(checkCookie("subcat_id")==true){
			DeleteCookie("subcat_id");
		}
		if(checkCookie("prod_id")==true){
			DeleteCookie("prod_id");
		}
		if(checkCookie("cat_id")==true){
			DeleteCookie("cat_id");
		}
		if(checkCookie("pageno")==true){
			DeleteCookie("pageno");
		}
	window.location=url;
}

