﻿
var xmlhttp
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
  try {
  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
 } catch (e) {
  try {
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  } catch (E) {
   xmlhttp=false
  }
 }
@else
 xmlhttp=false
@end @*/

if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
{
  try {
	xmlhttp = new XMLHttpRequest ();
  }
  catch (e) {
  xmlhttp = false}
}

function myXMLHttpRequest ()
{
  var xmlhttplocal;
  try {
  	xmlhttplocal = new ActiveXObject ("Msxml2.XMLHTTP")}
  catch (e) {
	try {
	xmlhttplocal = new ActiveXObject ("Microsoft.XMLHTTP")}
	catch (E) {
	  xmlhttplocal = false;
	}
  }

  if (!xmlhttplocal && typeof XMLHttpRequest != 'undefined') {
	try {
	  var xmlhttplocal = new XMLHttpRequest ();
	}
	catch (e) {
	  var xmlhttplocal = false;
	}
  }
  return (xmlhttplocal);
}

var mnmxmlhttp = Array ();
var xvotesString = Array ();
var mnmPrevColor = Array ();
var responsestring = Array ();
var myxmlhttp = Array ();
var responseString = new String;

// AYYASH KHABBR: changing this block to reflect new UI
function vote (user, id, htmlid, md5, value)
{
  	if (xmlhttp) {
		url = "/vote.php";
		var mycontent = "id=" + id + "&user=" + user + "&md5=" + md5 + "&value=" + value;
        
		anonymous_vote = false;
        if (anonymous_vote == false && user == '0') {
            window.location="/login.php?return="+location.href;
        } else {
    		mnmxmlhttp[htmlid] = new myXMLHttpRequest ();
    		if (mnmxmlhttp) {
    			mnmxmlhttp[htmlid].open ("POST", url, true);
    			mnmxmlhttp[htmlid].setRequestHeader ('Content-Type',
    					   'application/x-www-form-urlencoded');
    
    			mnmxmlhttp[htmlid].send (mycontent);
    			errormatch = new RegExp ("^ERROR:");    
    			//target1 = document.getElementById ('xvotes-' + htmlid);    
    			//mnmPrevColor[htmlid] = target1.style.backgroundColor;
    			mnmxmlhttp[htmlid].onreadystatechange = function () {
    				if (mnmxmlhttp[htmlid].readyState == 4) {
    					xvotesString[htmlid] = mnmxmlhttp[htmlid].responseText;
                        // AYYASH: TODO: i just dont understand this part at all!
    					if (xvotesString[htmlid].match (errormatch)) {
    						xvotesString[htmlid] = xvotesString[htmlid].substring (6, xvotesString[htmlid].length);
															changemnmvalues (htmlid, value, true);
							    					} else {
																						if (xvotesString[htmlid].substring (1, 6) == "ERROR") {
                                   alert(xvotesString[htmlid].substring (7)); 
                                   } else {
										changemnmvalues (htmlid, value, false); }
							    					}
    				}
    			}
    		}
    	}
	}
}


function unvote (user, id, htmlid, md5, value)
{
  	if (xmlhttp) {
		url = "/vote.php";
		var mycontent = "unvote=true&id=" + id + "&user=" + user + "&md5=" + md5 + "&value=" + value;
		anonymous_vote = false;
        if (anonymous_vote == false && user == '0') {
            window.location="/login.php?return="+location.href;
        } else {
    		mnmxmlhttp[htmlid] = new myXMLHttpRequest ();
    		if (mnmxmlhttp) {
    			mnmxmlhttp[htmlid].open ("POST", url, true);
    			mnmxmlhttp[htmlid].setRequestHeader ('Content-Type',
    					   'application/x-www-form-urlencoded');
    
    			mnmxmlhttp[htmlid].send (mycontent);
    			errormatch = new RegExp ("^ERROR:");    
				//target1 = document.getElementById ('xvotes-' + htmlid);
  				//target2 = document.getElementById ('xvote-' + htmlid);    
    			//mnmPrevColor[htmlid] = target1.style.backgroundColor;
    			mnmxmlhttp[htmlid].onreadystatechange = function () {
    				if (mnmxmlhttp[htmlid].readyState == 4) {
    					xvotesString[htmlid] = mnmxmlhttp[htmlid].responseText;
    					if (xvotesString[htmlid].match (errormatch)) {
    						 xvotesString[htmlid] = xvotesString[htmlid].substring (6, xvotesString[htmlid].length);
							changemnmvalues (htmlid, value, true,true);						} else {
															if (xvotesString[htmlid].substring (1, 6) == "ERROR") {
                                    alert(xvotesString[htmlid].substring (1, 6));  
                               	} else {
                                  
                                    changemnmvalues (htmlid, value, false, true);
								}
														
    					}
    				}
    			}
    		}
    	}
	}
}

// AYYASH KHABBR, changing few things in display of this function
function cvote (user, id, htmlid, md5, value)
{
	if (xmlhttp) {
		url = "/cvote.php";
		mycontent = "id=" + id + "&user=" + user + "&md5=" + md5 + "&value=" + value;
		anonymous_vote = false;

        if (anonymous_vote == false && user == '0') {
            window.location="/login.php?return="+location.href;
        } else {
    		mnmxmlhttp[htmlid] = new myXMLHttpRequest ();
    		if (mnmxmlhttp) {
    			mnmxmlhttp[htmlid].open ("POST", url, true);
    			mnmxmlhttp[htmlid].setRequestHeader ('Content-Type', 'application/x-www-form-urlencoded');    
    			mnmxmlhttp[htmlid].send (mycontent);
    			errormatch = new RegExp ("^ERROR:");
    			target1 = document.getElementById ('cvote-' + htmlid);      
    			mnmPrevColor[htmlid] = target1.style.backgroundColor;
    			//target1.style.backgroundColor = '#FFFFFF';
    			mnmxmlhttp[htmlid].onreadystatechange = function () {
    				if (mnmxmlhttp[htmlid].readyState == 4) {
    					xvotesString[htmlid] = mnmxmlhttp[htmlid].responseText;
    					if (xvotesString[htmlid].match (errormatch)) {
    						xvotesString[htmlid] = xvotesString[htmlid].substring (6, xvotesString[htmlid].length);						
    						changecvotevalues (htmlid, true);							
    					} else {
                        	
							//target1 = document.getElementById ('ratebuttons-' + id);
							//target1.style.display = "none";
                            document.getElementById ('ratebuttons-' + id + '-1').style.display = 'none';
                            document.getElementById ('ratebuttons-' + id + '-2').style.display = 'none';
							target2 = document.getElementById ('ratetext-' + id);
                            // AYYASH KHABBR, removing the text to the language file
							target2.innerHTML = "تم التصويت";
							changecvotevalues (htmlid, false);
    					}
    				}
    			}
    		}
    	}
	}
}

function changemnmvalues (id, value, error, unvote)
{
	split = new RegExp ("~--~");
	b = xvotesString[id].split (split);
	//alert(id);
	target1 = document.getElementById ('xvotes-' + id);
	target2 = document.getElementById ('xvote-' + id);
	target3 = document.getElementById ('xreport-' + id);
	target4 = document.getElementById ('xnews-' + id);
    // AYYASH
    target5 = document.getElementById ('xbox-' + id);
    // xvotes displays the new number, 
    // xvote is a link, should be disabled and title changes
    // xreport is a link, disable and change title
    // xnews change class to include cover
    // xbox is the khabbr box, change classname
    
    
    if (value > 0) {
    	// if unvoting
       
        target2.title = "مخبّرة";
        target2.href = "javascript://";
        target3.title = "";
        target3.href = "javascript://";
        
         if (unvote){
        	target2.title = "";
            target5.className += " mine";
            // TODO: make the block go away
            new Effect.Fade(target4);
            new Effect.Fade(target5);
        }else{
        	target5.className += " myvotes";
        }
    }else if(value < 0){
        target2.title = "";
        target2.href = "javascript://";
        target3.title = "خربانة";
        target3.href = "javascript://";
        target5.className += " burried";
        //target4.className += " cover";
        new Effect.Opacity(target4, {duration:0.8, from:1.0, to:0.3})
    }
       
   
    if (b.length <= 3) {
    	target1.innerHTML = b[0];
    }
    /*
	if (error) {
		if (value > 0) { target2.innerHTML = "<span>مخبّرة</span> "; }
		else if (value < 0) { target2.innerHTML = "<span>خربانة</span> "; }
		return false;
	}
	if (b.length <= 3) {
		target1.innerHTML = b[0];
		target1.style.backgroundColor = mnmPrevColor[id];
		new Effect.Fade(target3);
		if (value > 0) { target2.innerHTML = "<span>مخبّرة</span> "; }
		else if (value < 0 ) { target2.innerHTML = "<span>خربانة</span> ";
													new Effect.Opacity(target4, {duration:0.8, from:1.0, to:0.3}); }
	}
    */
	return false;
}

function changecvotevalues (id, error)
{
	split = new RegExp ("~--~");
	b = xvotesString[id].split (split);
	target1 = document.getElementById ('cvote-' + id);
	if (error) {
		return false;
	}
	if (b.length <= 3) {
		target1.innerHTML = b[0];
		target1.style.backgroundColor = mnmPrevColor[id];
	}
	return false;
}


function enablebutton (button, target)
{
	var string = target.value;
	if (string.length > 0) {
		button.disabled = false;
	} else {
		button.disabled = true;
	}
}


function checkfield (type, form, field)
{
// AYYASH KHABBR: changing the output
	url = '/checkfield.php?type='+type+'&name=' + field.value;
	checkitxmlhttp = new myXMLHttpRequest ();
	checkitxmlhttp.open ("GET", url, true);
	checkitxmlhttp.onreadystatechange = function () {
		if (checkitxmlhttp.readyState == 4) {
		responsestring = checkitxmlhttp.responseText;
			if (responsestring == 'OK') {
				document.getElementById (type+'checkitvalue').innerHTML = field.value + ' ' + responsestring;
               
				form.submit.disabled = '';
			} else {
				document.getElementById (type+'checkitvalue').innerHTML = field.value + ' ' + responsestring;
				form.submit.disabled = '';
			}
            document.getElementById (type+'checkitvalue').style.display = "";
		}
	}
  checkitxmlhttp.send (null);
  return false;
}

function emailto (original_id, htmlid, instpath, address_count)
{
	email_message = document.getElementById('email_message' + htmlid).value;
	email_address = document.getElementById('email_address_1_' + htmlid).value;
	if (address_count > 1){ 
		for ($i = 2; $i <= address_count; $i++) {
			if(document.getElementById('email_address_' + $i + '_' + htmlid).value != "") {
				email_address = email_address + ', ' + document.getElementById('email_address_' + $i + '_' + htmlid).value;
			}
		}
	}
	
	if (xmlhttp) {
		url = "/recommend.php";
		mycontent = "email_address=" + escape(email_address) + "&email_to_submit=submit&email_message=" + escape(email_message) + "&original_id=" + original_id + "&backup=0";
		target2 = document.getElementById ('emailto-' + htmlid);
		target2.innerHTML = "<br>Sending, please wait....";
		
		mnmxmlhttp[htmlid] = new myXMLHttpRequest ();
		if (mnmxmlhttp) {
			mnmxmlhttp[htmlid].open ("POST", url, true);
			mnmxmlhttp[htmlid].setRequestHeader ('Content-Type',
					   'application/x-www-form-urlencoded');

			mnmxmlhttp[htmlid].send (mycontent);
			errormatch = new RegExp ("^ERROR:");
			target1 = document.getElementById ('emailto-' + htmlid);
			mnmxmlhttp[htmlid].onreadystatechange = function () {
				if (mnmxmlhttp[htmlid].readyState == 4) {
					xvotesString[htmlid] = mnmxmlhttp[htmlid].responseText;
			
					if (xvotesString[htmlid].match (errormatch)) {
						xvotesString[htmlid] = xvotesString[htmlid].substring (6, xvotesString[htmlid].length);						
						target2 = document.getElementById ('emailto-' + htmlid);
						target2.innerHTML = xvotesString[htmlid];						
					} else {
						target2 = document.getElementById ('emailto-' + htmlid);
						target2.innerHTML = xvotesString[htmlid];
					}
				}
			}
		}
	}
}

function show_recommend(htmlid, linkid, instpath)
{
	var emailtodisplay=document.getElementById('emailto-' + htmlid).style.display ? '' : 'none';
	document.getElementById('emailto-' + htmlid).style.display = emailtodisplay;

	if (xmlhttp) {
		url = "/recommend.php";
		mycontent = "draw=small&htmlid=" + htmlid + "&linkid=" + linkid;
		target2 = document.getElementById ('emailto-' + htmlid);
		target2.innerHTML = "<span>انتظر ريثما يكتمل التحميل...</span>";		
		mnmxmlhttp[htmlid] = new myXMLHttpRequest ();
		
		if (mnmxmlhttp) {
			mnmxmlhttp[htmlid].open ("POST", url, true);
			mnmxmlhttp[htmlid].setRequestHeader ('Content-Type',
					   'application/x-www-form-urlencoded');

			mnmxmlhttp[htmlid].send (mycontent);
			errormatch = new RegExp ("^ERROR:");
			target1 = document.getElementById ('emailto-' + htmlid);
			mnmxmlhttp[htmlid].onreadystatechange = function () {
				if (mnmxmlhttp[htmlid].readyState == 4) {
					xvotesString[htmlid] = mnmxmlhttp[htmlid].responseText;			
					if (xvotesString[htmlid].match (errormatch)) {
						xvotesString[htmlid] = xvotesString[htmlid].substring (6, xvotesString[htmlid].length);						
						target2 = document.getElementById ('emailto-' + htmlid);
						target2.innerHTML = xvotesString[htmlid];						
					} else {
						target2 = document.getElementById ('emailto-' + htmlid);
						target2.innerHTML = xvotesString[htmlid];
					}
				}
			}
		}
	}
}