function getHTTPObject() {

  var xmlhttp = null;

  if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        xmlhttp = new XMLHttpRequest();
        if (xmlhttp.overrideMimeType) {
                xmlhttp.overrideMimeType('text/xml');
        }
  } else if (window.ActiveXObject) { // IE
        try {
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
                try {
                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
        }
}


  return xmlhttp;

}


var http = getHTTPObject();

function validate_email(email)
{
        var notice = document.getElementById('notice_email');
        var apos;
        var dotpos;

        with(email)
        {

                apos=value.indexOf("@");
                dotpos=value.lastIndexOf(".");
                if (value == "" || apos<1 || dotpos-apos<2)
                {

                        notice.innerHTML = "This email address is not valid";
                        notice.style.display = "block";
                        return 0;
                }
        }
                        return 1;
}

function check_user(email)
{
        var notice = document.getElementById('notice_email');
        var submit = document.getElementById('submit');

	submit.disabled = false;

        notice.style.display = "none";

	with(email)
	{	
	out='email='+value;
	}
	
        var url = 'user.php?action=check';

        http.open('POST', url, true);

        http.onreadystatechange = function (){

                                        if (http.readyState == 4) {

                                                var result = null;
                                                var notice = document.getElementById('notice_email');
						var submit = document.getElementById('submit');
                                                result = http.responseText;


                                                if(result.length<4){
                                                        submit.disabled = false;
							return 1;
                                                }else{
                                                        notice.innerHTML = "This username already exists";
                                                        notice.style.display = "block";
							submit.disabled = true;	
                                                }
                                        }
                                }
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http.setRequestHeader("Content-length", out.length);
        http.setRequestHeader("Connection", "close");
        http.send(out);
}

function required(input)
{

	with (input)
	{
  		if (value==null||value=="")
  		{
			style.border = '2px solid red';
			return 0;
	 	}
		else
			return 1;
	}

}

function hide_div(div_id)
{
	with(div_id)
	{
		style.display = "none";
	}
}

function show_div(div_id)
{
        with(div_id)
        {
		if(div_id == my_phones)
			show_phones();
                style.display = "block";
        }
}


function show_phones()
{
    $.ajax({
        type: "post",
        cache: false,
        url: "includes/cellphones.php?action=view",
        success: function(data_returned){
            $('#show_phones').html(data_returned);
        }
    });
}

function disp_confirm(id,type,action)
{
        var r=confirm("Are you sure you want to proceed?");
        if(type == 'user')
	        url='../'+type+'.php?action='+type+'_'+action+'&id='+id;
        else
	        url=type+'.php?action='+type+'_'+action+'&id='+id;
        if (r==true)
        {
                window.location.href =url;
                return true;
        }
}

function update_vmc(machine,command)
{
	var i=0;
	for (i=0; i< machine.length;i++)
	{
		$.ajax({
		type: "post",
		data: "&ajax=1",
		url:	"thr/command.php?vmc="+machine[i]+'&action='+command,
		cache: false,
		success: function(data_returned){
	   	}

	});
	}
}



function vmp_command(id,action,message)
{
        var r=confirm("Are you sure you want to proceed? "+message);
	url='thr/command.php?action='+action+'&vmc='+id;
        if (r==true)
        {
                window.location.href =url;
                return true;
        }
}
function ajax_radio(input,type)
{
	var product_radio = input;
	$.ajax({
		type: "post",
		data: "&product_radio="+product_radio,
		cache: false,
		url:type+".php?action=view_"+type,
		success: function(data_returned){
			$('#tableresult').html(data_returned);
	         
        	}

	});
}


function edit(id,type)
{
	$.ajax({
		type: "post",
		data: "&id="+id,
		cache: false,
		success: function(data_returned){
	             window.location.href ="index.php?action=edit_"+type;
        	}

	});
}

function deactivate_checkbox(id,type,input)
{
        if(input.checked)
                var action = 0;
        else
                var action = 1;
        $.ajax({
                                type: "post",
                                data:"&id="+id+"&active="+action,
                                cache: false,
                                url: type+".php?action=activate_"+type
                        });
}

function activate_checkbox(id,type,input)
{
        if(input.checked)
                var action = 1;
        else
                var action = 0;
        $.ajax({
                                type: "post",
                                data:"&id="+id+"&active="+action,
                                cache: false,
                                url: type+".php?action=activate_"+type
                        });
}

function change_status(id,input)
{
    var link="&id="+id+"&status="+input.value;
     $.ajax({
        type: "post",
        data: link,
        cache: false,
        url:"content.php?action=update_status&option=status"

        });
}
function sort_column(type,column)
{
	var perpage_res = document.pages.perpage_res.value;
        $.ajax({
                                type: "post",
                                data:"&column_"+type+"="+column+"&perpage_res="+perpage_res,
                                cache: false,
                                url: type+".php?action=view_"+type,
				success: function(data_returned){
							if(type == 'provider' || type == 'service')
		                     	$('#'+type+' #tableresult').html(data_returned);
							 else
							 	$('#tableresult').html(data_returned);
					 			document.pages.page_no.value=1;
                		}

                        });
}

function gotopage(page,type,perpage_num,last)
{
	$.ajax({
			type: "post",
			data:"&perpage_res="+perpage_num+"&page_no="+page,
			cache: false,
			url: type+".php?action=view_"+type,
			success: function(data_returned){
			$('#tableresult').html(data_returned);
			document.pages.page_no.value = page;
					/*if(page == 1)
					{
						$('.first').hide();
						$('.prev').hide();
					}
					else if(last == 0)
					{
						$('.next').hide();
						$('.last').hide()
						$('.first').show();
						$('.prev').show();
					}
					else
					{
						$('.next').show();
                        $('.last').show()
                        $('.first').show();
                        $('.prev').show();
					}
					*/
				
			}
		  });
}

function enter_pressed(char,element)
{
        if(char.keyCode==13)
                element.submit();
}


function call_results(total,numPages)
{
	var currentPage =0;

        var $pager = $('<div class="pager"></div>');

        for (var page = 0; page < numPages; page++) {

        $('<span class="page-number">' + (page + 1) + '</span>')

	 .click(function() {

          currentPage = page;

          repaginate();

        })

        .appendTo($pager).addClass('clickable');

        }

        $pager.insertBefore($('.callresults'));

}
function upload_file(file)
{
	$.ajax({
		type: "post",
		cache: false,
		url: "test_upload.php",
		success: function(data_returned)
		{
		$('#view_results').html(data_returned);
		}
    	});
}
function change_sorting(direction,id,action)
{
	if(action=='newsflash')
	{
		var data_out = "&direction="+direction+"&category_id="+id;
	}
	else
		var data_out = "&direction="+direction+"&category_id="+id;
	$.ajax({
		type: "post",
		data: data_out,
		cache: false,
		url: "content.php?action="+action+"_update&option=sorting",
		success: function(data_returned)
		{
		$('#view_'+action).html(data_returned);
		}
    	});
}
function remove_image(id,article_id,image_type,filename,page_no,per_page_no)
{
	link = "&image_id="+id+"&type="+image_type+"&filename="+filename;
    switch(image_type)
    {
        case 'edit_product':
            action_id ="product_id";
        break;
        case 'edit_customer':
            action_id ="customer_id";
        break;
        case 'edit_project':
            action_id ="project_id";
        break;
        case 'edit_sitecontent':
            action_id ="content_id";
        break;
        case 'edit_news':
            action_id ="news_id";
        break;
    }

	$.ajax({
		type: "post",
		data: link,
		cache: false,
		url: "content.php?action=image_remove",
		success: function(data_returned)
		{
				if(image_type=='newsflash')
					$('#view_newsflash').html(data_returned);
				else if(image_type=='menuimage')
					$('#view_menuimages').html(data_returned);
                else
                    window.location.href ="index.php?action="+image_type+"&"+action_id+"="+article_id;
				
			
		}
    	});
}
function edit_image(image_id,action)
{
	if(action==0)
	{
		document.getElementById('description_'+image_id).style.display = "none";
		document.getElementById('edit_description_'+image_id).style.display = "block";
	}
	else
	{
		var description = document.getElementById('edit_description_'+image_id).value;
		$.ajax({
			type: "post",
			data: "&image_id="+image_id+"&edit_description="+description,
			cache: false,
			url: "../content.php?action=edit_image",
			success: function()
			{
				if(description=='')
					description = '&nbsp';
				$('#description_'+image_id).show();
				$('#description_'+image_id).html(description);
				$('#edit_description_'+image_id).hide();
			}
    	});
	}
}
function block_user(id,block)
{
	$.ajax({
		type: "post",
		data: "&user_id="+id+"&block="+block,
		cache: false,
		url: "content.php?action=block_user",
		success: function(data_returned)
		{
		$('#view_user').html(data_returned);
		}
    	});
}

function display_column(div_id)
{
	if($(div_id).is(':visible'))
	{
		$(div_id).hide('slow');//animate({marginLeft:'300',opacity:'slow' },{ duration:1000 ,complete : function(){ $(this).hide(); } });
		display_next(div_id);
	}
	else
	{

		$('.column_body').each(function(){
				if($(this).is(':visible'))
						$(this).hide('slow');//animate({marginLeft:'-300',opacity:'slow' },{complete :function(){ $(this).css('margin-left','300px') } });
						});
		$(div_id).show('slow');//.animate({marginLeft:'0',opacity:'slow' },{duration:1000 } );
	}
}

function display_next(div_id)
{
	
}


function display_photo(div_id)
{
        with(div_id)
        {
		if(style.display=='block')
                	style.display = 'none';
		else
			style.display ='block'
        }
}

function results(page_no,per_page_no,flash,action)
{
        if(action!='news' && action!='branch')
		    link= action+".php?action=view_"+action+"s";
        else
            link= "../"+action+".php?action=view_"+action;
		$.ajax({
		type: "post",
		cache: false,
		url: link+"&page_no="+page_no+"&per_page_no="+per_page_no,
		success: function(data_returned)
		{
			$('.page_curr').hide();
			$('.page_link').show();
			$('#page_link_'+page_no).hide();
			$('#page_curr_'+page_no).show();
			$('#view_results').html(data_returned);
		}
    	});
}

function choose_article(article_id,flash)
{
	if(window.opener)
	{
		var loc = window.opener.document.getElementById('location').value;
	}
	else
		var loc = document.getElementById('location').value;

	$.ajax({
		type: "post",
			
		data: "&article_id="+article_id+"&flash="+flash,
		cache: false,
		url: "../content.php?action=newsflash_update&option=choose_article&location="+loc,
		success: function(data_returned)
		{
			//$('#view_newsflash').html(data_returned);
			if(window.opener)
			{
				window.opener.document.getElementById('view_newsflash').innerHTML = data_returned;
				window.close();
			}
			else
				$('#view_newsflash').html(data_returned);
		}
    	});

}
function select_newsflash(input)
{
	var loc= input.value;
	$.ajax({
		type: "post",
		cache: false,
		url: "forms/edit_newsflash.php?location="+loc,
		success: function(data_returned)
		{
				$('#view_newsflash').html(data_returned);
		}
    	});

}

function select_banner(input)
{
        var loc= input.value;
        $.ajax({
                type: "post",
		data: "&location="+loc,
                cache: false,
                url: "forms/edit_banner_content.php",
                success: function(data_returned)
                {
                                $('#banner_content').html(data_returned);
                }
        });

}
function choose_image(image_id,filename,flash)
{
	if(window.opener)
	{
		window.opener.document.getElementById('file_input_'+flash).style.display = "none";
		window.opener.document.getElementById('existing_description_'+flash).style.display = "none";
		window.opener.document.getElementById('existing_image_'+flash).value = filename;
		window.opener.document.getElementById('existing_check_'+flash).checked = "checked";
		window.opener.document.getElementById('existing_check_'+flash).value = image_id;
		window.opener.document.getElementById('existing_input_'+flash).style.display = "block";
		window.close();
	}
	else
	{

		if(!image_id.checked)
		{
			document.getElementById('existing_image_'+flash).value = null;
			document.getElementById('existing_check_'+flash).value = null;
			document.getElementById('existing_input_'+flash).style.display = "none";
			document.getElementById('file_input_'+flash).style.display = "block";
			document.getElementById('existing_description_'+flash).style.display = "block";

		}
	}
}

function change_gallery(week)
{
	link = "photo_gallery2.php";
	$.ajax({
		type: "post",
			
		data: "&week="+week,
		cache: false,
		url: link,
		success: function(data_returned)
		{
			$('#mcphoto').html(data_returned);
		}
    	});

}
function popup(mylink, windowname)
{
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
   		href=mylink;
	else
   		href=mylink.href;
	window.open(href, windowname, 'width=1200,height=800,scrollbars=yes');
	return false;
}

function addfield(num_input,link)
{
	num_input++;
	var needed_vals = new Array;	
	var j=0;
	$('#repeating_field .needed').each(function()
			{
		       	
			needed_vals[j] = this.value;
		       	j++;
	       	});


	$.ajax({
		type: "post",
		data:"&num_input="+num_input,
		cache: false,
		url: link,
		success: function(data_returned)
		{
			var i=0;
			$('#repeating_field').html(data_returned);
			for(i=0;i<j;i++)
			{
				$('#repeating_field .needed:eq('+i+')').val(needed_vals[i]);
			}
		}
    	});


}

function addfield_new(num_input,link)
{
	num_input++;

	$.ajax({
		type: "post",
		data:"&num_input="+num_input,
		cache: false,
		url: link,
		success: function(data_returned)
		{
			$('#partner_field').html(data_returned);
		}
    	});


}

function fill_in(action,input)
{
	with(input)
	{
		category=value;
	}
	if(action=='column')
	{
		$.ajax({
			type: "post",
			data:"&category="+category,
			cache: false,
			url: "../content.php?action=column_fill",
			success: function(data_returned)
			{
				vars = data_returned.split("&&");
				document.add_column.title.value = vars[0];
				document.add_column.author.value = vars[1];
			}
    		});	
	}
}

function basic_article(article_id,category_id,input)
{
	var check_name;
	var check_id;
	if(input.checked)
	{
		check_name = input.name;
		check_id = input.id;
		$.ajax({
				type: "post",
				data:"&article_id="+article_id+'&category_id='+category_id,
				cache: false,
				url: "../content.php?action=basic_article",
				success: function()
				{
						
						$('.check').each(function(){
							if(this.name == check_name)
							{
								if(this.id != check_id)
									this.checked=false;
							}
							});
				}
	    		});
	}	
}

function deactivate_article(id,input)
{
	if(input.checked)
		var action = 1;
	else
		var action = 0;
	$.ajax({
				type: "post",
				data:"&article_id="+id+"&active="+action,
				cache: false,
				url: "../content.php?action=deactivate_article"
	    		});
}
function update(column,link,action)
{

	var post = column.name;
	var value = column.value;
        $.ajax({
                type: "post",
                data:"&"+post+"="+value,
                cache: false,
                url: link,
                success: function(data_returned)
                {
                        $('#'+action).html(data_returned);
                }
        });
}
function ajax_link(link,action)
{

    $.ajax({
        //type: "post",
        cache: false,
        url: link,
        success: function(data_returned)
        {
            $('.'+action).html(data_returned);
        }
    });
}

function send_request(action)
{
if(action == 'submit')
{
    $('#enquiry').ajaxSubmit({
    
        success: function(data_returned)
        {
            $('#popup .details').hide();
            $('#popup .display').hide();
            $('#popup .result').html(data_returned);
            $('#popup .result').show();
        }
    });
}
else if(action == 'back')
{
     $('#popup .result').hide();
     $('#popup .details').show();
     $('#popup .display').show();

}
}

function collapse(myclass,action,signid)
{

    if(action=='collapse')
    {
        $('div#sign_'+signid).html('<a href="javascript:collapse(\''+myclass+'\',\'expand\','+signid+')">+</a>');
        $('div .child_'+signid).each(function(){
            if($(this).hasClass('parent'))
            {
                var curr_id = $(this).attr('id');
                $('div .child_'+curr_id).each(function(){
                             $(this).hide();
                });
            }
            $(this).hide();
        });
    }
    else if(action == 'expand')
    {
        $('div#sign_'+signid).html('<a href="javascript:collapse(\''+myclass+'\',\'collapse\','+signid+')">-</a>');
        $('div .child_'+signid).each(function(){
             if($(this).hasClass('parent'))
             {
                var curr_id = $(this).attr('id');
                $('div .child_'+curr_id).each(function(){
                $(this).show();
                });
             }
                $(this).show();
        });
    }
    
}

$(document).ready(function() {



		

  $('.callresults').each(function() {

    var currPage = 0;

    var numPPage = 20;

    var $mydiv = $(this);
	
	var total = $('.rowresult').size();
	
	var numPages = Math.ceil(total/numPPage);
	
	var repaginate = function(){	
   	$('.headingresults').show();
   	$('.rowresult').hide()
	.slice(currPage*numPPage,currPage*numPPage +numPPage )
	.show()
	.end()
	};
		
	var $pager = $('<div class="pager"></div>');

        for (var page = 0; page < numPages; page++) {
		if(page == 0)
			var msg = page + 1;
		else
			var msg =', '+(page+1);
		$('<a href="javascript://" class="page-number" >' + msg + '</a>')

		.bind('click', {'newPage': page}, function(event) {

    		currPage = event.data['newPage'];

    		repaginate();

		$(this).addClass('active').siblings().removeClass('active');

  		})
		.appendTo($pager).addClass('clickable');
        }
	$pager.find('a.page-number:first').addClass('active');
        $pager.insertBefore($('.tableresults'));
	
	repaginate();
  });


});

