/******************************************
 *
 * THIS FILE IS FOR SIMPLE JS CALLS THAT
 * MAY OR MAY NOT BE NEEDED
 *
 * CREATED BY: JUSTIN ST. GERMAIN
 *
 ******************************************/
function clickclear(thisfield, defaulttext){
	var obj = document.getElementById(thisfield);
    if (obj.value == defaulttext) {
        obj.value = "";
    }
}


function clickrecall(thisfield, defaulttext){
	var obj = document.getElementById(thisfield);
    if (obj.value == "") {
        obj.value = defaulttext;
    }
}


function sz(t){

    var t = document.getElementById(t);
    
    a = t.value.split('\n');
    b = 1;
    for (x = 0; x < a.length; x++) {
        if (a[x].length >= t.cols) 
            b += Math.floor(a[x].length / t.cols);
    }
    b += a.length;
    if (b > t.rows) 
        t.rows = b;
    
}


function toggleDisplay(id){

    var obj = document.getElementById(id);
    
    if (obj.style.display == "none") { // if it is checked, make it visible, if not, hide it
        obj.style.display = "block";
        createCookie("ibd_disp_" + id, "true", 0.01	);
    }
    else {
        obj.style.display = "none";
        eraseCookie("ibd_disp_" + id);
    }
    
}


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


function eraseCookie(name) {
	createCookie(name,"",-1);
}


function GetOptions(t, v, obj) {
	
	var t = document.getElementById(t);
	var obj = document.getElementById(obj);
	
	if(t.value == v) {
		obj.style.display = 'block';
	} else {
		obj.style.display = 'none';
	}
	
}


function toggleCheckbox(t, id){

    var t = document.getElementById(t);
    var obj = document.getElementById(id);
    
    if (obj.style.display == "none") { // if it is checked, make it visible, if not, hide it
        obj.style.display = "block";
        t.checked = "checked";
        
    }
    else {
    
        obj.style.display = "none";
        t.checked = "";
        
    }
    
}


function toggleOfferCheckbox(t, id){

    var t = document.getElementById(t);
    var obj = document.getElementById(id);
    
    if (obj.style.display == "none") { // if it is checked, make it visible, if not, hide it
		obj.style.display = "block";
		t.checked = "checked";
        
    }
    else {
    
		obj.style.display = "none";
		obj.value = "";
		t.checked = "";
        
    }
    
}


loader = new Image();
loader.src = "ajax-loader.gif";

loader2 = new Image();
loader2.src = "ajax-loader-t.gif";


var enablepersist = "on" //Enable saving state of content structure using session cookies? (on/off)
var collapseprevious = "no" //Collapse previously open content when opening present? (yes/no)
if (document.getElementById) {
    document.write('<style type="text/css">')
    document.write('.switchcontent{display:none;}')
    document.write('</style>')
}

function getElementbyClass(classname){
    ccollect = new Array()
    var inc = 0
    var alltags = document.all ? document.all : document.getElementsByTagName("*")
    for (i = 0; i < alltags.length; i++) {
        if (alltags[i].className == classname) 
            ccollect[inc++] = alltags[i]
    }
}

function contractcontent(omit){
    var inc = 0
    while (ccollect[inc]) {
        if (ccollect[inc].id != omit) 
            ccollect[inc].style.display = "none"
        inc++
    }
}

function expandcontent(cid){
    if (typeof ccollect != "undefined") {
        if (collapseprevious == "yes") 
            contractcontent(cid)
        document.getElementById(cid).style.display = (document.getElementById(cid).style.display != "block") ? "block" : "none"
    }
}

function revivecontent(){
    contractcontent("omitnothing")
    selectedItem = getselectedItem()
    selectedComponents = selectedItem.split("|")
    for (i = 0; i < selectedComponents.length - 1; i++) 
        document.getElementById(selectedComponents[i]).style.display = "block"
}

function get_cookie(Name){
    var search = Name + "="
    var returnvalue = "";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search)
        if (offset != -1) {
            offset += search.length
            end = document.cookie.indexOf(";", offset);
            if (end == -1) 
                end = document.cookie.length;
            returnvalue = unescape(document.cookie.substring(offset, end))
        }
    }
    return returnvalue;
}

function getselectedItem(){
    if (get_cookie(window.location.pathname) != "") {
        selectedItem = get_cookie(window.location.pathname)
        return selectedItem
    }
    else 
        return ""
}

function saveswitchstate(){
    var inc = 0, selectedItem = ""
    while (ccollect[inc]) {
        if (ccollect[inc].style.display == "block") 
            selectedItem += ccollect[inc].id + "|"
        inc++
    }
    
    document.cookie = window.location.pathname + "=" + selectedItem
}

function do_onload(){
    uniqueidn = window.location.pathname + "firsttimeload"
    getElementbyClass("switchcontent")
    if (enablepersist == "on" && typeof ccollect != "undefined") {
        document.cookie = (get_cookie(uniqueidn) == "") ? uniqueidn + "=1" : uniqueidn + "=0"
        firsttimeload = (get_cookie(uniqueidn) == 1) ? 1 : 0 //check if this is 1st page load
        if (!firsttimeload) 
            revivecontent()
    }
}


function limitlength (el, max, report) {
	
	if (el.value.length > max) {
		el.value = el.value.substring (0, max);
	}
	if (el.value.length < max-20) {
		document.getElementById(report).style.color = "#CCCCCC";
	}
	if (el.value.length >= max-20) {
		document.getElementById(report).style.color = "#5C0002";
	}
	if (el.value.length >= max-10) {
		document.getElementById(report).style.color = "#D40D12";
	}
	if (report) {
		document.getElementById(report).innerHTML = (max - el.value.length);
	}
}


if (window.addEventListener) 
    window.addEventListener("load", do_onload, false)
else 
    if (window.attachEvent) 
        window.attachEvent("onload", do_onload)
    else 
        if (document.getElementById) 
            window.onload = do_onload

if (enablepersist == "on" && document.getElementById) 
    window.onunload = saveswitchstate


function ajaxFunction(p, t){

    var ajaxRequest; // The variable that makes Ajax possible!
    try {
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } 
    catch (e) {
        // Internet Explorer Browsers
        try {
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) {
            try {
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
	
	document.getElementById(t).innerHTML = '<img src="ajax-loader.gif" />';
    var word = t;
    var queryString = "?token=" + word;
    ajaxRequest.open("GET", p + "ajax_input.php" + queryString, false);
    ajaxRequest.send(null);

}



function popUp(mypage,myname,w,h,scroll){
	var win= null;
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	var settings  ='height='+h+',';
	settings +='width='+w+',';
	settings +='top='+wint+',';
	settings +='left='+winl+',';
	settings +='scrollbars='+scroll+',';
	settings +='resizable=yes';
	win=window.open(mypage,myname,settings);
	if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}


function ajaxContactForm(theForm, url){

    var ajaxRequest; // The variable that makes Ajax possible!
    try {
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } 
    catch (e) {
        // Internet Explorer Browsers
        try {
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) {
            try {
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
	
	var name_element = document.getElementById('name');
	var email_element = document.getElementById('email');
	var subject_element = document.getElementById('subject');
	var message_element = document.getElementById('message');
	
	var name = name_element.value;
	var email = email_element.value;
	var subject = subject_element.value;
	var message = message_element.value;
	
	var reason = "";
	
	if (theForm.id == "contact_form") {
		reason += validateName(theForm.name);
		reason += validateEmail(theForm.email);
		reason += validateEmpty(theForm.subject);
		reason += validateEmpty(theForm.message);
	}
	else {
		reason += validateName(theForm.name);
		reason += validateEmail(theForm.email);
		reason += validateEmpty(theForm.subject);
		reason += validateEmpty(theForm.message);
	}
	
	if (reason != "") {
		
		//alert("Some fields need correction:\n" + reason);
		document.getElementById('mail_success').style.display = 'none';
		document.getElementById('mail_fail').style.display = 'block';
		//return false;
		
	} else {
		
		var ajax_url = url;
		var contact_from = name;
		var contact_email = email;
		var contact_subject = subject;
		var contact_message = message;
		
		var queryString = "?msg_from=" + contact_from + "&msg_email=" + contact_email + "&msg_subject=" + contact_subject + "&msg_message=" + contact_message;
		
		//alert(ajax_url + "ajax_email.incl.php" + queryString);
		
		ajaxRequest.open("GET", ajax_url + "ajax_email.incl.php" + queryString, false);
	    ajaxRequest.send(null);
		var response = true;
		
		if (response) {
			//alert("Your message has been sent");
			document.getElementById('mail_success').style.display = 'block';
			document.getElementById('mail_fail').style.display = 'none';
			document.getElementById('name').value = '';
			document.getElementById('email').value = '';
			document.getElementById('subject').value = '';
			document.getElementById('message').value = '';
			//message = 'Your message has been sent.';
		}
		
	}
	
	//return true;
	
}


function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.borderColor = "Red"; 
        error = "You need to type a message.\n"
    } else {
        fld.style.borderColor = "#CCCCCC";
    }
    return error;  
}

function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.borderColor = "Red"; 
        error = "Please enter your name.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 30)) {
        fld.style.borderColor = "Red"; 
        error = "Your name is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.borderColor = "Red"; 
        error = "The name you entered contains illegal characters.\n";
    } else {
        fld.style.borderColor = "#CCCCCC";
    }
    return error;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.borderColor = "Red"; 
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password is the wrong length. \n";
        fld.style.borderColor = "Red"; 
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.borderColor = "Red"; 
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
        fld.style.borderColor = "Red"; 
    } else {
        fld.style.borderColor = "#CCCCCC";
    }
   return error;
}  

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateName(fld) {
    var error = "";
    var illegalChars = /[0-9\(\)\<\>\,\;\:\\\"\[\]\!\@\#\$\%\^\&\*\?\{\}\+\=\_\|\`\~]+/; // dont allow numbers or odd characters
 
    if (fld.value == "") {
        fld.style.borderColor = "Red"; 
        error = "Please enter your name.\n";
    } else if ((fld.value.length < 2) || (fld.value.length > 30)) {
        fld.style.borderColor = "Red"; 
        error = "Your name is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.borderColor = "Red"; 
        error = "The name you entered contains illegal characters.\n";
    } else {
        fld.style.borderColor = "#CCCCCC";
    }
    return error;
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.borderColor = "Red"; 
        error = "Please enter your e-mail address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.borderColor = "Red"; 
        error = "Please enter a valid e-mail address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.borderColor = "Red"; 
        error = "The e-mail address contains illegal characters.\n";
    } else {
        fld.style.borderColor = "#CCCCCC";
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "Please enter your phone number.\n";
        fld.style.borderColor = "Red"; 
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.borderColor = "Red"; 
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.borderColor = "Red"; 
    }
    return error;
}


function offerStatusChange(p, t, r, id){

    var ajaxRequest; // The variable that makes Ajax possible!
    try {
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } 
    catch (e) {
        // Internet Explorer Browsers
        try {
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) {
            try {
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
	
	var img_id = r+id;
	var original_html = document.getElementById(img_id).innerHTML;
	
	document.getElementById(img_id).innerHTML = '<img src="ajax-loader-t.gif" />';
    var num = id;
    var queryString = "?id=" + num;
	
	ajaxRequest.open("GET", p + "offer_toggle.php" + queryString + "&t=" + t + "&r=" + r, false);
    ajaxRequest.send(null);
	var response = true;
	
	if (response) {
		if (original_html.split('check_gray.png')[0]==original_html) {
			new_html = original_html.replace(/check.png/, 'check_gray.png');
		} else{
			new_html = original_html.replace(/check_gray.png/, 'check.png');
		}
		document.getElementById(img_id).innerHTML = new_html;
		expandcontent(id)
	}
		
}


function deleteMedia(p, r, id){

    var ajaxRequest; // The variable that makes Ajax possible!
    try {
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } 
    catch (e) {
        // Internet Explorer Browsers
        try {
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) {
            try {
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
	
	var img_id = r+id;
	var original_html = document.getElementById(img_id).innerHTML;
	
	document.getElementById(img_id).innerHTML = '<img src="ajax-loader-t.gif" />';
    var num = id;
    var queryString = "?id=" + num;
	
	ajaxRequest.open("GET", p + "media_remove.php" + queryString, false);
    ajaxRequest.send(null);
	
}


function quickToggle(element, t, r, what, id) {
	
	var img_id = r+id;
	var original_html = document.getElementById(img_id).src;
	
	//alert(original_html);
	
	var num = id;
	var queryString = "?id=" + num;
	
	$.ajax({
		url: element + '?id=' + id + '&t=' + t + '&r=' + r + '&ajax=1',
		success: function(response) {
			if(response) {
				
				/*
				if(element.childNodes.length == 1 && element.childNodes[0].tagName == "IMG") {
					var image = element.childNodes[0];

					// Element was ticked, now should not be
					if(image.src.indexOf('check') != -1) {
						element.href = element.href.replace(what+'=0', what+'=1');
						image.src = image.src.replace('check', 'check_gray');
					}
					else {
						element.href = element.href.replace(what+'=1', what+'=0');
						image.src = image.src.replace('check_gray', 'check');
					}
				}
				*/
				
				if (what == 1) {
					new_html = original_html.replace(/check.png/, 'check_gray.png');
				} else if (what == 0) {
					new_html = original_html.replace(/check_gray.png/, 'check.png');
				}
				
				//alert(new_html);
				return new_html;
				
			}
		}
	});
}


function ajaxApproveMsg(p, t, i, l){

    var ajaxRequest; // The variable that makes Ajax possible!
    try {
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } 
    catch (e) {
        // Internet Explorer Browsers
        try {
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) {
            try {
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
	
	document.getElementById(l).innerHTML = '<img src="ajax-loader-t.gif" />';
    var word = t;
    var postid = i;
    var queryString = "?action=" + word + "&id=" + postid;
    ajaxRequest.open("GET", p + "ibdboard_comments.php" + queryString, false);
    ajaxRequest.send(null);
	
}


function ajaxCookieFunction(p, t){

    var ajaxRequest; // The variable that makes Ajax possible!
    try {
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } 
    catch (e) {
        // Internet Explorer Browsers
        try {
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) {
            try {
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
	
    var word = t;
	
	document.cookie = 'ibd_pm = ' + t;
	
}


