
//---------------------------------------------------
 /* Program Name            : Dextor.php
    Description             : Created by Piyush Dixit 
    Call                    : On top of every page
    Functionality           : Common javascript functions 
    Author                  : Piyush Dixit < Email : mystery1984@gmail.com > <Mob : +91.9891549233)
    Creation Date           : 24-October-09
    Modification History    : None
	Note		    : Don't modify before informing Author*/
//---------------------------------------------------

// JavaScript Document
function call(target_url,return_target_pos,getform,method,poststr,oncom_function,is_synchronous)
{	
	
	//----------------------------------------------
	//Get post parameter
	//----------------------------------------------
        if(!poststr)var poststr='';
	if(document.getElementById(getform))
	{
		poststr+=(poststr.substr(poststr.length-1,poststr.length-1)=='&')?'':'&';
		var form=document.getElementById(getform);
		for(var i=0;i<form.elements.length;i++)
		{
			if(form.elements[i].multiple&&form.elements[i].name)
			{
				var optval='';
				var opt=form.elements[i].options;
				for(var j=0;j<opt.length;j++)
				{
					if(opt[j].selected)
						optval+=opt[j].value+'~';
				}
				//optval=optval.substring(0,optval.length-2)
				poststr +=form.elements[i].name+'='+encodeURIComponent(optval);
			}
			else if(form.elements[i].type=='checkbox')
			{
				if(form.elements[i].name)
				{
					if(form.elements[i].checked)
						poststr +=form.elements[i].name+'='+encodeURIComponent(form.elements[i].value);
					else
						poststr +=form.elements[i].name+'=';
				}
			}
			else if(form.elements[i].type=='radio')
			{
				if(form.elements[i].checked&&form.elements[i].name)
					poststr +=form.elements[i].name+'='+encodeURIComponent(form.elements[i].value);
			}
			else if(form.elements[i].name)
			{
				poststr +=form.elements[i].name+'='+encodeURIComponent(form.elements[i].value);
			}
			
			if(i!=form.elements.length-1&&form.elements[i].name)
				poststr+='&'
		}	
		
	}
	
	if(method=='get'||method=='GET')
	{
		if(target_url.indexOf('?')==-1)
			target_url+='?'+poststr;
		else
			target_url+='&'+poststr;
		poststr='';	
	}
		
	
	
	//----------------------------------------------
	//Preloader
	//----------------------------------------------
	
	
	if(return_target_pos) 
	{
		var tmp_innerHTML=document.getElementById(return_target_pos).innerHTML;
		
	}
	var connectionerror='';

	//----------------------------------------------
	//Creating connection request
	//----------------------------------------------
	
	var xmlhttp = false;
	if (window.XMLHttpRequest) 
	{ 
		xmlhttp = new XMLHttpRequest();if (xmlhttp.overrideMimeType) xmlhttp.overrideMimeType('text/html'); 
	} 
	else if (window.ActiveXObject) 
	{ 
		 try 
		 {
			  if (!(xmlhttp = new ActiveXObject("Msxml2.XMLHTTP")))alert(connectionerror)
			  
		 } 
		 catch (e) 
		 {
			 try 
			 {
			   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			 } catch (e) {alert(connectionerror)}
		 }
	}
	
	//----------------------------------------------
	//Comunicating
	//----------------------------------------------
	
	 if(!is_synchronous)
	 	type=true;
	 else
	 	type=false;
	 
	 xmlhttp.open('POST', target_url, type);
	 xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	 xmlhttp.setRequestHeader("Content-length", poststr.length);
	 xmlhttp.setRequestHeader("Connection", "close");
	 xmlhttp.send(poststr);
	 xmlhttp.onreadystatechange = function()
	 {
		 if(xmlhttp.readyState == 2)
		 {
			 // xmlhttp.abort(); 
		 }
		
		 if (xmlhttp.readyState == 4) 
		 {
				//alert(xmlhttp.status)
				if (xmlhttp.status == 200) 
				{
					result = xmlhttp.responseText; 
					var jscode = "";
					var parts = result.match(/<script[^>]*>(.|\n|\t|\r)*?<\/script>/gi);
					if(parts){
						for(i=0;i<parts.length;i++){
							jscode += parts[i].replace(/<script[^>]*>|<\/script>/gi, "");
							result = result.replace(parts[i], "");
						}
					}						
					if(return_target_pos)
					{
						//alert('POST');
						if(document.getElementById(return_target_pos))
						document.getElementById(return_target_pos).innerHTML=result
						
					}
					if(oncom_function)
						oncom_function(result);
					if(jscode != "")
					{
						var incomingScript = document.createElement('SCRIPT');
						incomingScript.type = 'text/javascript'; 
						incomingScript.lang = 'javascript';
						incomingScript.defer = true;
						incomingScript.text = jscode;
						document.getElementsByTagName('head')[0].appendChild(incomingScript);
					}	
					//alert(document.getElementsByTagName('head')[0].innerHTML);
					
					xmlhttp=false;
				} 
				else
				{
					if(return_target_pos)
						document.getElementById(return_target_pos).innerHTML=tmp_innerHTML;
					//if(confirm('Network connection diconnected. Do you want to resend the request.'))
						//call(target_url,return_target_pos,getform,method,poststr,oncom_function,preloader)
					
					
					//if(debug)
					//document.write('<div style="padding-top:150px;font-family:tahoma; font-size:11px; color:#FF0000; font-weight:bold" align="center">An error have been occured while communicating with server, <br/><br/> Error 404 : URL ( '+target_url+' ) not found  <br/><br/> Post Parameter : '+poststr+'</div>')
				}
			}
	 };
	 return xmlhttp;
}

function cdtime(container, targetdate, cur_date_string)
{
        
	if (!document.getElementById || !document.getElementById(container)) return
	this.container=document.getElementById(container);
       
	this.currentTime=new Date(cur_date_string);
	this.targetdate=new Date(targetdate);
	this.timesup=false;
	this.updateTime();
}
	cdtime.prototype.updateTime=function(){
	
        var thisobj=this
	
        this.currentTime.setSeconds(this.currentTime.getSeconds()+1)
	setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
	}
	cdtime.prototype.displaycountdown=function(baseunit, functionref){
	this.baseunit=baseunit
	this.formatresults=functionref
	this.showresults()
	}
	cdtime.prototype.showresults=function(){
	var thisobj=this
	var timediff=(this.targetdate-this.currentTime)/1000 //difference btw target date and current date, in seconds
	if (timediff<0){ //if time is up
	this.timesup=true
	this.container.innerHTML=this.formatresults()
	return
}
	var oneMinute=60 //minute unit in seconds
	var oneHour=60*60 //hour unit in seconds
	var oneDay=60*60*24 //day unit in seconds
	var dayfield=Math.floor(timediff/oneDay)
	var hourfield=Math.floor((timediff-dayfield*oneDay)/oneHour)
	var minutefield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour)/oneMinute)
	var secondfield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute))
	if (this.baseunit=="hours"){ //if base unit is hours, set "hourfield" to be topmost level
	hourfield=dayfield*24+hourfield
	dayfield="n/a"
	}
	else if (this.baseunit=="minutes"){ //if base unit is minutes, set "minutefield" to be topmost level
	minutefield=dayfield*24*60+hourfield*60+minutefield
	dayfield=hourfield="n/a"
	}
	else if (this.baseunit=="seconds"){ //if base unit is seconds, set "secondfield" to be topmost level
	var secondfield=timediff
	dayfield=hourfield=minutefield="n/a"
	}
	this.container.innerHTML=this.formatresults(dayfield, hourfield, minutefield, secondfield)
	setTimeout(function(){thisobj.showresults()}, 1000) //update results every second
}

function formatresults()
{
	if (this.timesup==false){//if target date/time not yet met
            var displaystring=arguments[0]+" days "+arguments[1]+" hours "+arguments[2]+" minutes "+arguments[3]+" seconds "
	}
	else
	{
	//else if target date/time met
	var displaystring="time ended.";
	}
	return displaystring;
}


