function trim(str)
{
	return str.replace(/^\s*|\s*$/g,"");
}

function MM_preloadImages() 
{ 
	var d=document; 
	if(d.images)
	{ 
		if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
		for(i=0; i<a.length; i++){
			if (a[i].indexOf("#")!=0)
			{ 
				d.MM_p[j]=new Image; 	
				d.MM_p[j++].src=a[i];
			}
		}
	}
}

function bookmarkCode()
{	
	document.write('<a class="addthis_button" href="http://www.addthis.com/bookmark.php?v=250&amp;pub=xa-4b0a89da077d614e"><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pub=xa-4b0a89da077d614e"></script>');
}

function addBookmark()
{
	bookmarkurl = "http://www.greenbuildnews.co.uk/";
	bookmarktitle="Green build NEWS";
	if (document.all)
	window.external.AddFavorite(bookmarkurl,bookmarktitle)
	else if ( window.sidebar ) {
	window.sidebar.addPanel(bookmarktitle, bookmarkurl,"");
	}
}

function GetXmlHttpObject_ms()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if(window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}

function ajaxPost()
{
	if(xmlHttp.readyState == 4)
	{
		var retString = xmlHttp.responseText;
		//alert(retString); return false;
	}
}


function popupWindow(str,W,H)
{
	window.open(str,'MyPopUp','width='+W+',height='+H+',toolbar=0,scrollbars=1,screenX=10,screenY=10,left=0,top=0');
}


var xmlhttpCat;
var str="";
function loadXMLCat(url)
{
// code for Mozilla, etc.

	if (window.XMLHttpRequest)
	  {
		  xmlhttpCat=new XMLHttpRequest();
		  xmlhttpCat.open("GET",url,false);
		  xmlhttpCat.send(null);
		  
		  if (xmlhttpCat.readyState==4 && xmlhttpCat.status==200)
		  str = xmlhttpCat.responseText;
	  }
// code for IE
	else if (window.ActiveXObject)
	  {
	  	xmlhttpCat=new ActiveXObject("Microsoft.XMLHTTP");
		if (xmlhttpCat)
		{
		    xmlhttpCat.open("GET",url,false);
		    xmlhttpCat.send();
		    
		    if (xmlhttpCat.readyState==4 && xmlhttpCat.status==200)
		    str = xmlhttpCat.responseText;
		}
	  }	  	 
	  return str;
}

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 : dd/mm/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
}

function checkImageType(imgName)
{
	if(imgName.length>0)
	{
		imgName=imgName.toLowerCase();
		nmArray=imgName.split(".");
		nmlength=nmArray.length;
		imgType=nmArray[nmlength-1];
		if(imgType=="jpg" || imgType=="jpeg" || imgType=="pjpeg" || imgType=="png" || imgType=="gif")
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}

function checkDocType(imgName)
{
	if(imgName.length>0)
	{
		imgName=imgName.toLowerCase();
		nmArray=imgName.split(".");
		nmlength=nmArray.length;
		imgType=nmArray[nmlength-1];
		if(imgType=="doc" || imgType=="pdf" || imgType=="txt")
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}

function checkURL(value) 
{
	var urlregex = new RegExp("^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)");
	if(urlregex.test(value))
	{
		return(true);
	}
	return(false);
}

function validateEmailtofriend()
{
	var dt = document.frm;	
	if(trim(dt.From_Name.value)=="")
	{
		alert("Please enter from name.");
		dt.From_Name.focus();
		document.getElementById("From_Name_Id").style.backgroundColor = "#FEE9E7";
		return false;
	}
	else
	{
		document.getElementById("From_Name_Id").style.backgroundColor = "#EDFCE0";
	}
	
	if(trim(dt.From_Email.value)=="")
	{
		alert("Please enter from email.");
		dt.From_Email.focus();
		document.getElementById("From_Email_Id").style.backgroundColor = "#FEE9E7";
		return false;
	}	
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(trim(dt.From_Email.value)))) 
	{
		alert("Please enter valid from email."); 
		dt.From_Email.focus();
		document.getElementById("From_Email_Id").style.backgroundColor = "#FEE9E7";
		return false;
	}
	else
	{
		document.getElementById("From_Email_Id").style.backgroundColor = "#EDFCE0";
	}	
	if( trim(dt.Friend_Email.value)=="" )
	{
		alert("Please enter friend(s) email.");
		dt.Friend_Email.focus();
		document.getElementById("Friend_Email_Id").style.backgroundColor = "#FEE9E7";
		return false;
	}
	else
	{
		document.getElementById("Friend_Email_Id").style.backgroundColor = "#EDFCE0";
	}	
	return true;
}

function validateContactus()
{
	var dt = document.frmContactus;	
	if(trim(dt.Name.value)=="")
	{
		alert("Please enter name.");
		dt.Name.focus();
		document.getElementById("Name_Id").style.backgroundColor = "#FEE9E7";
		return false;
	}
	else
	{
		document.getElementById("Name_Id").style.backgroundColor = "#EDFCE0";
	}
	
	if(trim(dt.Email.value)=="")
	{
		alert("Please enter email.");
		dt.Email.focus();
		document.getElementById("Email_Id").style.backgroundColor = "#FEE9E7";
		return false;
	}	
	if( trim(dt.Email.value)!="" && !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(trim(dt.Email.value)))) 
	{
		alert("Please enter valid email."); 
		dt.Email.focus();
		document.getElementById("Email_Id").style.backgroundColor = "#FEE9E7";
		return false;
	}
	else
	{
		document.getElementById("Email_Id").style.backgroundColor = "#EDFCE0";
	}	
	if(trim(dt.private_key.value)=="")
	{
		alert("Please enter security code.");
		dt.private_key.focus();
		document.getElementById("private_key_Id").style.backgroundColor = "#FEE9E7";
		return false;
	}
	else
	{
		document.getElementById("private_key_Id").style.backgroundColor = "#EDFCE0";
	}	
	return true;
}

function validateNewslettersignup()
{
	var dt = document.frmJoinnow;	
	if(trim(dt.Member_Name.value)=="")
	{
		alert("Please enter name.");
		dt.Member_Name.focus();
		document.getElementById("Member_Name_Id").style.backgroundColor = "#FEE9E7";
		return false;
	}
	else
	{
		document.getElementById("Member_Name_Id").style.backgroundColor = "#EDFCE0";
	}
	
	if(trim(dt.Member_Email.value)=="")
	{
		alert("Please enter email.");
		dt.Member_Email.focus();
		document.getElementById("Member_Email_Id").style.backgroundColor = "#FEE9E7";
		return false;
	}	
	if( trim(dt.Member_Email.value)!="" && !(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(trim(dt.Member_Email.value)))) 
	{
		alert("Please enter valid email."); 
		dt.Member_Email.focus();
		document.getElementById("Member_Email_Id").style.backgroundColor = "#FEE9E7";
		return false;
	}
	else
	{
		document.getElementById("Member_Email_Id").style.backgroundColor = "#EDFCE0";
	}		
	return true;
}
