/**
 * site_functions.js - JavaScript Functions.
 * @package site_functions.js
 * @copyright 2005 - 2009 OSD
*/

/**
 * popUp Function
 * Launches pop up with given URL.
 * @param string URL (URL to include in the popup)
 * @param string height (height of the popup)
 * @param string width (width of the popup)
 * @param string scroll (boolean to show / hide the scrollbar)
 * @param string resize (boolean to enable / disable the resizing)
 * @access public
*/
function popUp(URL, height, width, scroll, resize) { 
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '"+id+"', 'toolbar=0,scrollbars="+scroll+ ",location=0,statusbar=0,menubar=0,resizable="+resize+",width="+width+",height="+height+",left = 10,top = 10');");
}


function CheckContactDetails(){ 
	var inputVal = document.form.email.value;
	pass = true;
	details = 'Invalid information entered.\n\n';

	if (document.form.name.value == '') { 
		details+= '- Please enter your Name.\n\n';
    	if (pass) document.form.name.focus();
		pass = false;
	}
	if (document.form.company.value == '') { 
		details+= '- Please enter your Company Name.\n\n';
    	if (pass) document.form.company.focus();
		pass = false;
	}
	if (document.form.address.value == '') { 
		details+= '- Please enter your Address.\n\n';
    	if (pass) document.form.address.focus();
		pass = false;
	}
	if (document.form.phone.value == '') { 
		details+= '- Please enter your Telephone Number.\n\n';
    	if (pass) document.form.phone.focus();
		pass = false;
	}
	if (inputVal == '') { 
		details+= '- Please enter an Email Address.\n\n';
    	if (pass) document.form.email.focus();
		pass = false;
	}
	else if (inputVal.indexOf("@") == -1){ 
		details+= '- Please enter a properly formatted Email Address.\n\n';
    	if (pass) document.form.email.focus();
		pass = false;
	}
	else if (inputVal.indexOf(".") == -1){ 
		details+= '- Please enter a properly formatted Email Address.\n\n';
    	if (pass) document.form.email.focus();
		pass = false;
	}
	if (document.form.user_code.value == '') { 
		details+= '- Please enter the letters found in the Image.\n\n';
    	if (pass) document.form.user_code.focus();
		pass = false;
	}
	if (pass) { 
		return true;
	}
	else { 
		details+= 'Please correct the above errors before proceeding.    \n\n';
		alert(details);
		return false;
	}
}

//**** start Email A Friend form field checks
function CheckEmailFriendDetails(p_id) { 
	var inputVal = document.form.email.value;
	var inputValfriend = document.form.emailfriend.value;
	pass = true;
	details = 'Invalid information entered.\n\n';

	if (document.form.name.value == '') {
		details+= '- Please enter Your Name.\n\n';
    	if (pass) document.form.name.focus();
		pass = false;
	}
	if (inputVal == '') {
		details+= '- Please enter Your Email Address.\n\n';
    	if (pass) document.form.email.focus();
		pass = false;
	}
	else if (inputVal.indexOf("@") == -1){
		details+= '- Please enter a properly formatted Email Address.\n\n';
    	if (pass) document.form.email.focus();
		pass = false;
	}
	else if (inputVal.indexOf(".") == -1){
		details+= '- Please enter a properly formatted Email Address.\n\n';
    	if (pass) document.form.email.focus();
		pass = false;
	}
	if (document.form.namefriend.value == '') {
		details+= '- Please enter Your Name.\n\n';
    	if (pass) document.form.namefriend.focus();
		pass = false;
	}
	if (inputValfriend == '') {
		details+= '- Please enter your Friends Email Address.\n\n';
    	if (pass) document.form.emailfriend.focus();
		pass = false;
	}
	else if (inputValfriend.indexOf("@") == -1){
		details+= '- Please enter a properly formatted Email Address.\n\n';
    	if (pass) document.form.emailfriend.focus();
		pass = false;
	}
	else if (inputValfriend.indexOf(".") == -1){
		details+= '- Please enter a properly formatted Email Address.\n\n';
    	if (pass) document.form.emailfriend.focus();
		pass = false;
	}
	if ((p_id == 0) && (document.form.user_code.value == '')) { 
		details+= '- Please enter the letters found in the Image.\n\n';
    	if (pass) document.form.user_code.focus();
		pass = false;
	}
	if (pass) { 
		return true;
	}
	else { 
		details+= 'Please correct the above errors before proceeding.    \n\n';
		alert(details);
		return false;
	}
}

function CheckNewsletterDetails(){
	var inputVal = document.formnews.email.value;
	pass = true;
	details = 'Invalid information entered.\n\n';

	if (inputVal == '') {
		details+= '- Please enter an Email Address.\n\n';
    	if (pass) document.formnews.email.focus();
		pass = false;
	}
	else if (inputVal.indexOf("@") == -1){
		details+= '- Please enter a properly formatted Email Address.\n\n';
    	if (pass) document.formnews.email.focus();
		pass = false;
	}
	else if (inputVal.indexOf(".") == -1){
		details+= '- Please enter a properly formatted Email Address.\n\n';
    	if (pass) document.formnews.email.focus();
		pass = false;
	}
	if (pass) { 
		return true;
	}
	else { 
		details+= 'Please correct the above errors before proceeding.    \n\n';
		alert(details);
		return false;
	}
}

function AddFavourites() { 
	var siteName = "domeafavour.ie";
	var siteAddress = "http://www.domeafavour.ie";
	var linkName = "<img src='/images/bookmark-icon.jpg'>";

	var NSstring = "<a href='javascript:alert(&#34;Click [Ctrl + D] to add " + siteName + " to your favourites\n\n\n\n\n&#34;);' title='[Ctrl + D] - add " + siteName + " to favourites'>" + linkName + "<\/a>";
	var OPstring = "<a href='javascript:alert(&#34;Click [Ctrl + T] to add " + siteName + " to your favourites&#34;);' title='[Ctrl + T] - add " + siteName + " to favourites'>" + linkName + "<\/a>";
	var IEstring = "<a href='javascript:window.external.addfavorite(\"" + siteAddress + "\",\"" + siteName + "\");'>" + linkName + "<\/a>";

	var OTHstring = "Bookmark " + siteName + "?";
	var whichString = OTHstring;
	var agt = navigator.userAgent.toLowerCase();
	var app = navigator.appName.toLowerCase();
	var ieAgent = agt.indexOf('msie');
	var nsAgent = app.indexOf('netscape');
	var opAgent = app.indexOf('opera');
	if (ieAgent!= -1) { whichString = IEstring; } else if (nsAgent!= -1){ whichString = NSstring; } else if (opAgent!= -1) {whichString = OPstring; }
	document.write(whichString);
}

function onlyNumbers(e) { 
	var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
	//alert(keyCode);
	if ((keyCode != 8) && (keyCode != 46) && (keyCode != 127) && ((keyCode < 48) || (keyCode > 57)))
		return false;
}

function onlyGoodChars(e) { 
	var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
	//alert(keyCode);
	if ((keyCode == 34) || (keyCode == 96))
		return false;
}

function is_not_array(obj) {
   return (obj.constructor.toString().indexOf("Array") == -1)
}

function categorySelect(page) { 
    var category_selected_id = $("#select_category").val();
    var category_selected_name = $("#select_category option:selected").attr("title").replace(/ /g, "-").toLowerCase();
    var category_url = page+category_selected_id+"/"+category_selected_name;
    if (category_selected_id != "") { 
        //alert("Cat URL : "+ category_url);
        window.location.href = category_url;
    }
}

function colourSelect() { 
    var search_colour = $("#search_colour").val();
    //alert("search_colour: "+ search_colour);
    $("#shop_colour").submit();
}


