//generic function to display a record on a page.
function norun(){}
function sendwebcontact(){
	var Name = document.quote.Name.value;
	var Phone = document.quote.Phone.value;
	var PostCode = document.quote.PostCode.value;
	var Email = document.quote.Email.value;
	var EmailConfirm = document.quote.EmailConfirm.value;
	var Comments = document.quote.Comments.value;

	$("#storetheform").slideUp('fast',function(){
		
		$.post("inc/mailform.php", { Name: Name, Phone: Phone, PostCode: PostCode, Email: Email, EmailConfirm: EmailConfirm, Comments: Comments}, function(data){
				$('#submittheform').html(data); 
			
		});
	});

}
function show_contact(state,page_table){
	//$('#contact_content').load('inc/contact_us_loader.php');
	if(state == 'United Kingdom')
	{
		$.post('inc/contact_us_address_uk.php', { state: state, page_table: page_table}, function(data){
			$('#contactHeadOffice').html(data);	
		});
	}
	$.post('inc/contact_us_loader.php', { state: state, page_table: page_table}, function(data){
		$('#contact_content').html(data);	
	});
}

function displayrecord(divnum,info,mode,removeload){

	if(mode=='display'){

		for (i=0; i<document['record_edit'+divnum].elements.length; i++){

			if(document['record_edit'+divnum].elements[i].type=="textarea"){

				tinyMCE.execCommand('mceRemoveControl', false, document['record_edit'+divnum].elements[i].id);

			}

		}

	}

	if($.browser.msie!=true){

		if(mode!=null){		

			$('#record'+divnum).slideUp('slow',function(){

				$.post('inc/showrecord.php', { info: info, mode: mode}, function(data){

					$('#record'+divnum).html(data);	

					$('#record'+divnum).slideDown('slow', function(){

																   if(removeload!=null){$(removeload).html('');}

																   });

				});

			});

		}else{

			$.post('inc/showrecord.php', { info: info, mode: mode}, function(data){

				$('#record'+divnum).html(data);	

			});

		}

	}else{

		$.post('inc/showrecord.php', { info: info, mode: mode}, function(data){

				$('#record'+divnum).html(data);	

				if(removeload!=null){$(removeload).html('');}

		});

	}



}



function addrecord(divnum,info,reloadafter){

	

	$('#record'+divnum+' > #edit_menu > table > tbody > tr > td > #loadanim').html('<img src="jscripts/ajax-loader.gif"/>');

	$('#record'+(divnum)).before('<div id="record'+(numrecords)+'"></div>');



	numrecords = numrecords + 1;

	var removeload = '#record'+divnum+' > #edit_menu > table > tbody > tr > td > #loadanim';

	$.post('inc/addrecord.php', { info: info, numrecords: numrecords, removeload: removeload, reloadafter:reloadafter}, function(data){

		$('#runsql').html(data);	

	});

}



function deleterecord(divnum,info){

	if(confirm ("Delete this record?")){

		if($.browser.msie!=true){

			$('#record'+divnum).slideUp('slow',function(){

				$('#record'+(divnum)).remove();										

			});

		}else{

			$('#record'+(divnum)).remove();		

		}

		$.post('inc/deleterecord.php', { info: info, numrecords: numrecords}, function(data){

			$('#runsql').html(data);

		});

	}

}



function savechanges(info,divnum,removemce){

	//clean up text areas

	for (i=0; i<removemce.length; i++){

		tinyMCE.execCommand('mceRemoveControl', false, removemce[i]);

	}

	

	var fieldarray = new Array();

	var valuearray = new Array();

	//tinyMCE.triggerSave();

	var count = 0;

  	for (i=0; i<document['record_edit'+divnum].elements.length; i++){

		if(document['record_edit'+divnum].elements[i].type!="BUTTON"){

			if(document['record_edit'+divnum].elements[i].name!=""){

				fieldarray[count] = document['record_edit'+divnum].elements[i].name;

				valuearray[count] = document['record_edit'+divnum].elements[i].value;

				count++ ;

			}

		}

	}

	$.post('inc/savechanges.php', { info: info, fieldarray: fieldarray, valuearray:valuearray }, function(data){

		$('#runsql').html(data);	

	});

}



function start_tinymce(addmce){

	for (i=0; i<addmce.length; i++){

		tinyMCE.execCommand('mceAddControl', false, addmce[i]);

	}

}



function stop_tinymce(removemce){

	for (i=0; i<removemce.length; i++){

		tinyMCE.execCommand('mceRemoveControl', false, removemce[i]);

	}

}



function addpicture(divnum,LinkID,Gallery){

	$.post('inc/addpicture.php', { divnum: divnum, LinkID: LinkID, Gallery:Gallery, Picture:document['getimg'+divnum]['imghold'+divnum].value }, function(data){

		$('#runsql').html(data);	

	});

}



var miniGalleryOptions1 = {

      thumbnailId: 'thumb1', slideshowGroup: 'group1'

   }



   // Make all images animate to the one visible thumbnail

   var miniGalleryOptions2 = {

      thumbnailId: 'thumb2', slideshowGroup: 'group2'

   }



   // Make all images animate to the one visible thumbnail

   var miniGalleryOptions3 = {

      thumbnailId: 'thumb3', slideshowGroup: 'group3'

   }







function addpreviewimg(divnum,picnum,image,thumb,incdelete,highslide){

	if(highslide!='no'){

		if(incdelete=='false'){

		window['config' + divnum] = {

			slideshowGroup: 'group'+divnum,

			numberPosition: 'caption',

			transitions: ['expand', 'crossfade']

		};

		$('#imgpreview'+divnum+' > ul').append('<li id="highslidepic"><table cellpadding="0px" cellspacing="0px"><tr><td height="170px" width="170px" valign="middle" align="center"><a style="margin: auto; border: 0px;" href="'+image+'" class="highslide" onclick="return hs.expand(this, config'+divnum+' )"><img border="0" src="'+thumb+'"  alt=""/></a></td></tr></table></li>');	

	}else{

		window['econfig' + divnum] = {

			slideshowGroup: 'group'+divnum,

			numberPosition: 'caption',

			transitions: ['expand', 'crossfade']

		};

		$('#imgpreview'+divnum+' > ul').append('<li id="'+picnum+'"><div id="delete"><a onclick="deletepicture(\''+divnum+'\',\''+picnum+'\')"></a></div><table cellpadding="0px" cellspacing="0px"><tr><td height="143px" width="143px" valign="middle" align="center"><a style="margin: auto;" href="'+image+'" class="highslide" onclick="return hs.expand(this, econfig'+divnum+' )"><img src="'+thumb+'"  alt=""/></a></td></tr></table></li>');	

	}

	}else{

		$('#imgpreview'+divnum).append('<li id="highslidepic"><table cellpadding="0px" cellspacing="0px"><tr><td height="170px" width="170px" valign="middle" align="center"><img border="0" src="'+thumb+'"  alt=""/></td></tr></table></li>');		

	}



}



function deletepicture(divnum,picnum){

	$('#imgpreview'+divnum+' > ul > li#'+picnum).fadeOut('slow',function(){

		$.post('inc/deletepicture.php', { picnum: picnum }, function(data){

			$('#runsql').html(data);	

		});		 

																		 

	});	

}




function createpages(loadtodiv,total,perpage,onpage,page,view){

	//alert('loadto: '+loadtodiv+' , total: '+total+' , perpage: '+perpage+' , onpage: '+onpage+'');

	var pagetext = '';

	pagetext = pagetext + '<ul>';

	var nopages = Math.ceil(total/perpage);

	if(nopages>1){

		if(onpage<nopages){

			pagetext = pagetext + '<li><a href="?page='+page+'&view='+view+'&p='+(onpage+1)+'&l='+perpage+'">></a></li>';

		}

		if(nopages<5){

			for (i=0; i<nopages; i++){

				if((nopages-i)==onpage){

					pagetext = pagetext + '<li><a class="onpage" ';

				}else{

					pagetext = pagetext + '<li><a ';

				}

				pagetext = pagetext + 'href="?page='+page+'&view='+view+'&p='+(nopages-i)+'&l='+perpage+'">'+(nopages-i)+'</a></li>';	

			}

		}else{

			if(onpage>nopages-6){

				for (i=0; i<6; i++){

					if((nopages-i)==onpage){

						pagetext = pagetext + '<li><a class="onpage" ';

					}else{

						pagetext = pagetext + '<li><a ';

					}

					pagetext = pagetext + 'href="?page='+page+'&view='+view+'&p='+(nopages-i)+'&l='+perpage+'">'+(nopages-i)+'</a></li>';	

				}		

			}else{

				pagetext = pagetext + '<li><a href="?page='+page+'&view='+view+'&p='+(nopages)+'&l='+perpage+'">'+(nopages)+'</a></li>';	

				pagetext = pagetext + '<li><a href="?page='+page+'&view='+view+'&p='+(nopages-1)+'&l='+perpage+'">'+(nopages-1)+'</a></li>';	

				pagetext = pagetext + '<li><a class="pagedots"></a></li>';	

				if(onpage==1){

					pagetext = pagetext + '<li><a href="?page='+page+'&view='+view+'&p=3&l='+perpage+'">3</a></li>';

					pagetext = pagetext + '<li><a href="?page='+page+'&view='+view+'&p=2&l='+perpage+'">2</a></li>';

					pagetext = pagetext + '<li><a class="onpage" href="?page='+page+'&view='+view+'&p=1&l='+perpage+'">1</a></li>';

				}else{

					pagetext = pagetext + '<li><a href="?page='+page+'&view='+view+'&p='+(onpage+1)+'&l='+perpage+'">'+(onpage+1)+'</a></li>';	

					pagetext = pagetext + '<li><a class="onpage" href="?page='+page+'&view='+view+'&p='+(onpage)+'&l='+perpage+'">'+(onpage)+'</a></li>';	

					pagetext = pagetext + '<li><a href="?page='+page+'&view='+view+'&p='+(onpage-1)+'&l='+perpage+'">'+(onpage-1)+'</a></li>';	

				}

			}

			

		}

		

		if(onpage!=1){

			pagetext = pagetext + '<li><a href="?page='+page+'&view='+view+'&p='+(onpage-1)+'&l='+perpage+'"><</a></li>';

			pagetext = pagetext + '<li><a href="?page='+page+'&view='+view+'&p=1&l='+perpage+'"><<</a></li>';

		}

		pagetext = pagetext + '</ul>';

		$('#'+loadtodiv).append(pagetext);

	}

}



















//////////////////////OLD TOOLS////////////////////////////////////



function loaddiv(loadto,content,animate,setmceto,removemcefrom){

	if(removemcefrom!=null){

		removemce(removemcefrom);

	}

	

	if($.browser.msie!=true){ //Test if displaying in internet explorer

		loadto = '#'+loadto;

		if(animate=='true' ){

			$(loadto).slideUp('slow',function(){

				$(loadto).load(content, function(){

					$(loadto).slideDown('slow');

					if(setmceto!=null){

						setmce(setmceto);

					}

					

				});

			});

	

		}else{

			$(loadto).load(content, function(){

				if(setmceto!=null){

					setmce(setmceto);

				}

			});

		}

	}else{

		if(animate=='true' ){

			ajax_loadContent(loadto,content);

		}else{

			ajax_loadContent(loadto,content);

		}

	}

}



function addnew(table,ID){

	if(numrecords>0){

		$('#article'+(numrecords-1)).after('<div id="article'+numrecords+'"></div>');

	}

	numrecords = numrecords + 1;

	var i = 0;

	loaddiv('sqlentry','inc/add.php?table='+table+'&ID='+ID,'false');

	if($.browser.msie==true){

		 location.reload();	

	}

}



function loaddiv(loadto,content,animate,setmceto,removemcefrom){

	loadto = '#'+loadto;

	$(loadto).load(content);	

/*	if(removemcefrom!=null){

		removemce(removemcefrom);

	}

	

	if($.browser.msie!=true){ //Test if displaying in internet explorer

		loadto = '#'+loadto;

		if(animate=='true' ){

			$(loadto).slideUp('slow',function(){

				$(loadto).load(content, function(){

					$(loadto).slideDown('slow');

					if(setmceto!=null){

						setmce(setmceto);

					}

					

				});

			});

	

		}else{

			$(loadto).load(content, function(){

				if(setmceto!=null){

					setmce(setmceto);

				}

			});

		}

	}else{

		if(animate=='true' ){

			ajax_loadContent(loadto,content);

		}else{

			ajax_loadContent(loadto,content);

		}

	}*/

}





function processSQL_articles(divid,divno){

	var articleno = "article_edit"+divno;

	var bodyno = "Body"+divno;

	removemce(divid);

	var Status = document[articleno].Status.value;

	var DateEntered = document[articleno].DateEntered.value;

	var Title = document[articleno].Title.value;

	var Body = document[articleno][bodyno].value;

	var articleno = document[articleno].ArticleNo.value;

	$("#article"+divno).slideUp('slow',function(){

			$.post("inc/process.php", { Type: "articles", Status: Status, DateEntered: DateEntered, Title: Title, Body: Body, ArticleNo:articleno, divid:divid}, function(data){

			$('#article'+divno).html(data); 

			if($.browser.msie!=true){

				loaddiv('article'+divno,'article.php?divno='+divno+'&articleno='+articleno+'&mode=display&type=articles','true');

			}else{

			 location.reload();	

			}

		});

	});

}





function processSQL_merchandise(divid,divno){

	var articleno = "article_edit"+divno;

	var bodyno = "Body"+divno;

	removemce(divid);

	var Status = document[articleno].Status.value;

	var DateEntered = document[articleno].DateEntered.value;

	var Name = document[articleno].Name.value;

	var Price = document[articleno].Price.value;

	var Description = document[articleno][bodyno].value;

	var articleno = document[articleno].ArticleNo.value;

	$("#article"+divno).slideUp('slow',function(){

			$.post("inc/process.php", { Type: "merchandise", Status: Status, DateEntered: DateEntered, Name: Name, Description: Description, Price: Price, ArticleNo:articleno, divid:divid}, function(data){

			$('#article'+divno).html(data); 

			if($.browser.msie!=true){

				loaddiv('article'+divno,'article.php?divno='+divno+'&articleno='+articleno+'&mode=display&type=merchandise','true');

			}else{

			 location.reload();	

			}

		});

	});

}





function processSQL_events(divid,divno){

	var articleno = "article_edit"+divno;

	var bodyno = "Body"+divno;

	removemce(divid);

	var DateEntered = document[articleno].DateEntered.value;

	var Title = document[articleno].Title.value;

	var Body = document[articleno][bodyno].value;

	var articleno = document[articleno].ArticleNo.value;



	$("#article"+divno).slideUp('slow',function(){

			$.post("inc/process.php", { Type: "events", DateEntered: DateEntered, Title: Title, Body: Body, ArticleNo:articleno, divid:divid}, function(data){

			$('#article'+divno).html(data); 

			if($.browser.msie!=true){

				loaddiv('article'+divno,'article.php?divno='+divno+'&articleno='+articleno+'&mode=display&type=events','true');

			}else{

			 location.reload();	

			}

		});

	});

}



function processSQL_page(divid,divno,pagename){

	var articleno = "article_edit"+divno;

	var bodyno = "Body"+divno;

	removemce(divid);

	var Body = document[articleno][bodyno].value;

	$("#article"+divno).slideUp('slow',function(){

			$.post("inc/process.php", { Type: "pages", Pagename: pagename, Body: Body}, function(data){

			$('#article'+divno).html(data); 

			if($.browser.msie!=true){

				loaddiv('article'+divno,'article.php?divno='+divno+'&pagename='+pagename+'&mode=display&type=pages','true');

			}else{

			 location.reload();	

			}

		});

	});

}





function setmce(divid){

	tinyMCE.execCommand('mceRemoveControl', false, divid);

	tinyMCE.execCommand('mceAddControl', false, divid);

}

function removemce(divid){tinyMCE.execCommand('mceRemoveControl', false, divid);}



function warning(divid,warning){

	$('#'+divid).innerHTLM = warning;

}



function checkEnter(e){

	if (e.keyCode == 13) {

        logmein();

        return false;

    }

}

function logmein(){

	var username = document.login.username.value

	var password = document.login.password.value

	var rememberme = document.login.rememberme.value

	loaddiv('logindiv','inc/runlogin.php?user='+username+'&pass='+password+'&rememberme='+rememberme,'true');	

}



function logout(){
	loaddiv('login','inc/logout.php','true');
}





//*********************DATE VALIDATION*****************************//

function validatedate(validatefield,datediv,divno){

	var articleno = "record_edit"+divno;



	if(isDate(document['record_edit'+divno][validatefield].value)){

		$('#datevalidation'+divno).load('inc/rightorwrong.php?answer=right');	

	}else{

		$('#datevalidation'+divno).load('inc/rightorwrong.php?answer=wrong&message=dd/mm/yyyy');	

	}

}

function test(msg){

	alert(msg);	

}





var dtCh= "/";

var minYear=1900;

var maxYear=2100;



function isInteger(s){

	var i;

    for (i = 0; i < s.length; i++){   

        // Check that current character is number.

        var c = s.charAt(i);

        if (((c < "0") || (c > "9"))) return false;

    }

    // All characters are numbers.

    return true;

}



function stripCharsInBag(s, bag){

	var i;

    var returnString = "";

    // Search through string's characters one by one.

    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++){   

        var c = s.charAt(i);

        if (bag.indexOf(c) == -1) returnString += c;

    }

    return returnString;

}



function daysInFebruary (year){

	// February has 29 days in any year evenly divisible by four,

    // EXCEPT for centurial years which are not also divisible by 400.

    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );

}

function DaysArray(n) {

	for (var i = 1; i <= n; i++) {

		this[i] = 31

		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}

		if (i==2) {this[i] = 29}

   } 

   return this

}



function isDate(dtStr){

	var daysInMonth = DaysArray(12)

	var pos1=dtStr.indexOf(dtCh)

	var pos2=dtStr.indexOf(dtCh,pos1+1)

	var strDay=dtStr.substring(0,pos1)

	var strMonth=dtStr.substring(pos1+1,pos2)

	var strYear=dtStr.substring(pos2+1)

	strYr=strYear

	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)

	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)

	for (var i = 1; i <= 3; i++) {

		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)

	}

	month=parseInt(strMonth)

	day=parseInt(strDay)

	year=parseInt(strYr)

	if (pos1==-1 || pos2==-1){

		//alert("The date format should be : mm/dd/yyyy")

		return false

	}

	if (strMonth.length<1 || month<1 || month>12){

		//alert("Please enter a valid month")

		return false

	}

	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){

		//alert("Please enter a valid day")

		return false

	}

	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){

		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)

		return false

	}

	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){

		//alert("Please enter a valid date")

		return false

	}

return true

}

 

 //*********************END DATE VALIDATION*****************************//
